Thursday 5 December 2019

C Tutorial - Structure & Function of C Program


Structure of C Program

Every C program is made of function and every function consist of instructions called statement.

Structure of C Program.

#include //stdio.h is the header file
main() // main function is the first function which is executed by a C program.

All C statements are written within main function.
{

// All C statements.

}

Functions

Every C program consists of one or more modules called functions. One of the functions must be called main( ).
The program will always begin by executing the main function, which may access other functions.

Any other function definitions must be defined separately, either ahead of or after main.

A function name is always followed by a pair of parenthesis, namely, ( ). And all function statements are enclosed within a pair of braces { }.

The program execution always starts with main function. When the closing brace of the main function is reached, program execution stops, and the control is handed back to the OS (Operating System).

Statements

Single C language instruction is called a statement. Statements are written according to the grammar of C language. Every C language statement must ends with semicolon(;).

In order to write a C program we should follow some basic rules which are described below:

a) Usually all statements in C are entered in small alphabets.

b) Blank spaces may be inserted between two words to improve the program readability. However no blank spaces are allowed within a variables, constants or key words.

c) It has no specific rules for the position at which statements is to be retained that's why it’s often called a free form language.

d) All C statements must end with a semicolon (; )

Till now, We have very little knowledge about the type of variables, constants and key words. Now we would try to understand the simple C program.


A sample of C language program

Example of C program to calculate area of circle:

#include 
#include 
#define PI 3.142
 
void main()
{
    float radius, area;
 
    printf("Enter the radius of a circle \n");
    scanf("%f", &radius);
    area = PI * pow(radius, 2);
    printf("Area of a circle = %5.2f\n", area);
}

Explanation of above program :

# is the preprocessor directive which commands that the content of file should be included at the time of compilation.

< stdio.h> is the header file which contains all input and output functions like scanf(), printf()which are frequently used in c programs.

main() is the first function which is executed by a C program.

1. int a,r=5; -> This statement declares 2 variables a and r of type integer and r has been assigned a value of 5.

2. const pi=3.14; -> This statement declares pi as constant and value 3.14 has been assigned to it.

3. a = pi * r *r; -> This statement computes the area of circle and assign it to variable a

4. printf("%f",a); -> This statement prints the area of circle using printf function.

5. getch(); -> It is used to get character from user.

Note: /* Any thing written within it is consider as comment statements. */


Previous                                                  Next

More Topics









100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...