C - Program Structure
Tags: c
Published: 2020-12-02 04:05:36

# Table of Contents:
* [Recap](#recap)
* [Program Structure](#program-structure) 
    * [Case Sensitivitiy](#case-sensitivity)
    * [Semicolons](#semicolons)
    * [Indentation](#indentation)
    * [Comments](#comments)
    * [The `main` Function](#the-raw-main-endraw-function)
    * [`int`](#-raw-int-endraw-)
    * [The Preprocesser](#the-preprocessor)

## 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:

```c
#include <stdio.h>

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

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:

```c
    printf("Hello World!");
    return 0;
```

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.

```c
    /* Multi-line */
    // Single-line
```

### 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](https://www.geeksforgeeks.org/exit-codes-in-c-c-with-examples/). Zero is the exit code for `EXIT_SUCCESS`. 

- [Exit Status](https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html)

### Preprocessor

The [C Preprocessor](https://en.wikipedia.org/wiki/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:

```c
#include <stdio.h>
```

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>`](https://www.tutorialspoint.com/c_standard_library/stdio_h.htm) 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`](https://en.wikibooks.org/wiki/C_Programming/stdio.h/printf) function. Without `<stdio.h>`, the program wouldn't compile.

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

```c
// 1.) #include <stdio.h>
// 2.) #include "stdio.h"
```

Thank you for reading!