Skip to main content

Structure of C Program with Example | C Programming

Structure of C program

In this post I will tell you the basic structure of C program 

with example in C programming in very simple words.


Our First C program :

# include<stdio.h>

int main()

{

           printf("Hello World");

           return 0;

}

save te above program with file name "First.c"

Note : Every C language file should be saved using .C extention name.

Basic structure of a C program :

All C programs have to follow a basic structure.
The execution of a C program starts from the main() function. The C compiler starts execution from the main() function and executes each statement written inside the main() function line by line.

Each instruction of a C program should be terminated by a semicolon ( ; ) i.e. every line of a C program ends with the semicolon ( ; ).

1.     Every program execution starts from the main( ) function.

2.     All the statements written in the program should terminated by semicolon ( ; ).

3.     C is a case sensitive language so, the instructions are alsio case sensitive.

4.     All the instructions are executed in the same order in which they are written in the main()  function.

Comments :

Comments are those lines written in the C code for the convinence of the coders. These lines are not the actual code of c language. It is a way for us to add notes to our program.
There are two types of comments in C language :

1.     Single line comment : // This is a comment

2.     Multi line comment : /* This is a
                                             multiline comment */

Comments in a C pogram are not executed and ignored by the C compiler.

Compilation and Execution process of a C program:

First.c ----> C compiler  ----> First.exe

The C program is written as a plain text in a editor program. The C programis a combination of instructions following a particular sequence.

The compiler is a machine or a computer program which converts the source code written by us into machine code which is understandable by the computer. The compiler first checks the program written by us and if there is any mistake in writing it throws an error then after correcting the error by us it converts it into  .exe file which is what we need to create by our code.

Finally the .exe file is the executable file or program which we created using C language.

Format Specifiers :

The format specifiers are used to print or take data from the user.

1.     %d is a format specifier used for integers or int datatype

2.     %f is a format specifier used for real or float values or float datatype

3.     %c is a format specifier used for characters or char datatype

4.     %dl is a format specifier used for double datatype

Library Functions in C :

C language has a lot of inbuilt library functions. These library functions are very usefull and valuable and it helps us to write our code efficently. They are used to carry out certain tasks.

For instance printf() function is used to print values on the screen.

syntax of printf function is :
printf("This is %d " , i );

This will print the value of i on the console.

scanf function --> It is used to take input from the user.

syntax of scanf() function :
scanf("%d", &i);

This will take input from user by keyboard.
while taking input from the user the addressof operator ( & ) is very important.

 

 


Comments

Popular posts from this blog

What Are Variables in C language | Constants and Keywords in language

Variables are the containers in C language which are used to store data in the program. Variables are the small memory blocks which holds some values in it. Variables in c language :       What are variables in C language ?     In C language, A variable is just like a container that contains values of different data           types. A variable is an entity whose value can be change.      This means that to store any value we use variables in C language. These variables can e        imagined as the small memory blocks in which the data is stored of different types. The           variables are the one of important components of c language as it is used most                          frequently inside the programs of C language.        For Instance In our Kitchen we have differen...

C Programming Examples | C Examples | C programming Exercises, Practice, Solution

  Here are few very simple and basic C language programs with C programming examples that you will use to understand the basics very easily. Write a C programme to calculate area of a rectangle : #include<stdio.h> int main(){           Int l, b, area;           printf(“Enter length and breadth of a rectangle \n”);           scanf(“%d %d”, &l, &b);           area = l *b;           printf(“Area of rectangle is %d \n”, area);           return 0; } Write a C program to calculate area of a circle : #include<stdio.h> int main(){           float r, area;           printf(“Enter the radius of the...