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

1. Hello World

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

To begin programming in C, you need a text editor and a C compiler. You can get both at the same time by installing an Integrated Development Environment (IDE) that includes support for C. A good choice is Microsoft’s Visual Studio Community Edition, which is a free version of Visual Studio available from Microsoft’s website.1 This IDE has built-in support for the C89 standard and includes most features up to C99 as of the 2017 version.

Some other popular cross-platform IDEs include Eclipse CDT, Visual Studio Code, Code::Blocks, and CodeLite. Alternatively, you can develop using a simple text editor—such as Notepad—although this is less convenient than using an IDE. If you choose to do so, just create an empty document with a .c file extension and open it in the editor of your choice. By convention, the .c extension is used for files that contain source code for C programs.

Creating a Project

After installing Visual Studio, with the C++ component selected during installation, go ahead and launch the program. You then need to create a project, which will manage the C source files and other resources. Go to File ➤ New ➤ Project to display the New Project window. From there, select the Visual C++ template type in the left frame. Then select the Empty Project template in the right frame. At the bottom of the window you can configure the name and location of the project. When you are finished, click the OK button and the wizard will create your empty project.

Adding a Source File

You have now created a C/C++ project. In the Solution Explorer panel (View ➤ Solution Explorer), you can see that the project consists of four empty folders: External Dependencies, Header Files, Resource Files, and Source Files. Right-click on the Source Files folder and select Add ➤ New Item. From the Add New Item dialog box, choose the C++ File (.cpp) template. Give this source file the name myapp.c. The .c file extension will make the file compile in C instead of C++. Click the Add button, and the empty C file will be added to your project and opened for you.

Hello World

The first thing to add to the source file is the main function. This is the entry point of the program, and the code inside of the curly brackets is what will be executed when the program runs. The brackets, along with their content, are collectively referred to as a code block, or just a block.
int main(void) {}
Your first application will simply output the text "Hello World" to the screen. Before this can be done, the stdio.h header needs to be included. This header provides input and output functionality for the program, and is one of the standard libraries that comes with all C/C++ compilers. What the #include directive does is effectively replace the line with everything in the specified header before the file is compiled.
#include <stdio.h>
int main(void) {}
With stdio.h included you gain access to several new functions, including the printf function that is used for printing text—in this case to a console window. To call this function, you type its name followed by a set of parentheses that includes the text string that will be displayed. The string is delimited by double quotes, and the whole statement is followed by a semicolon. The semicolon is used in C to mark the end of a code statement.
#include <stdio.h>
int main(void) {
  printf("Hello World");
  return 0;
}

The main function here ends with a return statement , which returns a status code as the program exits. This can be useful if the intent is for your program to be executed by another program. The status code can then signal to the caller the success or failure of your program to complete its function. By convention, the return code 0 is used to indicate that a program or function has executed successfully.

The C89 standard requires the return statement to be present, but following C90 the statement became optional. As of C90 the compiler will automatically include the return statement if it is omitted. For brevity the statement will be left out from future code examples.

IntelliSense

When writing code in Visual Studio, a window called IntelliSense will pop up wherever there are multiple predetermined alternatives from which to choose. This window can also be brought up manually at any time by pressing Ctrl+Space to provide quick access to any code entities you are able to use within your program. This is a very powerful feature that you should learn to make good use of.