© Mikael Olsson 2019
Mikael OlssonModern C Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4288-9_2

2. Compile and Run

Mikael Olsson1 
(1)
Hammarland, Länsi-Suomi, Finland
 

Before a program can run, the source code has to be translated into an executable format by a compiler. This step transforms the human-readable source code into binary machine code, which is a sequence of instructions that can be executed by a computer.

Visual Studio Compilation

Continuing from the last chapter, the Hello World program is now complete and ready to be compiled and run. You can do this by going to the Build menu and choosing Debug ➤ Start Without Debugging (Ctrl+F5). Visual Studio then compiles and runs the application that displays the text in a console window.

Depending on your version of Visual Studio, the console window displaying Hello World may close as soon as the main function has finished executing. To prevent this, you need to explicitly specify that this is a console application. First right-click the Project node in the Solution Explorer and then click on Properties to bring up the project’s properties window. From there, navigate to Configuration Properties ➤ Linker ➤ System and set the SubSystem option to Console using the dropdown list. Click OK and the console window will now no longer close automatically.

Another way to prevent the console window from closing is to add a call to the getchar function at the end of main. This function, included with the stdio.h header, will read a character from the keyboard and thereby prevent the program from exiting until the return key is pressed.
#include <stdio.h>
int main(void) {
  printf("Hello World");
  getchar();
}

Console Compilation

As an alternative to using an IDE, you can also compile source files from a terminal window as long as you have a C compiler. For example, on a Linux machine you can use the GNU C compiler, which is available on virtually all UNIX systems—including Linux and the BSD family—as part of the GNU Compiler Collection (GCC). This compiler can also be installed on Windows by downloading MinGW1 or on Mac as part of the Xcode development environment.2

To use the GNU compiler, you type its name gcc in a terminal window and give it the input and output filenames as arguments. It then produces an executable file, which when run gives the same result as one compiled under Windows in Visual Studio.
gcc myapp.c -o myapp.exe
./myapp.exe
Hello World

Comments

Comments are used to insert notes into the source code. They have no effect on the end program and are meant only to enhance the readability of the code, both for you and for other developers. The C89 standard featured only one comment notation, a multiline comment delimited by /* and */.
/* multi-line
   comment */
The C99 standard added the single-line comment, which starts with // and extends to the end of the line. This comment was standardized since it was a convenient feature found in many other programming languages, such as C++. Many C compilers also started to support the single-line comment long before the C99 standard was formalized.
// single-line comment

Keep in mind that whitespace characters—such as comments, spaces, and tabs—are generally ignored by the compiler. This gives you a lot of freedom in how you format your code.