0% found this document useful (0 votes)
4 views

Chapter 3 structure of c program.doc

Uploaded by

anfalsuriya7333
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 3 structure of c program.doc

Uploaded by

anfalsuriya7333
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 3.

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.

An expression is a combination of variables, constants and operators written according to the


syntax of the language. Every expression results in some value of certain type that can be assigned to
a variable.

Operators are classified into number of categories, these are:


1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Assignment Operators
5) Increment and decrement Operators
6) Conditional Operators
7) Bitwise Operators
8) Special Operators

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.

Arithmetic Operators in C are

+ 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.

There are three logical 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.

The truth table for logical operator is

Value of exp1 Value of exp 2 Result


a) &&
True True True
True False False
False True False
False False False
b) ||
True True True
True False True
False True True
False False False
c) !
True False
False True

[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.

Syntax:- variable-name=constant or exp;


E.g., Count=0; pi=3.14; c=a+b; e=d;

Short hand assignment operator :-

The operator =’ is known as short hand assignment operator. This operator is written as :

Syntax: Variable name op= expression

Simple assignment op Short hand operator


N=n+1 N+=1
N=n-1 n-=1
N=n8(p+l) N*=p+1
N=n/(p+1) n/=p+1
N=n%p N%=p

Increment and Decrement Operators:-


The C language support increment and decrement operators which are ++, --, and unary
– (minus).

Increment and decrement operator are unary operator s, which add 1 and subtract 1 respectively.

These operators can written in two ways:

1} Prefix operator

2} Postfix operator

In prefix operator first increment/decrement by 1 to the operand is performed and then


the result is assigned to the variable e.g., ++n, --n.

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.

E.g., (a>b) ? a:b;

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 Operator Operator Meaning


Bitwise Logical
Operator
and Bitwise And
| Bitwise or
^ Bitwise Exclusive Or
Bitwise Shift
Operator
<< Shift Left
>> Shift Right

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.

Sizeof operators used to return the size in bytes of its operands.

Pointer operators ( & and *)

Member selection operators ( . and ->)

[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

Global Declaration Section

Main ( ) function 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

4) Global Declaration Section:-


There are some variables that are used in more than one function. Such variables are called
global variables and are declared in global declaration section that is outside of all the
functions. This section also declares all the user defines functions.

5) Main ( ) Function Section:-


Every c program must have one main function section. This section contains two parts
1) Declaration part
2) Executable part

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.

Following figure illustrate the compiling and execution of ‘C’ program

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.

C supports 3 classes of data types.

1) Primary (fundamental) data types


2) Derived data types
3) User-defined data types

[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.

Data types size in bytes Range

1. char (signed) 01 -128 to 127


2. unsigned char 01 0 to 255
3. int (short/signed) 02 -32768 to +32767
4. unsigned int 02 0 to 65535
5. long int (signed) 04 -2147483648 to +2147483648
6. long unsigned 04 0 to 4294967295
7. float 04 -3.4×10-38 to 3.4×1038
8. double 08 -1.7×10-308 to 1.7×10308
9. long double 10 -3.4×10-4932 to 1.1×104932

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).

Code Format code meaning

%c single character

%d Decimal integer

%h short integer

%i decimal, hexadecimal of octal integer

%ld unsigned decimal integer

%lu long integer

%l long unsigned integer

%e floating point value (exponential format)

%f floating point value (fixed point format)

%g floating point value (%f or %g)

%lf double value

%Lf long double value

%O octal integer

%s hexadecimal integer

%x or %X string of word (s)

%[…]

2. Derived data types :-


Derived data types are data types derived from primary data type. It is also known as
secondary data type.
Derived data types are as follows:
1> Array :- An Array is collections of similar element, stored in contiguous memory
locations. An array is declared by writing the data followed by name followed by size in
brackets.
E.x., int a[2]
2> Pointer :- A pointer must be defined so as to point particular data types.
E.g., int n, float *rate.
3> Structure :- A structure declaration consists of the keyword struct followed by an optional
structure name, followed by structure elements in Brace. The end of of structure must be
followed by semicolon.
E.g., sturct <identifier>
{

[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
};

3. User defined data types :-


1> Typedef :-
“C” supports features known as “type definition” that allows users to define an identifier
that would represent an existing data type.
Syntax :- typedef int units;
Here, units symbolizes int. they can be later used to declare variable as follows:
units batch1, batch2;

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.

Enum day { Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5,


Saturday=6, Sunday=7 };

[Type text]
PTO

You might also like