DEV Community

Cover image for C - Program Structure
Ethan Gustafson
Ethan Gustafson

Posted on • Edited on

C - Program Structure

#c

Table of Contents:

Recap

In the last blog, we configured our environment to use the C language. We had to use a simple code example in order to run a program:

#include <stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Now we'll learn a bit about the basic program structure in C.

Program Structure

Case Sensitivity

Everything in C is case sensitive. Lowercase and uppercase code matters here. If something is out of place, there will be compilation errors.

Semicolons

Every statement in C ends with a semicolon. Statements are lines of code within the code block. The code block is set by curly braces {}. The statements are within the code block above:

    printf("Hello World!");
    return 0;
Enter fullscreen mode Exit fullscreen mode

Those are required to end with a semicolon, otherwise, there will be compilation errors.

Indentation

Although the code above doesn't need to be indented in any way, it is written to be readable. Readable code is easy to understand. When you don't write readable code, it will be difficult to follow later on.

Comments

Comments will be ignored by the compiler. They won't produce errors. There are two ways to write comments, multi-line and single-line.

    /* Multi-line */
    // Single-line
Enter fullscreen mode Exit fullscreen mode

The main Function

In the example above, main is a function. The parenthesis after the name main() denotes that it is a function. The main function is special, as it is the first function that runs upon program execution. You are only allowed to write one main function.

int

If you're wondering what the int is before the main() function, that is used to define what the function will return. It is a return type. There are other ones besides int, but int defines main() as a function that will return an integer.

The function above returned zero. That is an exit code. Zero is the exit code for EXIT_SUCCESS.

Preprocessor

The C Preprocessor is an instruction sent to the compiler before the actual compilation takes place. It will analyze special statements in your code before the program runs.

They are usually defined at the top of a code file but can actually be placed anywhere in the code. Preprocessor statements begin with a pound sign # and follow with a directive. There are many different directives.

The #include directive was used above in the code example:

#include <stdio.h>
Enter fullscreen mode Exit fullscreen mode

The #include statement is a preprocessor directive that allows you to include code from other locations. It means that the code you're writing is dependent on code from another file.

stdio.h is a file name. This is a header file. The #include directive is going to include filenames ending with a .h extension. The <stdio.h> header file is a part of the C Standard Library. It stands for standard input-output. It provides functionality for displaying input and output.

<stdio.h> allows us to use the printf function. Without <stdio.h>, the program wouldn't compile.

You can also write the name of the file in two ways:

// 1.) #include <stdio.h>
// 2.) #include "stdio.h"
Enter fullscreen mode Exit fullscreen mode

Thank you for reading!

Top comments (0)