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
Post a Comment