Delen via


Walkthrough: Compiling a C Program on the Command Line

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

Prerequisites

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

    Note

    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.

  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. At the command prompt, enter dir and press Enter. You should see the source file you created:

    C:\simple>dir
    

Volume in drive C has no label. Volume Serial Number is CC62-6545

Directory of C:\simple

10/02/2012 03:46 PM <DIR> . 10/02/2012 03:46 PM <DIR> .. 10/02/2012 03:36 PM 102 simple.c 1 File(s) 102 bytes 2 Dir(s) 514,900,566,016 bytes free

The details will differ on your computer. If you don't see your source code file, make sure you've changed to the directory you created, and make sure you save your source file in it.
  1. 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.

    Output

    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.


> [!NOTE]
> <P>If you get an error such as "'cl' is not recognized as an internal or external command, operable program or batch file," you must set the environment for the compiler and tools. For details, review Step 1.</P>
> <P>If you get a compiler error or warning, review your source code for errors, then save it and run the compiler again. For more information about specific errors, use the search box on this page.</P>
  1. 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.

See Also

Tasks

Walkthrough: Creating a Win32 Console Program (C++)

Reference

C Language Reference

Other Resources

Building C/C++ Programs

Compatibility