Walkthrough: Compiling a C Program on the Command Line
includes a C compiler that you can use to create everything from basic console programs to Windows Desktop applications.
This walkthrough shows how to create a basic C program by using a text editor, and then compile it on the command line.
You can use your own C programs instead of typing the sample programs shown in this walkthrough. You can also use any C code sample programs that are included in the Help topics.
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 regardless of file name extension, use the /Tc compiler option.
To create a C source file and compile it on the command line
Open a developer command prompt. In Windows 8, on the Start screen, open the Visual Studio Tools folder and then choose the Developer Command Prompt shortcut. In earlier versions, choose the Start button, expand All Programs, Microsoft Visual Studio, and Visual Studio Tools, and then choose Developer Command Prompt.
Depending on the version of Windows on the computer and the system security configuration, you might have to open the shortcut menu for Developer Command Prompt and then choose Run as Administrator to successfully build and run the application that you create by following these steps.
The Developer Command Prompt automatically sets the correct path of the C compiler and any required libraries. Use it instead of the regular Command Prompt window. For more information, see Setting the Path and Environment Variables for Command-Line Builds.
At the command prompt, create a directory for your source file and make it the current working directory. For example, type md c:\simple and press Enter to create a directory that's named Simple, and then type cd c:\simple and press Enter to change to that directory.
At the command prompt, type notepad and press Enter.
In Notepad, enter the following lines.
On the menu bar, choose File, Save to open the Save As dialog box. Navigate to the directory that you created. In the File name box, enter a name for your source file—for example, simple.c—and then in the Save as type drop-down list, select All Files (*.*). Choose the Save button to create a C source file in your working directory.
At the command prompt, specify the cl command together with the name of your source file—for example, cl simple.c—and press Enter to compile the program. The cl.exe compiler generates an .obj file that contains the compiled code, and then runs the linker to build an executable program that has the name of your source file, but has an .exe file name extension—for example, Simple.exe.
You can see the executable program name in the lines of output information that the compiler displays.
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86 Copyright (C) Microsoft Corporation. All rights reserved. simple.c Microsoft (R) Incremental Linker Version 11.00.50727.1 Copyright (C) Microsoft Corporation. All rights reserved. /out:simple.exe simple.obj
The version number of the tools depends on the version of Visual Studio and any updates you have installed.
To run your program, type its name without the file name extension—for example, simple—and press Enter.
The program displays this text and then exits:
This is a native C program.