Lecture 2 – Elements of C Language
Control Structures
Control
Structures are the structures which control the flow of the program in any
programming language. There are three basic control structures in any
programming language.
- Sequence or Statements
- Selection or Decisions
- Iteration or Loops
Sequence or Statements
These
structures are responsible for the linear flow of the program. Following are
the types of Sequence Structures
- Input – these statements are responsible for taking input from the user. For example scanf, gets, getch, getche etc.
- Output – these statements are responsible for giving output to the user. For example printf, puts etc.
- Expressions – these statements constitutes of program logic. For example y = mx +c; or c= a+ b; etc.
Selection or Decisions
There
are some situations where we have to take decision based upon some condition.
These structures are used to transfer the control of your program based upon a
condition. For example if you want to go to your home and you stuck in a
traffic jam, you opt for an alternate route. Similarly we make decisions based
upon a certain condition. Examples of decisions are if, if-else, if-else-if and
switch. They will be discussed later.
Iterations or Loops
There
are situations where we need to repeat some statements. These structures help
us to achieve that. There are two types of loops
1.
Counter
controlled loops – We have only one loop in this category i.e. the
‘for’ loop. This loop repeats a statement or a block of statements for the
specified number of times.
2.
Sentinel
controlled loops – These loops repeat a statement of a block of
statements based upon a condition. We have two loops in this category while,
and do-while loop.
Now! I shall discuss the first control structure, i.e.
statements
Figure
1 - Basic Structure of a C Program
Elements of a C Program
Preprocessor Directive
It is the instruction for the compiler. It
tells the compiler to do it first. Preprocessor directive starts with a # symbol followed by a keyword like
include, define and if etc. The keyword
which is followed by # symbol is NOT
necessarily a keyword but it is a part of the preprocessor directive.
Header File
It
is the file which contains the definition of the most functions. These header
files are pre-programmed and are shipped with the compiler itself. Dot H is the
extension for the header file. The header file stdio.h contains the definitions of the functions like printf and scanf. (Functions will be discussed later)
Include Folder
It is
specified by the angle brackets <>.
If
the header file name is specified between these angle brackets (e.g. #include
<stdio.h> )then the linker will search the file from the include
folder which is specified in the IDE settings. The default location of Include folder is C:\TC\INCLUDE it may be
changed if required from the IDE options.
If
the header file is included using inverted commas (e.g. #include “stdio.h” ) then
the linker would search the file in the CURRENT WORKING DIRECTORY, normally C:\TC\BIN
Return Type
It is
the type of a variable which is returned when the function’s body ends. For
example, if I say “Dear Student! Bring me
a glass of water”. Now let us breakup this statement so that we would be in
a better position to understand what is a function,
return type and parameter. (Return types will be discussed with functions)
·
Bring me – it is analogous to a function call
·
A glass of water – it is analogous to a
parameter passed
Main function
It
is the entry point to a C program. In most languages there’s a main function to
start with, like C, C++, Java, and Visual C++. If there’s no main function
there’s no C program. The compiler tries to locate the main function. If there
is more than one function in a C program then this main function stands out
from the rest, telling the compiler to start from here (i.e. main).
Comments
Post a Comment