Chapter 3 structure of c program.doc
Chapter 3 structure of c program.doc
C Tokens
Operator:-
The operator is a symbol that performs arithmetic or other operations or is used for logical
comparisons, etc. These operators operate on integer, character and real numbers. Operators are used
in program to manipulate data and variable.
Arithmetic Operator :-
For the performing the basic mathematical calculation the equation must be converted in C by
using the equivalent arithmetic operators and library functions.
+ Addition
- Subtraction
* Multiplication
/ Division
% modulus
The hierarchy of operation of the modulus (%), multiplication (*) and Division (/) are
performed first and then subtraction and addition are performed
[Type text]
PTO
Relational Operator:-
These operators compare two values and results in one of two possible outcomes, True or
false. C Language Supports six relational operators as follows:-
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater
>= Is greater than or equal to
== Is equal to
!= Is not equal to
When arithmetic expressions are used on either side of relational operator, the arithmetic
expression will be evaluated first and then result is compared.
Logical operator :-
These operators produce true or false result after comparing two or more expression that uses
the Arithmetic or Relational operators.
Operator Meaning
Logical && Logical and operator && and || are
used || Logical or when we to test more than
one ! Logical not condition and make
Division.
[Type text]
PTO
Assignment Operator :-
The Assignment operator is simple equal sign (=) this operator is used to assign the
result of an expression or constant to a variable.
The operator =’ is known as short hand assignment operator. This operator is written as :
Increment and decrement operator are unary operator s, which add 1 and subtract 1 respectively.
1} Prefix operator
2} Postfix operator
While in postfix operator first assign the value to the variable then increment or decrement by 1 to
the operand is perform. E.g., n++, n--.
Conditional operator :-
This is ternary operator, this operator (? and :) compare two value and evaluate the single
value after comparing two expressions, the expressions used Arithmetic or Logical Operators.
[Type text]
PTO
Syntax: expn1? expn2: expn3
If expression 1 evaluate to true, then expn2 is selected for execution otherwise expn 3 is
selected for execution.
Bitwise Operator:-
These operators operates on each bit of data, these operators are used for testing,
complementing or shifting bits to the right or left.
Bit level manipulation are used in setting a particular bit or graph of bits to 1 or 0 which
helps to perform numerical computation fast. These operators permit to access a single bit from data.
Bitwise operators can operate upon integer and character but not on float and double.
Bitwise Complement
operator
~ Ones complement
Special Operators:
Special operators are common operators used to make an expression
by putting several expressions inside a set of parenthesis.
[Type text]
PTO
Structure of C Program
C program is group of building blocks called functions. A function is a subroutine that may include
one or more statements designed to perform a specific Task.
The basic structure of c program can be best explained by using following figure.
Documentation Section
Link Section
Definition Section
Declaration part
Executable part
Subprogram Section
Function 1
Function 2
Function
1) Documentation section:-
Documentation section consists of a set of comment lines giving the name of the program, the
author and other details which programmer would like to use later.
2) Link Section:-
The Link section provides instructions to the compiler to link functions from the system
library.
3) Definition Section :-
The definition section defines all symbolic constants
[Type text]
PTO
i.e. #define MAX 30
The declaration part contains all the variables used in executable part. There is at least one
statement in the executable part. These two parts must appear between opening and closing
braces. The program execution begins at opening braces and ends at closing brace. The
closing brace of main () is the logical end of the program
6) Subprogram Sections :-
This section contains all the user-defined functions that are called in main( ) functions. This
section is generally placed after the main( ) section although they may appear in any order.
Execution of ‘C’ Program
This execution process of ‘C’ program involves series of steps as follows :-
1. Creating the program.
2. Compiling the program.
3. Linking the program with functions that are needed from ‘C’ Library.
Data Types
C language is rich in its data types. Storage representation and machine instructions to handle
constants differ from machine to machine. The variety of data types available allows the programmer
to select the type appropriate to the need of application as well as the machine.
[Type text]
PTO
1. Primary data types :-
All C compilers support five fundamentals data types.
[Type text]
PTO
Size of the data types is the space in memory in terms of bytes given by computer. The range
of the values represented by any data types is directly proportional to the size of that data
types.
Format specifier :-
[Type text]
PTO
These are the special character codes starting with % sign which specifies the format
of the value to be read (scanf) or (printf).
%c single character
%d Decimal integer
%h short integer
%O octal integer
%s hexadecimal integer
%[…]
[Type text]
PTO
……..
……………
};
4> Union :- Union is syntactically identical to structure. An element of the union is accessed
in same way as member of structure. A union provides a way to look at the same memory
location’s contents in more than one way.
5> Enum :- An enumerated data type is the one which has user specified one values.
E.g., enum city
{
Aurangabad, pune, Nagpur, Kolhapur
};
2> Enum :-
Another user-defined data type is enumerated data type provided by ANSI standard. It is
defined as
enum identifier
{ value1, value2, valueN };
e.g., enum day
{ Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
enum day d1,d2;
The compiler automatically assigns integer digits beginning with 0 to all the enumeration
constants.
i.e., Monday will have value 0
Tuesday will have value 1.
.
.
Sunday will have value 7.
However the automatic assignments can be overridden by assigning values explicitly to
the enumeration constant.
[Type text]
PTO