Professional Documents
Culture Documents
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
Machine language:
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.
Translation is free.
High speed.
Machine dependent
Complex language
Assembly language:
Functions of assembler:
In case of large assembly programs it also provides linking facility among the
subroutines.
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.
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.
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:
Machine independent.
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
Definition
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
Objects
Classes
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.
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:
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.
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.
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.
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
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.
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
13
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'.
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 */
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:
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.
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
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127
int
2 or 4 bytes
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.
ii.
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
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.
Exponential
length
Mantissa
Float
8 bits
23bits
Double
11 bits
52 bits
2. Pointer type
3. Function type
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.
.
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
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;
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.
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.
II.
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