As previously mentioned
C is a structured programming language. So we want to learn the structure of C
to deal with C. When we write a program in We want to follow the following
structure otherwise the program may not work properly.
Documentation
Section
The Documentation section contains the
comments and remarks. The comments consist of set of comment lines giving the
name of the program or convey the logic of the steps of the programs. The
comments are non-executable part of program and it should be enclosed between
‘/*’ and ‘*/’. Or for single line comments we use ‘//’ at the beginning of the
line. The comments can appear anywhere in any size in a program. The comments
are very useful and make it as a habit when writing the program. It will be
useful to you and your followers latter. In our industry employee change occurs
frequently, so the new employee may not think like you. The comments you made
it may help him/her to complete the task.
Example:
/* This is a sample
program*/
Preprocessor
directives
The preprocessors is a
program that process the source code before it pass through the compiler
(Compiler is a program that convert high level language in to machine readable
form). The source code is examined by the preprocessor for any directive.
If any directives are present ,appropriate
action will taken. Then the source code is passed to the compiler. The
preprocessor directives begins with a ‘#’ symbol. And the preprocessor
directives may classified under three categories.
1. Macro Substitution (# define
directive)
It is used to define constants, formulas etc
Example:
#define pi 3.1416
(Replace all occurrence of pi with 3.1416)
2. File inclusion (# include
directive)
It
is used to include file contents.
Example:
#include<stdio.h>
(Inserts the entire contents of the file
stdio.h into the source code)
3. Compiler control directive (# if)
It
is used as a checking statement.
(checks
the expression after #if, if it is true all the statements between ‘{‘ and ‘}’
will be executed.)
Example:
#if x=10{y=20} # end if.
(if x=10 then the statement y=20 will be
executed)
Global
variable declaration
Global
variables are variables, which are declare at the beginning of the program.
That means outside of all functions even the main function. Its scope is
throughout the program and hence it can be accessible by all the part of the
program. These variables are available as long as the program alive.
Example
int x,y ;
main()
{
Statements;
}
(here x,y are two integer variables with
global scope).
The
main( ) function
Every C program must have one main( ) function. This
section contains two parts, declaration part and the execution part. The declaration
part declares all the variables used in the execution part.
These two part contain
with in the opening and the closing braces. The program execution begins at
opening brace and end at closing brace. The closing brace of main function is
the logical end of the program. All the statements in the declaration and execution part terminate with a semicolon.
And our C follows top to bottom execution approach. That is execution begins at
top of the program here opening brace of main function and end at bottom. That
is closing brace of main function.
Example:
[code]
#include<stdio.h>
Main()
{
int x;
x=10;
printf(“%d”,x);
}
[/code]
Header
files
There are some files
with extension ‘.h’. It is generally used with #include preprocessor directive.
These files are called header files. These files contain information that
assist the compiler in checking the code being compiled for syntax and other
errors.
They are of two forms
#include<filename>
#include “filename”
Angle brackets are used
for generic header files and the compiler look it in the environment space.
Double quotes are used for user defined header files. That is we can make
header files, which we can include a file name that is use full in all our
programs. It must be precompiled and error free.
The following are
commonly used header files.
Stdio.h
It contains the input output functions, data types, and
macros are defined in ‘stdio.h’. And its
expansion is like standard input output functions.
String.h
String functions
such as strcmp( ), strlen( ), strcpy( ) are defined in this header file.
Math.h
Mathematical functions are defined in this header file.
For more header files
refer the following links
Ok we can go through a
simple program with the above structure
[code]
/*A sample program */
#include<stdio.h>
main()
{
Pritf(“This is my first
program”);
}
[/code]
No comments:
Post a Comment