C Programming
In this tutorial we will learn about the basic structure of a C program.
Following is the basic structure of a C program.
Documentation | Consists of comments, some description of the program, programmer name and any other useful points that can be referenced later. |
Link | Provides instruction to the compiler to link function from the library function. |
Definition | Consists of symbolic constants. |
Global declaration | Consists of function declaration and global variables. |
main( ) { } | Every C program must have a main() function which is the starting point of the program execution. |
Subprograms | User defined functions. |
Lets explore the sections with an example.
In the following example we will find the area of a circle for a given radius 10cm.
The formula to compute the area of a circle is πr2
where π
is PI = 3.1416 (approx.) and r
is the radius of the circle.
Lets write the C code to compute the area of the circle.
/**
* file: circle.c
* author: yusuf shakeel
* date: 2010-11-25
* description: program to find the area of a circle
* using the radius r
*/
#include <stdio.h>
#define PI 3.1416
float area(float r);
int main(void)
{
float r = 10;
printf("Area: %.2f", area(r));
return 0;
}
float area(float r) {
return PI * r * r;
}
The above code will give the following output.
Area: 314.16
This section contains a multi line comment describing the code.
/**
* file: circle.c
* author: yusuf shakeel
* date: 2010-11-25
* description: program to find the area of a circle
* using the radius r
*/
In C, we can create single line comment using two forward slash //
and we can create multi line comment using /* */
.
Comments are ignored by the compiler and is used to write notes and document code.
This section includes header file.
#include <stdio.h>
We are including the stdio.h
input/output header file from the C library.
This section contains constant.
#define PI 3.1416
In the above code we have created a constant PI and assigned 3.1416 to it.
The #define
is a preprocessor compiler directive which is used to create constants. We generally use uppercase letters to create constants.
The #define
is not a statement and must not end with a ;
semicolon.
This section contains function declaration.
float area(float r);
We have declared an area
function which takes a floating number (i.e., number with decimal parts) as argument and returns floating number.
This section contains the main()
function.
int main(void)
{
float r = 10;
printf("Area: %.2f", area(r));
return 0;
}
This is the main()
function of the code. Inside this function we have created a floating variable r
and assigned 10 to it.
Then we have called the printf()
function. The first argument contains "Area: %.2f"
which means we will print floating number having only 2 decimal place. In the second argument we are calling the area()
function and passing the value of r
to it.
This section contains a subprogram, an area()
function that is called from the main()
function.
float area(float r) {
return PI * r * r;
}
This is the definition of the area()
function. It receives the value of radius in variable r
and then returns the area of the circle using the following formula PI * r * r
.
ADVERTISEMENT