Walkthrough: Compiling a C Program

Visual Studio includes a C compiler that you can use to create everything from basic C programs to Windows API 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.

You must understand the fundamentals of the C language.

To create a C source file and compile it on the command line

  1. Open a developer command prompt. In Windows 8, on the Start screen, choose the Developer Command Prompt for VS2012 tile. In earlier versions, choose the Start button, expand All Programs, Microsoft Visual Studio 2012, and Visual Studio Tools, and then choose Developer Command Prompt for VS2012.

    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 for VS2012 and then choose Run as Administrator to successfully build and run the application that you create by following these steps.

    Note Note

    The Developer Command Prompt for VS2012 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.

  2. 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.

  3. At the command prompt, type notepad and press Enter.

  4. In Notepad, enter the following lines.

    #include <stdio.h>
    int main()
        printf("This is a native C program.\n");
        return 0;
  5. 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.

  6. Close Notepad.

  7. 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 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.
    Microsoft (R) Incremental Linker Version 11.00.50727.1
    Copyright (C) Microsoft Corporation.  All rights reserved.
  8. To see a list of files in the working directory, type dir and press Enter.

    The .obj file is an intermediate-format file that you can ignore.

  9. 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.

  10. To close the Command Prompt window, type exit and press Enter.