Posted on

C Program Structure

Let us see the basic structure of C programming with a “Hello world” example. There are many text editors to compile and run our C program, some popular text editors are CodeBlocks, TurboC, Visual Studio Code, Sublime Text, and a normal text editor(eg: Notepad). Type the below example program and save the file with .c as extension.

In this entire tutorial, we are using CodeBlocks to compile and run the example programs.

#include <stdio.h>

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

Let us take a look at this simple program line by line

#include <stdio.h>

This line tells the compiler to include the standard library header file stdio.h in the program. A header file contains function declarations, data types, macros and Standard I/O component of the standard C library. Every C program must include the header file.

int main()

From this line, the function definition starts. main is the name of the function. The program execution starts from main() function. This function returns an integer type of value.

{
.....
}

The pairs of curly braces are used to indicate a block of code, where it begins and ends.

printf("Hello world!\n");

printf() is a function, which is used to print the output in the screen. “Hello world” is the string, that will be printed in the screen.

Note: In every C programs, the statements must be terminated with a semi-colon(;).

return 0;

When a main() function is defined, we will declare a function returning as int or void. int returns an integer value and void returns nothing.

In this example, the program will return a integer value 0, Traditionally, a return value of 0 indicates that the program executed successfully. After return 0; statement the program execution process will terminate.

Compile and Execute

After writing the code, save the file with .c as extension. Compile the program(Compiler convert our program to machine code), if it show any error correct it and don’t forgot to save the program after making any changes in the program, then recompile the program if it doesn’t shows any error, now run the program and you will get output as,

Hello world!

One comment on “C Program Structure”

  • admin March 4, 2021
    Reply

    Very well worded!

Write a comment