The structure of a C program is typically organized into several parts. Here’s an outline of the key components of a basic C program:
1.Preprocessor Directives
These are instructions to the C preprocessor. They usually include library imports or macro definitions and start with #. Common examples:
#include <stdio.h> // For input/output functions
#include <stdlib.h> // For memory allocation, process control, etc.
2. Global Declarations (Optional)
Variables or functions declared outside any function can be used globally throughout the program.
3. The main() Function
This is every C program’s entry point(void main()). The main() function must be present, and execution starts here.
int main() {
// code here
return 0;
}
4. Variable Declarations
Inside the main() function (or any other function), you can declare variables that will be used within the scope of that function.
int num;
float price;
5. Executable Statements
These are the operations or actions your program will perform. These can include calculations, function calls, loops, and conditionals.
num = 10;
printf(“The value of num is: %d”, num);
6. Functions (Optional)
Functions other than main() can be defined before or after the main() function. These functions can be used to modularize the code for reusability and clarity.
void myFunction() {
printf(“Hello from myFunction!\n”);
}
7. Comments (Optional but Recommended)
Comments are used to make the code more readable. They can be single-line (//) or multi-line (/* */).
// This is a single-line comment
/* This is a multi-line comment */
What is the Program Structure?
1)Documentation Section—Include Comment
2)Link Section—-Include header file
3)Definition Section—Include symbolic
4)Global Declaration—Include Global Variable
5)Main Section—Include the main function
6){
7)Declaration part—Include variable declaration
8)Executable part—Include a statement
9)}
FILES CREATED……
.exe file ——– required computer to execute the program.
.obj file ——–After compiling the program this file of the program is generated.
Structure of C Program with Example
//program of arithmetic calculation… Documentation Section
//program of arithmetic calculation… Documentation Section
#include<stdio.h> //header file ….Link Section
void main() //Main section
{
int firstno,secno,resultno; //variable declaration
clrscr(); //clear the previous screen output
/*print the message for inputting the value(optional)*/
printf(“enter the value of firstno and secno”);
/*scanf is used input the value to the variables(& address operator)*/
scanf(“%d%d”,&firstno,&secno);
resultno=firstno*secno; //calculation
printf(“c=%d”,resultno); //print the output with message
getch(); //get the character
}
Explanation
The main()Function–> The main () consists of the name main followed by a pair of empty parentheses (()) & a pair of braces({}). Within the braces is the statement that makes up the main body of the program. The program execution starts at the first statement in main() and terminates at the last statement in main().
The # include directive(Preprocessor)–>An include file is a separate disk file that contains information needed by your program or the compiler. Several of these files (sometime called header files) are supplied with your computer.
Variable declaration–> A variable is a name assigned to a data storage location.
Program Statement–> C statement displays information onscreen, reads keyboard input, performs math operation, etc.
Printf–> This statement is a library function that displays information on the screen.
Scanf–> It reads data from the keyboard and assigns that data to one or more program variable.
Braces{}–> A group of one or more statements enclosed within braces is called a block.
Functions in C