Walkthrough: Compiling a C Program

Visual C++ 2010 includes a C compiler that you can use to create everything from basic C programs to Windows API applications.

This walkthrough shows how to use a text editor to create a basic C program and then compile it on the command line.

You can use your own C programs instead of typing the sample programs that are shown in this walkthrough.

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. Choose the Start button, expand All Programs, Microsoft Visual Studio 2010, and Visual Studio Tools, and then choose Visual Studio 2010 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 Visual Studio 2010 Command Prompt and then choose Run as Administrator to successfully build and run the application that you create by following these steps.

    NoteNote

    The Visual Studio 2010 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.

  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 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 a .exe file name extension—for example, Simple.exe.

    Notice the executable program name in the lines of output information that the compiler displays.

    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved. 
    
    simple.c
    Microsoft (R) Incremental Linker Version 10.00.30319.01
    Copyright (C) Microsoft Corporation.  All rights reserved. 
    
    /out:simple.exe
    simple.obj
  8. To examine 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 exits:

    This is a native C program.

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft