Basic Structure of C Programming
The
format or way according to which a computer program is written is called the structure of the program. The program
structure of different programming language is different. C is a structured
programming language. It provides a well-defined way of writing programs.
The
basic structure of C program consists of the following main parts:
Preprocessor directives
ii) The main() function
iii) C statement
Preprocessor Directives
The commands that given to instruction to the
C-compiler are called preprocessor
directives. They are given at the beginning of the source code. The
preprocessor directives are also known as compiler
directives. A preprocessor directive begins with a # sign followed by a
word “include” or “define”. It does not end with semicolon (;).
The
preprocessor directives are processed before compiling the source code. The
part of the C-compiler that takes action on the preprocessor directives is
called preprocessor. For example,
“include” directive is used to include header file in the C program.
The
most important preprocessor directives are as following:
Include
directive
Define
directive
The ‘include’
The ‘include’ preprocessor directive is used
to include the specified header file in the program. It means that this
preprocessor directive tells the preprocessor to insert the definition of
standard function into C program from a standard library.
The
syntax of “include” directive to include standard header file is as follows:
#include<name
of header file>
For Example,
#include<stdio.h>
The ‘define’
The
define preprocessor directive is another important preprocessor directive. It
is used to define a constant known as constant
macro. A constant macro is an identifier, which is assigned a particular
constant micro. The value of identifier remains constant during the execution
of program.
The
general syntax of define directive is as follows:
#define
identifier expression
For Example,
#define
PI 3.1416
#define
city Sialkot
Header file
The
header file contain the declarations or information of standard library
function. These function are called in the main body of the program to perform
different tasks. The extension of header file is “.h”.
In
C-Language, there are many header files. Each header file contain information
for a specific group of function. For example, the header file “math.h” contain
declaration of mathematical function available in C.
A
header file must be included at the beginning of the program, if its function
are to be used in the program. A program may contain many header files.
The “include” preprocessor directive is used to
include the header file into the program. The name of header file is written
between angle brackets “< >” or in double quotes.
The
syntax to include a header file at the beginning of C program is as follows:
#include<name
of header file>
For example,
#include<stdio.h>
#include<conio.h>
In most of C programs, header file “stdio.h” is
used. The word “stdio” stand for Standard Input and Output. The scanf() and
printf() function is most commonly used input and output and “conio.h” header
file are use to “clrscr()”. It is also used to clear screen of monitor.
The main function
The main() function indicates the beginning
of the actual C-program. It is the point at which execution of program is
started. When a C program is executed, the execution control goes to directly
to the main() function.
Every
C program must have the main() function. The main() function comes after the
preprocessor directives.
The
general syntax of main() function is follows:
void
main(void){
Body
of main() function
}
The C Statement
A
program statement is a fundamental unit of any programming language. It is an
instruction for the computer to do something. The set of statement of C program
are written under the main() function between curly brackets i.e { }. In C program , the library function are
called for execution in the main body of the program. For example, the printf()
function is used to display a message or
output of the program.
Each
statement of C program is terminated with semicolon (;). It is called statement terminator. If semi colon (;)
is missing at the end of any statement , then the error message will be show by
the C-compiler during compiling process.
Statement
missing;
Syntax of C-program is used to clear the computer
screen and to display a message on the screen.
#include<stdio.h>
#include<conio.h>
main(){
clrscr();
printf(“welcome”);
}
Write a program to print your name
#include<stdio.h>
void main(){
printf(“My name is Zain”);
}
No comments:
Post a Comment
Thanks For Visiting Here...!! I will Reply you as soon as possible.