C Program Structure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

C PROGRAM STRUCTURE

A C program involves the following sections:

• Documentations (Documentation Section)


• Preprocessor Statements (Link Section)
• Global Declarations (Definition Section)
• The main() function
o Local Declarations
o Program Statements & Expressions
• User Defined Functions

Documentation Section
Comments that we use in a c program comes under the
documentation section. Comments are a way to tell the
compiler that this block of code should be ignored.

Comments are of two types:

• Single line comment:- //


• Multiple line comment:- /*Example code*/

Example:-
/* Assignment for C
Date: 16-12-2021
Description:
Describing documentation section*/
#include<stdio.h>

void main()
{
printf("Hello, MSI!\n");

}
Preprocessor statement( Link section)
The header files that we use to include in the program comes
under the link section. The link section provides instructions to
the compiler to link functions from the system library such as
using the #include directive.
The function we use in our program such as printf(), scan()
comes from the stdio(standard input output) header file.
All the header files included in the program must be enclosed
within angular brackets.
Link section looks like this:
#include <stdio.h>

Example:-
#include<stdio.h>

void main()
{
printf("Hello, World!\n");
return;
}

Definition section
The definition section is used to define a variable
globally. Suppose the value of pi is 3.14 and we don’t
want to declare it everytime we are taking the value of
pi. So there, we can use #define pi = 3.14 it will globally
store the value of pi as defined.
Example:-
#include <stdio.h>
#define name "anas"
void main(){
float pi, radius;
printf(“Name of student is &c:”, name);
}

The main() function


The execution of every c program starts from main()
function. All the code written in a main function will be
executed first then the user-defined functions would be
executed.

This is the main function, which is the default entry point


for every C program and the void in front of it indicates
that it does not return a value.
main() always returns a numeric value. The return 0
value tells the compiler to terminate the program and if
there’s an error in the code then it returns a non zero
value. The bigger the return value the more severe will
be the error.
Example:-
int main() {
printf(“Hello, MSI”);
return 0;
}
USER DEFINED FUNCTIONS
When we want to define your function that fulfils
a particular requirement, we can define them in
this section.
Suppose, we need to create a circle and color it
depending upon the radius and color. We can create
two functions to solve this problem:

#include <stdio.h>
int addNumbers(int a, int b);
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2);
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b)
{
int result;
result = a+b;
return result;
}

You might also like