Skip to main content

Structure Of C Program

Any program written in C should follow a specific struture as defined by the standard. If defined structure of C program is not followed, it will lead to a compilation error.

Example of Structure Of C Program

// Include Header files
#include "stdio.h" //printf, scanf etc

// Start of C application
int main()
{
// Print to Console/Terminal
printf("Hello World\n");

// Return Success
return 0;
}
Output:

Description of Structure Of C Program

Including Header files

Very first component of any C Program is "inclusion of the header files". The Header files have .h extension, and usually contain define libraries of pre-written functions, as well as constants, macros, and other types of preprocessor variables that the program may need.

By including the necessary header files in the program, you can make sure that the compiler has access to all the information it needs to compile and run the code.

For example, the following line of code includes the stdio.h header file, which provides declarations and definitions for standard input and output functions such as printf() and scanf():

#include <stdio.h>

#include

It is a preprocessor directive that instructs the compiler to include the contents of a header file in the program.

Some of the most common header files from standard libraries are:

  • stddef.h - Defines useful types and macros.
  • stdio.h - Defines input and output functions, like printf
  • stdlib.h - Defines functions like atoi, malloc etc for memory allocation
  • string.h - Defines string manipulation functions
  • math.h - Defines common maths functions - like square root

Main() Function

The next component of a C program is the main() function. It is the entry point of a C program.

When a C program is executed, the operating system first looks for the main() function and then starts executing the instructions inside it.

Following is the syntax to declare the main function:

int main() {}

Variables

The next component of a C program is the declaration for variables, that are to be used within the function. Variables are used to store and manipulate data in a program, they provide means to organize data in the program.

Following is the syntax to declare variable:

char var[] = "world";

declaration

In the C program, no variable can be used without being declared i.e, the variables are to be declared before any operation in the program.

Also, When you declare a variable in C, you must specify its data type and a name that you'll use to refer to it later in the program.

The Body Part

The body of C program, refers to the statements, expressions and operations that are performed in the program. It is contained within the main() function.

For Example - the below statement simply prints a string Hello and a variable var.

printf("Hello %s\n", var);

more than one statement

The body of a C program can contain many more statements like core logic of the program, operations like - searching, sorting, printing, getting inputs from user and can be much more complex than this example.

Return Statement

The last component of any C program is the return statement. The return statement returns some values from a function or program.

In the above program, return 0; is the return statement, which returns the status of the program. Here '0' means success.

return type

The value being returned should match the type of the specified return type. Void being an exception, where nothing is returned, and return statement will not be used.