Compiling a C Program
Visual C++ 2008 includes a C compiler that you can use to create everything from basic C programs to Windows applications.
The steps in this document show how to create a basic C program by using a text editor and then compile it on the command line.
Instead of using the programs shown in this walkthrough, you can use your own programs. You can also use any of the sample C programs that are included in other Help documents.
By default, the Visual C++ compiler treats all files that end in .c as C source code, and all files that end in .cpp as C++ source code. To force the compiler to treat all files as C source code regardless of extension, use the /Tc compiler option.
Prerequisites
You must understand the fundamentals of the C++ language.
To create a C source file and compile it on the command line
Click Start, click All Programs, click Microsoft Visual Studio 2008, click Visual Studio Tools, and then click Visual Studio 2008 Command Prompt.
Depending on the version of Windows on the computer and the system security configuration, you might have to right-click Visual Studio 2008 Command Prompt and then click Run as Administrator to successfully run the application that you create by following these steps.
Note
Instead of the system Command Prompt window, we recommend that you use the Visual Studio 2008 Command Prompt window because it automatically sets the correct path of the Visual C++ compiler and any required libraries. For more information, see Setting the Path and Environment Variables for Command-Line Builds.
At the command prompt, type notepad simple.c and press ENTER.
When you are prompted to create a file, click Yes to create simple.c and open it in Notepad.
In Notepad, type the following lines.
#include <stdio.h> int main() { printf("This is a native C program.\n"); return 0; }
Save the file and close it.
At the command prompt, type cl simple.c and press ENTER.
The cl.exe compiler generates an executable program named simple.exe.
The name is displayed in the compiler output, which resembles this:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. simple.c Microsoft (R) Incremental Linker Version 9.00 Copyright (C) Microsoft Corporation. All rights reserved. /out:simple.exe simple.obj
To see a list of all files in the \simple\ directory, at the command prompt, type dir simple.* and press ENTER.
The .obj file is an intermediate format file that you can safely ignore.
To run simple.exe, at the command prompt, type simple and press ENTER.
The program displays the following text and exits:
This is a native C program.
To close the Visual Studio 2008 Command Prompt window, type exit and press ENTER.
Next Steps
Previous:Compiling a C++ Program that Targets the CLR in Visual Studio (C+) | Next:Creating Windows Applications (C+)
See Also
Tasks
Creating Command-Line Applications (C+)
Reference
Other Resources
Change History
Date |
History |
Reason |
---|---|---|
January 2010 |
Added suggestion to run example code as administrator. |
Customer feedback. |