3. Writing your first C program

So, Let’s start by writing our first ever code in C language. Open the codeblocks (or your preferred IDE) and write the code below.

Figure 1: The code

Here, I have written just 6 lines of code…Let’s explain the lines one by one.

Line 1 

Let’s skip it for simplicity. I will explain it after line 5. So stay patient 😀

Line 2

This is a blink line. The compiler ignores these kind of blank lines.

Line 3 

void main () : This is a function body. Consider it the starting point of the compiler. In easy words, the, compiler will look for main() in a program and start to execute everything that is written inside its curly brackets {}.

Figure 2: Simplified illustration

Line 4

{ : starting of curly bracket

Line 5

printf(“Hello world!”); : This is a function call. Wait, what is function call?

Any word( “printf”  in this case) followed by “()” is a function call in C. Here, printf() is a function call for our case. We use this function to do a task for us. What task?

The task is to print something on the monitor so that we can read or see the output. Everything written in the “ ” inside the parenthesis of printf will be shown on the output screen. Here in our case, we have written “Hello world!”, so Hello world! will be on our output screen.

And one thing not to miss out: the little ; at the end of line 5. You can think it as a full stop. In the C programming, we end a line with semicolon. If you miss it out, you will obviously get an error.

One thing is still not clear to me: how does printf() print something on the monitor?

The answer is that you don’t need to worry about this as this is a built-in function (you don’t need to worry about how built-in function work). The compiler handles how printf() will function. The compiler has a specific file named stdio.h which directs printf() to print something on the output screen (you can think stdio.h as a helper or header file for your program). And this explains why we include stdio.h in the very first line.

Line 1

#include<stdio.h> : This is the grammar of C language how to include a helper or header file to your code. Any valid helper file name written inside #include<> will be included to your code.

Line 6

} : closing of curly bracket.

Now you press Build and run (Figure 3), you will see the output (Figure 4) as expected.

Figure 3: Build and run the code

Figure 4: Output

Congratulations on your first-ever successful C program!

So, this is as simple as that. I hope you grasp these basics very tight. If not, please let me know in the contact.
Stay well … !