You are on page 1of 26

PROBLEM SOLVING TECHNICS AND C PROGRAMMING

REFERENCE BOOKS
Programming with C
By, Byron S. Gottfried
C programming language
By, W. Kennigham, Dennis M. Ritchie
C programming a modern approach
By, K.N. King
C programming in 12 easy lessons
By, Greg Perry
Compiler construction
By, Aho and hulman

Programming language is classified into:


Machine language
Assembly language
High level language
Programming Language Definition:Primary interface between the programmer and
computer is known as programming language.
A program is a ordered list of instructions that when executed causes the computer to
behave in a pre determined manner.
A programming language includes series of commands which are used in
development of software.

Machine language:

First generation language.


Machine language uses 0s and 1s to represent data and instruction written in this
language.
Native language of computer

A British mathematician Alan Mathisan Turing was the first person to recognize that
programming in machine language is a less time consuming.
Machine language instruction form:
Op code

Operand

(operation code)

(memory
location)

The op code is command or operations which tell the computer what functions are to
be performed.
Operand which tells the computer where to find or store the data on which the desired
operation is to be performed.

Advantages of machine language:

Translation is free.

Less time consuming.

High speed.

Disadvantages of machine language:

Machine dependent

Complex language

Error prompt means difficult to find errors

Tedious means modifying and correcting the errors

Assembly language:

Second generation language.

Developed in 1950.its main developers is IBM.

Requires assembler which translates assembly language to machine language.

Functions of assembler:

In case of large assembly programs it also provides linking facility among the
subroutines.

It assembles all the instructions in the main memory for execution.

Advantages of assembly language:

Easy to understand and use:

Assembly language uses mnemonics instead of using numerical opcodes and memory
locations used in machine language. Hence the programs written in assembly language are
much more to understand and used as compare to machine language counterpart. Assembly
languages are easy to modify.

Less error prompt:

Mnemonic codes and symbolic addresses are used. The programmer did not have to
keep storage locations of information and instruction. Hence there are borne to be less error
while writing assembly language program.

Efficiency:

Assembly programs can run much faster and use less memory and other resources
than similar programs.

Disadvantages of assembly language:

Machine dependent:

Different computer architectures have their own machines and assembly languages
which means program written in this language not portable to incompatible system. This
makes it a low level language.

Less efficient

No standardisation:

Assembly language cannot be standardised because each type of computer have a


different instruction set and therefore have a different assembly language.

High level language:

Third generation language.

Machine independent.

Compiler / interpreter are used as translator.

System development programs:


System software is associated with the development of computer programs.
These program development tools allow the programmers to write and construct
programs that the operating system can execute.
System development programs are classified into:
Programming language
Language translators
Linker
Loader

Linker:
A typical software generally comprises 100s, 1000s or even millions of lines of
programming statements or code. The code is divided into logical groups and store in
different independent models. So that the debugging and maintenance of code becomes
easier. Before execution, different object codes resulting from the independent models have
to be linked together to create executable program. This job is performed by a tool known as
linker. A linker is an system software that links together several object models and libraries to
form a single, coherent program.

Loader:
It is a kind of system software which is responsible for loading and relocation of the
executable program in the main memory. The functions of loader includes assigning load
time, space for storage i.e. storage allocation and to assist a program to execute appropriate.
4

Structure of C program:
Pre-processor statements
Global declaration
main function
{
Declaration

//nature of the variable / constraint is stated But no storage is allocated.

Definition

//assigned storage for the variable.

Natural language:
It is a language which arises in an unpremeditated fashion. These languages possessed
by the human intellect. A natural language is typically used for communication and may be
spoken or written.

Formal language:
Formal languages are languages that are designed for specific application.
Ex: A notation that mathematician use a formal language i.e. particularly at denoting
relationship among numbers and symbols.
Ex: Chemist uses formal language to represent chemical structure of molecules
Programming languages are formal language that has been designed to express competitions.

Programming language:
Programming languages for computers are developed with the primary objective of
facilitating a large number of persons to use computers without the need to know in detail the
internal structure of a computer.
High level language is classified into:
Structured programming
Procedural oriented programming language
Object oriented programming language

Structured programming:
It is also known as modular programming is a subset of procedural programming that
enforces a logical structure on the program being written to make it more efficient and easier
to understand and modify. Structured programming employs top-down design model in
which developer map the overall program structure into separate sub sections. A defined
function or set of similar functions is coded in a separate model or sub model i.e. a code can
be coded into memory more efficiently and those models can be reused in other programs.
5

After a model has been tested individually it is then integrated with other models into the
overall program structure. Structures programming was first generated by Corrado Bohm and
Giuseppe Jacobin.
Ex: Languages such as Pascal, ADA

Procedural oriented programming language:


Procedural programming can also be called imperative programming i.e. specifying
the steps. The program must take to reach the derived state. These are derived from structured
programming based upon the concept of the procedural call. Procedures also known as
routines, subroutines, methods or functions. These languages contain a series of
computational steps to be carried out. Any given procedure might be called at any point
during programs execution including by other procedures or it-self.

Object oriented programming language:


The object oriented programming was first developed in 1960s and implemented in
languages such as SIMULA-67. In object oriented programming language model organised
around object rather than data and logic. The data structures become an object that includes
both data and functions. In addition programmers can create relationship between one object
to another object. Object oriented programming is a method of implementation in which
programs are organised as co-operative collection of objects. Each of which represents an
instance of some class and classes are all members of a hierarchy of a classes united via
inheritance relationships. Object oriented programming consists of instantiating a number of
objects which communicate with one another so as to achieve some desired behaviour.

Object oriented programming concepts:

Objects

Classes

Data abstraction and encapsulation

Inheritance

Polymorphism

Dynamic binding

Message passing

Attribute grammar:
An attribute grammar is a context pre grammar that has been extended to provide
context sensitive information by appending attributes to some of its non terminal. Each
distinct symbol in the grammar has associated with it a finite, possibly empty set of attributes.
Each attributes has a domain of possible values. An attribute may be assigned values from its
domain during parsing. Attributes can be evaluated in assignments or conditions.

Two classes of attributes:


1. Synthesized attribute:
An attribute that gets its value from the attributes attached to the childrens of its non
terminals.
2. Inherited attributes:
An attribute that gets its value from the attributes attached to the parents(siblings) of
its non terminals.

Programming language syntax:


The syntax of programming language describes structure of program without
consideration of their meaning.
Criteria of the syntax of programming:
Readability:
Program is considered readable if an algorithm and data are apparent by instructions.
Writ ability:
Easy for writing the program.
Verifiability:
Ability to prove the correctness.
Translability:
Easy of translating the program into executable form.
Lack of ambiguity:
The syntax should provide for easy of avoiding ambiguous structures.

Basic syntactic concepts:


1. Character set:
Character is the alphabet of language. Several different character sets are used those
are ASCII, EBCDIC and Unicode.

ASCII:
American Standard Code For Information Interchange. It is a form of
character encoding i.e. based on the English alphabet. ASCII code represent
the text in computers and communication tools that handles the text.

EBCDIC:

Extended Binary Coded Decimal Interchange. Developed by IBM and


coding method used by other computers to present letter. No other symbols in
binary language computer can understand.

Unicode:
Unicode standard is the universal character encoding standard used for
representation of texts for computer processing. Unicode provides a consistent
way of encoding multilingual plain text making it easier to exchange text files
internationally.

2. Identifiers:
Strings of letters or digits usually beginning with a letter.

Operator symbols: +, -, *, /

Keywords/reserve words:
Used for fixed part of the syntax of the statement.

Noise words:
Optional words inserted into statements to improve readability.

Comments:
Comments are used to improve readability and for documentation purposes.
Comments are usually enclosed by special markers.

Blanks:
Rules vary from language to language usually significant in literal strings.

Delimiters:
Used to denote the beginning and end of syntactic constraints.

Expressions:
Functions that access data objects in a program and return a value.

Statements:
These are the sentences of the language they describe a task to be performed.

Stages in translation analysis of the source program:


It constraints the following 3 steps:
1. Analysis of the source program
2. synthesis of the object program
3. Bootstrapping
8

1. Analysis of the source program:


The analysis part break ups the source program into constant piece and creates an
intermediate representation of the source program. It contains the following steps:
Lexical analysis:
It is also called as a scanner. Identifying the tokens of the programming language or it
is a program that brooms sequence of characters into lexemes and output to the syntax
analysis.
Tokens are symbolic names for the entities that make up the text of the program.
Ex: void main ( )
{
Print f (welcome to M.sc 1st semester);
}
Here the tokens are void, main, ( ) { print f,( ), ; welcome to M.sc 1st semester }
Syntactic and Semantic analysis:
It is also called as parsing determining structure of program as define by the language
grammar or a parsing is program that groups the sequence of tokens from lexical analysis
phase into phrases each with an associated phrase type. A phrase is logical unit with respect
to the rules of source language.
Semantic analysis:
Assigning meaning to the syntactic analysis. The semantic analysis builds the bridge
between analysis and synthesis . Basic semantic tasks are
a. Symbol table maintenance.
b. Insertion of implicit information.
c. Error detection
d. Macro processing

2. Synthesis of the object program:


The synthesis part constructs the desired target programming from
intermediate representation.
It includes three main steps:
1. Optimization
2. Code generation

3. Linking and loading


Optimization:
Removing redundant statements or an optimizer attempts to improves the time and space
requirement of the program. There are many ways in which code can be optimized but most
are expensive in terms of time and space to implement.
Common optimization includes:

Removing redundant identifiers.

Removing unreachable sections of the code.

Identifying common sub expression.

Unfolding loops and eliminating procedures.

Intermediate code generation:


A source program has been decomposed into a symbol table and parse tree. Both of
which may have been modified by the semantic analyzer. From this information the process
of generating objects code according to either of two approaches.
a. Generate code for specific machine.
b. Generate code for general or abstract machine. Then use further translators to turn the
abstract code into for specific machines.
Code generation:
The final phase is to generate code for specific machine. In this phase we consider
a. Memory management
b. Register assignment
c. Machine specific optimization
The output from this phase is usually assembly language or re locatable machine
code.

Linking and loading:


Linking:
The linker combines object code produced from compiling and assembling many
source programs as well as standard library functions and resources supplied by the operating
system. This involves resolving reference in each object file to external variables and
procedure declared in other files.
Loaders:
Compilers, assemblers and linkers usually produce a code whose memory references are
made relative to an undetermined starting location that can be anywhere in the memory. A
loader calculates appropriate absolute address for these memory location and aments the code
10

to use these addresses.

3. Bootstrapping:
It is a process of starting up a computer. It also refers to the program that initializes
the operating system during startup.

Functional Programming:
It is a collection of mathematical functions each with an input and a result functions
interact and combine with each other using functional composition, conditionals and
recursion. Major functional programming languages are LISP, ML, Scheme and Haskell.

Logical programming (Declarative):


It is a collection of logical declaration about what an outcome function should
accomplish rather than how that outcome should be accomplished. Execution of program
applies these declarations to achieve a series of possible solutions to a problem.
Applications of logic programming have 2 major designs:
1. Database design
2. Artificial intelligence

Database Design:
SQL has been a dominant declarative language while in artificial intelligence prolog has
been very influential. The new features of logic programs that these application domains
exploit or non determination and backtracking.

A non deterministic logic program may find several solutions to a problem rather than
just one has would be harm in the other programming domains.

Backtracking mechanism which enables non determinism is built into prolog


interpreter and it is available to all prolog programs.

BNF:
Backus Naur Form is a formal mathematical way to describe a language (To describe the
syntax of the programming language). The BNF is a way of defining syntax.
It consists of:
a. Set of terminal symbols.
b. Set of non terminal symbols.
c. Set of production rules of the form.

Terminals:
Symbols of the alphabet of the language being defined are called as terminals.

Non terminals:
11

A finite set of other symbols each of which represent a language. Production has the
form of variables, string of variables and terminals.

EBNF:
Extended Backus Naur Form is a few simple extensions to BNF which makes
expressing grammar more convenient thus it is more concise then BNF. EBNF is no more
powerful then BNF that is anything can be expressed in EBNF can also be expressed in BNF.
It is derived from regular expression syntax. The extensions of EBNF are as follows:
1. '*'(Klein star): 0 or more occurrence.
2. '+'(Klein cross): 1 or more occurrence.
3. '?': 0 or 1 occurrences (sum times [ ...] used instead)

Parse tree:
A parse tree is the graphical representation of the grammar rules used to derive a
string. Parse trees have the advantage that they make explicit the hierarchical structure of the
strings.
A parse tree for a grammar G is a tree where

the root is the start symbol for G


the interior nodes are the nonterminals of G
the leaf nodes are the terminal symbols of G.
the children of a node T (from left to right) correspond to the symbols on the right
hand side of some production for T in G.

Every terminal string generated by a grammar has a corresponding parse tree; every valid
parse tree represents a string generated by the grammar (called the yield of the parse tree).
Example: Given the following grammar, find a parse tree for the string 1 + 2 * 3:
1.
2.
3.
4.
5.
6.
7.

<E> --> <D>


<E> --> ( <E> )
<E> --> <E> + <E>
<E> --> <E> - <E>
<E> --> <E> * <E>
<E> --> <E> / <E>
<D> --> 0 | 1 | 2 | ... 9

The parse tree is:


E --> E --> N --> 1
+
E --> E --> N --> 2
*
E --> N --> 3

Ambiguity:
12

A grammar is ambiguous if the language it defines contains at least one string that has
two or more possible derivations which corresponds to different parse trees.
A grammar for which there are two different parse trees for the same terminal string is said to
be ambiguous.
The grammar for balanced parentheses given is an example of an ambiguous grammar:
P --> ( P ) | P P | epsilon
We can prove this grammar is ambiguous by demonstrating two parse trees for the same
terminal string.
Here are two parse trees for the empty string:
P --> P --> epsilon
P --> epsilon
P --> epsilon

Here are two parse trees for ():


P --> P --> (
P --> epsilon
)
P --> epsilon
P --> P --> epsilon
P --> (
P --> epsilon
)
While in general it may be difficult to prove a grammar is ambiguous, the demonstration of
two distinct parse trees for the same terminal string is sufficient proof that a grammar is
ambiguous.
An unambiguous grammar for the same language (that is, the set of strings consisting of
balanced parentheses) is:
P --> ( P ) P | epsilon
(Refer url for more details : http://www.cs.rochester.edu/~nelson/courses/csc_173/grammars/parsetrees.html)

Tokens: Tokens are symbols, operators, special characters and identifiers.


Tokens are individual words and punctuation marks in passage of text. In C, program the
smallest individual units are known as C Tokens. C has Six types of Tokens. The Tokens
are shown in figure.

13

A C program must be a syntactically valid sequence of characters of the language. In every C


program, the most basic element recognized by the compiler is a single character or group of
characters called C token. The compiler first groups the characters into tokens. A token is an
atomic unit of source program, i.e., the compiler will not break down the token any
further. For example, the words main, brace, parenthesis all are tokens of C program. The
tokens comprise the basic elements of the language.
Variables: A variable is a name given to the memory location.
EX: For-is a valid variable
for-is a invalid variable.
A variable is just a named area of storage that can hold a single value (numeric or character). The C language
demands that you declare the name of each variable that you are going to use and its type before actually try to do
anything with it.
The Programming language C has two main variable types

Local Variables
Global Variables

Local Variables

Local variables scope is confined within the block or function where it is defined. Local variables must always
be defined at the top of a block.

When a local variable is defined - it is not initalised by the system, you must initalise it yourself.
When execution of the block starts the variable is available, and when the block ends the variable 'dies'.

Check following example's output

main()
{
int i=4;
int j=10;
14

i++;
if (j > 0)
{
/* i defined in 'main' can be seen */
printf("i is %d\n",i);
}
if (j > 0)
{
/* 'i' is defined and so local to this block */
int i=100;
printf("i is %d\n",i);
}/* 'i' (value 100) dies here */
printf("i is %d\n",i); /* 'i' (value 5) is now visable.*/
}
This will generate following output
i is 5
i is 100
i is 5
Here ++ is called incremental operator and it increase the value of any integer variable by 1. Thus i++ is equivalent
to i = i + 1;
You will see -- operator also which is called decremental operator and it idecrease the value of any integer variable by
1. Thus i-- is equivalent to i = i - 1;

Global Variables

Global variable is defined at the top of the program file and it can be visible and modified by
any function that may reference it. Global variables are initalised automatically by the system
when you define them!
Data Type
int
char
float
pointer

Initialser
0
'\0'
0
NULL

If same variable name is being used for global and local variable then local variable takes
preference in its scope. But it is not a good practice to use global variables and local variables
with the same name.
int i=4;

/* Global definition */

main()
{
i++;
/* Global variable */
func();
printf( "Value of i = %d -- main function\n", i );
15

}
func()
{
int i=10;
i++;

/* Local definition */
/* Local variable */

printf( "Value of i = %d -- func() function\n", i );


}

This will produce following result


Value of i = 11 -- func() function
Value of i = 5 -- main function
i in main function is global and will be incremented to 5. i in func is internal and will be
incremented to 11. When control returns to main the internal variable will die and and any
reference to i will be to the global.

Data Type: The type of variable that can be stored in memory location is called data type.
In the C programming language, data types refer to an extensive system used for declaring variables or functions
of different types. The type of a variable determines how much space it occupies in storage and how the bit
pattern stored is interpreted.
The types in C can be classified as follows:

S.N. Types and Description


1

Basic Types:
They are arithmetic types and consists of the two types: (a) integer types (b) floatingpoint and (c) char types.

Enumerated types:
They are again arithmetic types and they are used to define variables that can only be
assigned certain discrete integer values throughout the program.

The type void:


The type specifier void indicates that no value is available.

Derived types:
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and
(e) Function types.

The array types and structure types are referred to collectively as the aggregate types. The type of a function specifies
the type of the function's return value.

Integer Types
Following table gives you details about standard integer types with its storage sizes and value ranges:

16

Type

Storage size

Value range

char

1 byte

-128 to 127 or 0 to 255

unsigned char

1 byte

0 to 255

signed char

1 byte

-128 to 127

int

2 or 4 bytes

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

unsigned int

2 or 4 bytes

0 to 65,535 or 0 to 4,294,967,295

short

2 bytes

-32,768 to 32,767

unsigned short

2 bytes

0 to 65,535

long

4 bytes

-2,147,483,648 to 2,147,483,647

unsigned long

4 bytes

0 to 4,294,967,295

To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The
expressions sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of
int type on any machine:

Integer:
For 16-bit machine (2 bytes)
Range of unsigned integer 0 to 2^61-1
Or 0 to 65535
Range of signed integer -2^15 to 2^15-1
Or -32768 to 32767
For 32-machine (4 bytes)
Range of unsigned integer 0 to 2^32-1
Or 0 to 4294967295
Range of signed integer -2^31 to -2^31-1
Or
2147483648 to 2147483647

#include <stdio.h>
#include <limits.h>
int main()
{
printf("Storage size for int : %d \n", sizeof(int));
return 0;
}
When you compile and execute the above program it produces the following result on Linux:
Storage size for int : 4
(try with windows (any version) operating system)

Advantages:
It will have exact quantities which will be used in counting and indexing.

Disadvantages:
i.

We cant give higher number.


17

ii.

We cannot use decimal values.

Floating-Point Types
Following table gives you details about standard floating-point types with storage sizes and value ranges and their
precision:
Type

Storage size

Value range

Precision

float

4 byte

1.2E-38 to 3.4E+38

6 decimal places

double

8 byte

2.3E-308 to 1.7E+308

15 decimal places

long double

10 byte

3.4E-4932 to 1.1E+4932

19 decimal places

The header file float.h defines macros that allow you to use these values and other details about the binary
representation of real numbers in your programs. Following example will print storage space taken by a float type and
its range values:

#include <stdio.h>
#include <float.h>
int main()
{
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );
return 0;
}
When you compile and execute the above program, it produces the following result on Linux:
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6

Float:
For 16 bit machine (4 bytes)
3.4E-38 to 3.4E38
For 32 bit machine (8 bytes)
1.7E-3082 to 1.7E308
8 digits are printed after the decimal point.
Double:
The precision of double is more than float.
16 digits are printed after the decimal point.
For 16 bit machine (8 bytes)
1.7E-308 to 1.7E308
For 32 bit machine (16 bytes)
3.4E-4932 to 1.1E4912

Char:
18

In memory location it is stored with its ASCII value.For both 16-bit and 32-bit machine it is 1
byte.
0 to 2^8-1 Unsigned character
-2^7 to 2^7-1 Signed character

The void Type


The void type specifies that no value is available. It is used in three kinds of situations:
S.N. Types and Description
1

Function returns as void


There are various functions in C which do not return value or you can say they return void. A
function with no return value has the return type as void. For example void exit (int status);

Function arguments as void


There are various functions in C which do not accept any parameter. A function with no
parameter can accept as a void. For example, int rand(void);

Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example a
memory allocation function void *malloc( size_t size ); returns a pointer to void which can be
casted to any data type.

Lengths of exponents and mantissa:


Type

Exponential
length

Mantissa

Float

8 bits

23bits

Double

11 bits

52 bits

Derived Data Types


These data types are derived from the basic data types. Derived data types available in C are:
1. Array type

e.g. char[ ],int[ ], etc.

2. Pointer type

e.g. char*, int*, etc.

3. Function type

e.g.int(int,int), float(int), etc.

User-defined Data Types


The C language provides flexibility to the user to create new data types. These newly created
data types are called user-defined data types. The user-defined data types in C can be
created by using:
1. Structure
2. Union
3. Enumeration

19

Data modifiers:
Additional qualifiers or modifiers can be applied to basic data typed to get additional ranges
called data modifiers. Basic data types except void can be modified using a series of type modifiers to
fit the requirement of a particular program more closely to have a accuracy for a real numbers. We are
using double data type. Similarly for integer data accuracy can be obtained by some data modifiers.
These modifiers are also called qualifiers. They are signed, unsigned, short and long.

Signed:
This can be applied to integer variable. The default integer declaration assumes a signed
number. The signed modifier can also be applied with a character data type, to create a small integer
in some implications char is unsigned means it can hold integers 0 to 255. If it is specified as signed
then it can hold numbers in the range -128 to -127.

Unsigned:
This can be used for both integer and character. It can also be used in combination with long
or short. It is used to create an unsigned integer. A higher order bit of a signed integer is used as a
signed flag. If the higher order bit is 0 then the integer is treated positive and if high order bit is 1 then
integer is treated negative(negative numbers are represented using 2s complement notation).

Long:
This can be applied to the integer data type. When applied to int it doubles the length (in bits)
of the basic type. If an integer is of 16 bits, then long int is 32 bits. Similarly this can be used with
double. When long is applied to double it doubles the precision.

Short:
This makes the size of an int half but most compilers have 16 bit int and ANSIC requirement
is that the smallest acceptable size for int 16 bits. Most of the systems provide size of a short int as
that of int.

Basic structure of C program


Preprocessor statements
Global declaration
main
{
Variable declaration
Stuct 1;
.
.
20

.
Stuct n;
}
User defined functions

Preprocessor:
Before the program compiled is known as preprocessor statements. Preprocessor
statements are processed before the source program is handed over to the compiler. They are
placed in a source program before the function main. The various preprocessor directories
are:
1) Symbolic Names:
An identifier is replaced by sequence of characters which may represent a
constant or string. These identifiers (also called symbolic names) are associated with
constants and hence they are called symbolic constant. The syntax of macro definition
for symbolic name is
#define Name String
Ex: #define PI 3.142
This instructs the compiler that whenever PI is used in the program, it is
replaced by the constant 3.142.
2) Macro Substitution:
The symbolic names can also be used to define the macros.
Ex: consider the macro definition as shown below
#define square(x) (x*x)
Whenever square appears in the program with the x as a parameter it is replaced by
(x) (x*x)
#include<stdio.h>
#include<conio.h>
Void main()
{
int m=10,n=5;
printf(5 square is =%d\n,square(n));

21

printf(10 square is=%d\n,square(m));


printf(10+5 square is =%d\n,square(m+n));
getch();
}

Output:
5 square is=25
10 square is=100
10+5 square is =65

The two answers are correct and the square of 10+5 is 65 which is wrong. The correct answer is 225.
Note that when macro SUARE (m+n) is called the actual replacement will be (10+5*10+5). To get
the correct square the actual definition can be modified by placing the brackets as shown below
#define square((x)*(x))
By doing these changes to the macro definition if we execute the above program the output is
5 square is=25
10 square is=100
10+5 square is =225
3) File inclusion:
It is a process using which the external files containing functions and definitions are included in C
programming. This avoids the rewriting of functions and macros. Any external file can be include
#include directory.
This syntax is #include file name
EX: #include<fact.c)
Ex:

#include<stdio.h>
The above information instructs the compiler to include an entire file into the user program.

Ex: let us write a small program to find square of number with the file inclusion. Type the
following program; assume the name of the file square.c. This file contains only one statement as
shown below
#define SQUARE((x)+(x))
Type the following program by including the previous file square.c as shown below
#include<stdio.h>

22

#include square.c
Void main()
{
int m=10,n=5;
printf(5 square is =%d\n,SQUARE(n));
printf(10 square is=%d\n,SQUARE(m));
printf(10+5 square is =%d\n,SQUARE(m+n));
getch();
}
4) Conditional Compilation:
Enables the coder to control the execution of preprocessor directives and the
compilation of the program code. Conditional preprocessor directives evaluate constant
integer expression. The one that cannot be evaluated in the preprocessor directives are size of
expressions and Enumeration constants.

Global declaration:
These variables and functions are declared before the main program. These external variables
are recognized globally. They can be accessed from any function that falls within their scope.

Main ():
As the name itself indicates this is main function of every C program. Execution of C
program starts from main (). No program is executed without main () function. Main is collective
name given to a set of statements. All statements belonging to main are enclosed within braces. The
main function does not terminate with a semicolon. Their must be one and only one main function in
every C program.

Braces:
Every C program uses a pair of curly braces {}. The left brace indicates the beginning of a
main on the other hand the right side brace indicated the end of the main function. The braces can also
be used to indicate beginning and end of the user defined functions. Also the compound statements.

Statements:
Statement is any executable code. It must always end with a semicolon in C program. We can
write any number of statements in a single line and semicolon is a delimiter and there is no limit out
the statements and statement can be expression or built in functions which are used in C program.
EX: c=a+b;

User defined functions:


23

The name itself indicates these functions are defined by the user or the programmer
depending on the user application. Sometimes these user defined functions are declared in the variable
declaration part. In single c program any number of user defined function can be used.

Operators:
1) An operator is a symbol or token that specifies the operation to be performed.
EX: + - / *
C language includes large number of operators. These operators can be classified based on the number
of operands an operator has.
2) Type of operation being performed.
Classification based on number of operands. Mathematical expression consist of number and variables
joined by operators such as + - * / to perform meaningful computations.
EX: a+b; c%d;
The operators are classified into 4 major categories based on the number of operands as given below

Unary operator:
An operator which adds on only one operand to produce the result is called unary
operator.
EX: *a, -9, &c are all expressions with unary operators.

Binary operator:
An operator which adds on two operands to produce the result is called binary
operator.
EX: c+d, d%c, x/y are all expressions with binary operators.

Ternary operator:
An operator which adds on 3 operands to produce a result is called ternary operator.
Ternary operator is also called conditional operators.
Ex: a?b:c

Special operator:
The special operators used in c program are size of, address operators and comma.

Classification based on the type of operation:


In c more number of operators instead of classifying the operators based on the number of operands
on which the operator adds. It is better to classify them based on the type of operation being
performed. The classification is given below

Arithmetic
Assignment
Increment/decrement
Relational
24

Logical
Condition
Bitwise
Special

Arithmetic operators:
The operators that are used to perform arithmetic operations such as + - * / are called
arithmetic operators. This operator is classified into 2 types based on number of operators
I.

Arithmetic binary operators:


Arithmetic operators involving 2 operations are called Arithmetic binary operators.

II.

Arithmetic unary operators:


Arithmetic operators involving only one operand are called Arithmetic unary
operators. Arithmetic unary operators is classified into unary plus and unary minus.

Arithmetic expressions:
An expression consisting of constants and variables along with arithmetic operators such as +
- * / to perform meaningful computation is called arithmetic expression.
Ex: (9+2+8)
EX: ((c+d)*(a-b))/d

Conversion of expressions:
The expressions which are used in mathematics cannot be directly used in C program. They
have to be converted suitably into C equivalent expressions. The below example shows mathematical
expressions and their equivalent C expressions.
Mathematical expressions

C equivalent expressions

ax^2+bx+c

A*(x*x)+(b*x)+c

A=\/s(s-a)(s-b)(s-c)

A=sqrt(s(s-a)(s-b)(s-c))

Type conversion:
In C language we can convert the data from one data type to another data type. Sometimes the
compiler itself converts the data from one data type to another data type. This type of processing one
data type to another data type is called type conversion. It is done using 2 methods:

Implicit conversion
Explicit conversion

The process of conversion of lower data type to higher data type automatically by the compiler is
called promotion or implicit conversion. The rules followed by compiler during implicit conversion as
shown in below table
25

Operand 2
Char

int

long int

float

double

char

Char

int

long int

float

double

int

int

int

long int

float

double

long int

long int

long int

long int

float

double

float

float

float

float

float

double

double

double

double

double

double

double

Lower
data

Higher
data
type

type

Higher
data

Lower
data
type

type

If operand 1 is =int and operand 2=double observe from table that the operand is
promoted to double. Lower data type is converted to higher data type.

26

You might also like