Tek Eye Logo

Tek Eye

How to Compile C in Windows with Visual Studio

The C programming language was created in the 1970s and became widely used to program all types of computers, from simple embedded microcontrollers to multiprocessor supercomputers. It remains a beneficial computer language for programmers to know and has influenced the development of many other computer languages.

Windows Hello World

It makes sense to be able to write, compile, run, and debug C code on your computer. (If you're starting out in programming see What is Computer Programming?). After writing the C code in a file editor and saving it to a .c file it needs to be compiled into a program executable that the computer can run. There are a great many free C compilers which you can choose to use. On a Windows computer, you can use Microsoft's own C compiler. It can be installed with the Microsoft Visual Studio Integrated Development Environment (IDE), Microsoft's main software development tooling. Let's see how to compile C in Windows with the Windows compiler installed by Visual Studio.

Install Windows C Compiler with Visual Studio

If you are learning to code, contributing to open source projects, or developing code for your personal use, then you can select and install the free Visual Studio Community version. You will still need to register the software, but once registered the Community version is free to use. You may be lucky and can get the Professional or Enterprise versions from your workplace.

To compile a C program in Windows, you need to select the Desktop development with C++ option when installing Visual Studio. The Microsoft compiler supports C and C++ programs (C++ is a follow-on from C). If the Visual Studio IDE is already installed use the Tools then Get Tools and Features menu to reopen the Visual Studio installer and select and install the Desktop development with C++ option.

Windows C Compiler

How to Check that the Microsoft C Compiler is Installed

With Visual Studio installed, it is now possible to use the Developer Command Prompt. From this command prompt it is possible to check that the Microsoft C Compiler is installed on your Windows machine. Simply start typing Developer Command in the Windows search bar (press the Windows key to access the search) and select the Developer Command Prompt app.

Starting the Developer Command Prompt

In the command prompt run the compiler by entering cl and pressing return. If the C compiler is present you will see a message from it.

Running the Windows C compiler

The command prompt will have set up a path to the C compiler. This means you can switch to a directory where you will be keeping your C project files and be able to access the compiler from there.

Compile an Example Hello World C Program

Here is a simple C program that prints the message Hello World. Add this code to a file using Windows Notepad or another file editor, such as Notepad++ or the Visual Studio IDE. Save this program in a file with a .c extension, e.g., hello.c.

#include <stdio.h>

int main() {
    printf("Hello World!");
    return 0;
}

Change to the file location in the Developer Command Prompt and run the compiler with the file name using, for example, cl hello.c. This will compile the C code into a Windows program called hello.exe. Running hello.exe in the command prompt will print the message. See the first image at the start of the article to see it in action.

Compile More Than One C File in Windows

We can change the above example C code to call a function in another file. Here is a C file printing a message via a sayhello function. The code is saved into the same directory in a file called another.c:

#include <stdio.h>

void sayhello() {
  printf("Hello from another file.");
}

Now change hello.c. It references the new sayhello function before using it. A line break has been added to the end of the previous message:

#include <stdio.h>
void sayhello();

int main() {
  printf("Hello World!\n");
  sayhello();
  return 0;
}

If I had lots of functions in another file I would have listed them in a .h file, a header file. Then used the #include directive to read the header file into the main program file.

You can now pass the new another.c file to the compiler along with the hello.c. The two code files are added to the compiled program:

Compile two C files

Compile and Link a Windows C DLL

We can get the C compiler to produce a Windows dynamic link library, a DLL, for the another.c file. Then call the sayhello function located in the DLL file from the hello.exe file. To use the Windows DLL file the symbols for sayhello are exported into a library, or .lib file. (Another type of library file, a static library, can contain compiled code, but in this case, only the symbols are needed as the code is in the DLL.) The library file is used to tell the compiler about the DLL contents. Telling the C compiler to export the correct library symbols is done by declaring sayhello to be DLL exported:

#include <stdio.h>

__declspec(dllexport) void __cdecl sayhello(void) {
  printf("Hello from another file.");
}

We then import sayhello in hello.c:

#include <stdio.h>
__declspec(dllimport) void sayhello();

int main() {
  printf("Hello World!\n");
  sayhello();
  return 0;
}

Now compile another.c on its own but tell the C compiler to generate another.dll using the /LD compiler switch. The another.lib file is generated. Then compile hello.c and pass it another.lib so it can be linked to the DLL:

Compile and link a C DLL

You now have the basics set up to enable the development of C code on Windows.

See Also

Author:  Published:  

ShareSubmit to TwitterSubmit to FacebookSubmit to LinkedInSubmit to redditPrint Page

Do you have a question or comment about this article?

(Alternatively, use the email address at the bottom of the web page.)

 This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.


Articles on:

Android Programming and Android Practice Projects, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS, C# Programming, Using Windows for Programming


Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples



Tek Eye Published Projects