You are on page 1of 4

2.

The structure of a C program

a. Directives

Before a C program is compiled in a compiler, source code is processed by a program 


called preprocessor. This process is called preprocessing. Commands used in 
preprocessor are called preprocessor directives and they begin with “#” symbol. 
Below is the list of preprocessor directives that C programming language offers. 

Syntax: #define 

Macro 
This macro defines constant value and can be any of the basic data 
types. 

Syntax: #include <file_name> 


Header file 
inclusion  The source code of the file “file_name” is included in the main 
program at the specified place. 

Syntax: #ifdef, #endif, #if, #else, #ifndef 


Conditional 
compilatian  Set of commands are included or excluded in source program before 
compilation with respect to the condition.  

Syntax: #undef, #pragma 


Other 
directives  #undef is used to undefine a defined macro variable. #Pragma is used 
to call a function before and after main function in a C program. 

 
b. Main function () 

Every C-programs needs to have the main function. Each main function 
contains 2 parts. A declaration part and an Execution part. The declaration 
part is the part where all the variables are declared. The execution part 
begins with the curly brackets and ends with the curly close bracket. Both 
the declaration and execution part are inside the curly braces. 

int main(void)
{
int a=10;
printf(" %d", a);
return 0;
}

c. User functions 

A function is a block of code that performs a specific task. C allows us to 


define functions according to a specific need. These functions are known as 
user-defined. For example: 

createSquare () function 

color () function 

 
A
​ n example to add 2 integers 

#include <stdio.h> 

int addNumbers(int a, int b); ​ // function prototype 

int main() 

int n1,n2,sum; 

printf("Enters two numbers: "); 

scanf("%d %d",&n1,&n2); 

sum = addNumbers(n1, n2); ​ // function call 

printf("sum = %d",sum); 

return 0; 

int addNumbers(int a, int b) ​ // function definition   

int result; 

result = a+b; 

return result; ​ // return statement 

 
d. Execution stages 

The C program follows many steps in execution. To understand the flow of C


program well, let us see a simple program first.

#include <stdio.h>

int​main(){

printf(​"Hello C Language"​);

return​0;

1) C program (source code) is sent to the ​preprocessor​first. The ​preprocessor​is


responsible to convert ​preprocessor directives​into their respective values. The
preprocessor generates​an expanded source code.

2) Expanded source code is sent to the ​compiler ​which compiles the code and
converts ​
it into​assembly code​.

3) The​assembly code​is sent to the assembler which ​assembles​the code and


converts​it into ​
object code​. Now a simple.obj file is generated.

4) The ​object code​is sent to linker which links it to the library such as header files.
Then it is ​
converted ​
into​executable code​. A simple.exe file is generated.

5) The ​executable code​is sent to the loader which ​loads​it into memory and then
it is ​executed​
. After execution, output is sent to the console.

You might also like