Basic C program structure

In this post, we will look at Basic C Program Structure. Probably you would have taken C programming classes in academic and you’re aware of basic C program. If this is the case then feel free to skip and proceed for next lesson. Here we will explore building blocks or elements of most famous Hello World! Program. Every C program includes couple of preprocessor directives, functions, variable, some loops and comments. Don’t worry if you’re not getting any of these for a while. We will discuss one by one.learn-embedded-system

Let’s take a look at each line of code in this C Program. We have to look from top to bottom because that the usual flow of code while execution of any C Program.

C Program Building Blocks
C Program Building Blocks
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}

Code Explanation: Basic C Program Structure

#include <stdio.h>
#include <stdlib.h>

These are standard include statements which tells the compiler to include header file for compilation. Usually a header file includes function prototypes and pre-processor directives. Some of these are standard header files such as: stdio.h, stdlib.h, unistd.h, math.h etc etc.

int main()
{
    
}

main() is a Function. Every C Program must have minimum one function and this is that one function. The main() function is always called when program first executes.

{} These curly braces include collection of commands to do something. Whatever commands we write in here defines a main function body.

printf("Hello world!\n");

The printf function is standard C function which use to display output on screen. So we have used printf to display text “Hello World!” on output screen. “\n” this funny looking symbol is treated as single character which stands for newline. Newline moves the cursor on your screen to next line. The “;” (semicolon) tells the compiler to terminate statement.

return 0;

Finally, at the end of the program return 0; terminates the main function and return the value 0. Since this main function is of type integer (int). Here goes output when we build and run this program.

output from hello.c program
Output from hello.c Program

This is it for this lesson and we hope that now you have understanding of Basic C Program Structure. In future lesson we will explore different features and build more complex projects and write multiple file projects. We hope that you will find this post educational. Thanks and see you in next lesson.

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Login

Register | Lost your password?