1
Reference notes:
C - Language
Introduction of C:
It is very popular function oriented structured programming high level language. It is originally called
middle level language, because it is in between low level and high level language. Initially, it was
developed to write the programs on UNIX operating system. After modifications, now it is able to use on
any type of operating systems.
History of C:
It was developed by system engineer "Dennise Ritchie" at AT & T Bell lab, Newjersy of USA
in 1972. AT & T stands for American Telephone & Telegraph. It is derived from another
language 'B'. It was developed by programmer "Kenthomson" at Bell lab in 1969. It is also
derived from another language BCPL (Basic Combined Programming Language). It was
developed by professor "Martin Richards" at Cambridge university in 1967.
Features of C:
1) It is multiple purpose language.
2) It is very portable language.
3) It supports several data types.
4) It has sevaral predefined functions(Library functions).
5) It supports user defined functions.
6) It can be used to develop application softwares and system softwares.
Fundamentals of C:
1) Character set
2) keywords
3) Identifier
4) Constants
5) Datatypes
6) Operators
7) Variable
8) Function
2
Character set:
The group of characters which are supported by C language is called character set. They are divided in
to three types.
1) Letters - A to Z, a to z
2) Digits - 0 to 9
3) Special characters - +,-,*,/,%,#,!,~,?,\,&,^, : , ; , , , . ,(,),[,],{,},',",<,>,=,|,_
Keywords: The language predefined words are called keywords. C has 32 keywords. Every keyword
must use in lowercase only. They cannot redefined by the user in the program.
ex: int, float, for, break, struct,.....
Identifier(User defined word):
The name given to the program elements variable, array, function by the user is called an identifier.
Rules:
1) It may consists of letters(a-z),digits(0-9) and one special charector underscore( _ ).
2) It must begin with letter.
3) It is not allowed a blank space.
4) It should not be name of the keyword.
ex: code, emp_no, mark1, n
Constant:
A data item which does not change during running of a program is called a constant.
Types of constants:
C has three types of constants. They are
1. Numerical constant
2. Character constant
3. String constant
Numerical constant:
A constant which represents sequence of digits is called a numerical constant. It is not allowed a blank
space and special character , . If it is negative, sign - is essential.
1. Integer numerical constant
2. Floating point numerical constant
A numerical constant without decimal point(.) is called integer numerical constant.
3
ex: 125, -256, 100000
A Numerical constant with one decimal point(.) is called floating point numerical constant.
ex: 1.25, -56.6, 100.00000
Character constant:
Any single character enclosed with in single quotation marks ' ' is called charector constant.
ex: 'a', '7', '+'
String constant:
The sequence of several characters grouped together is called a string.
The string enclosed with in double quotation marks " " is called string constant.
ex: "anil", "ongole", "rama rao"
Datatypes:
The datatypes are used to define the type of data to be process in the program. C supports three types
datatypes. They are
1) Built-in datatypes
2) User defined datatypes
3) Derived datatypes
Built-in datatypes:
The language predefined datatypes are called built-in datatypes. C has 5 built-in datatypes. They are
1) int
2) float
3) double
4) char
5) void
int: It stands for integer. It is used to define the data as short signed integer numerical value. The size is 2
bytes. The range is
-32768 to 32767
long int - long signed integer - 4 bytes
unsigned int - unsigned short integer,2 bytes
unsigned long int - unsigned long integer, 4 bytes
4
float: It is used to define the data as short floating point numerical value.The size is 4 bytes. The range is
3.4e-38 to 3.4e+38.
double:(long float): It is used to define the data as long floating point numerical value.The size is 8
bytes. The range is 1.7e-308 to 1.7e+308
char: It stands for character. It is used to define the data as a character. The size is 1 byte.
User defined data types: The data types which are defined by user are called user defined data types. C
has 3 user defined data types. They are
structure
union
enumeration
Derived data types: The data types which are derived from other data types are called derived data
types. C has three derived data types.
1) Array
2) Pointer
3) Function
OPerators:
An operator is a small symbol which is used to perform a small operation on the data. C has large
number of operators. They are devided in to 5 types.
1) Arithmetic operators
2) Assignment operators
3) Comparison operators
4) Bitwise operators
5) Special operators
Arithmetic operators:
These operators are used to perform arithmetic operations.
A.O Meaning
------------------------------------
+ Addition
- Subtraction
5
* Multiplication
/ Division
% Integer division for remainder
-------------------------------------------------
Assignment operators:
The main assignment operator is '='. It is used to assign one value at a time to one name to store in the
memory. C providing some more assignment operators addition to the main operator. They are +=, -=, *=,
/=, %=, &=, |=, ^=, ~=, >>=, <<=. These operators are called compound assignment operators.
a=10;
a=a+5;
a+=5
Comparison operators:
1) Relational operators - >, <, >=, <=
2) Equality operators - ==, !=
3) Logical operators - && , ||, !
Relational operators:
R.O Meaning
< Less than
>
<= less than or equal to
>= Greater than or equal to
--------------------------------------------------------
6
Equality operators:
E.O Meaning
== Equal to
!= Not equal to
Logical operators:
L.O Meaning
&& Logical AND
|| logical OR
! logical NOT
Bitwise operators:
BIT - BInary digiT(0,1)
These operators are used to process the given data bitwise(0,1).
B.O Meaning
------------------------------------
& Bitwise AND
| Bitwise OR
^ Bitwise XOR(Exclusive OR)
~ Bitwise compliment
>> Bitwise rightshift
<< Bitwise leftshift
---------------------------------------------
Special operators:
1) Unary operators
2) Ternary operators
7
3) Comma operator
4) Other operators
Unary operators:
U.O Meaning
--------------------------------
++ Increment
-- Decrement
sizeof Determine size of the datatype
---------------------------------------------
Ternary operators:
The ternary operators are ?, : . They are equalent to the if ... else statement. These operators are also
called conditional operators.
Comma operator:
The comma operator is , . It is used for separation.
Other operators:
The other operators are (,),[,],{,}
Variable:
It is an identifier. It is used to store one value at a time in one name in the memory RAM.
Declaring the variables:
The syntax is
datatype variablename;
ex: int n;
float a;
int x,y,z;
Function:
A function is a small program. It is used to do a small work define by the user.
Types of functions:
C has two types of functions. They are
1) Library functions
8
2) User defined functions
Library functions:
The language predefined functions are called library functions. C has several library functions. These
functions are defined and stored in the form of files in the library. These files are called "header files".
Every header file has one unique name.
ex: stdio.h, math.h, conio.h,.....
In order to use library functions in the program, the name of the header file must mention in the
program using a preprocessor statement "#include". The syntax is
#include<header filename>
ex: #include<stdio.h>
#include<conio.h>
Input / Output operations:
The I/O operations can perform using two library functions. They are
1) scanf()
2) printf()
scanf() : It is an input library function. It is used to input any type and any number of values at a time
from keyboard in to the program. The syntax is
scanf("conversion charectors",&variable1,&variable2,......);
here, conversion characters are used to control input values. They are
1) %d - for short integer numerical value.
2) %f - for short floating point value.
3) %c - for character.
4) %s - for string.
5) %u - for unsigned integer value.
6) %ld - for long integer value.
7) %lf - for long floating point value.
ex: scanf("%d",&a);
scanf("%f",&n);
9
scanf("%d%d%d",&x,&y,&z);
printf(): It is an output library function. It is used to output any type and any number of values at a time
from program to the monitor. The syntax is
printf("conversion charectors",item1,item2,.....);
here conversion characters are used to control output values. item1,item2,..... are represents
constants,variables,expressions.
ex:
1) printf("%d",100);
2) printf("%d",n);
3) printf("%d",n+30);
4) printf("%d%f",n,a);
5) printf("kumar");
6) printf("sum=%d",n+40);
Note: These two library functions are defined in the header file stdio.h
Structure of a program:
1) Header files including - Essential
2) Define symbolic constants - Optional
3) Global declaration - Optional
4) main() function - Essential
Every C program must have one function main(). It indicates beginning and ending of program
execution.
main()
Local declaration;
statement1;
statement2;
:
10
A simple program to display an address of a person:
Anil kumar
Anjaiah road
Ongole
#include<stdio.h>
main()
printf("Anil kumar");
printf("Anjaiah road");
printf("Ongole");
Loading C:
1) Ready the system by loading some OPerateing system(windowsXP).
2) Change in to command prompt.
start->run->cmd
3) Change in sub directory \tc
cd \tc
4) Type command tc to load C
After loading C, The following commands are used to implement program.
1) Save the program - File->save [F2]
2) Compile the program - Compile->compile to OBJ[Alt+F9]
3) Run the program - Run->run [Ctrl+F9]
4) Open the user screen - Run->user screen[Alt+F5]
5) Open the new file - File->New
6) Open the old file - File->load[F3]
11
7) Close C perminently - File->quit [Alt+x]
Clear user screen:
C providing a library function to clear entire user screen. That is clrscr(). The syntax is
clrscr();
Note: This library function was defined in the header file conio.h
(Console input output header file)
Esape sequence charectors:
1) \n - new line
2) \t - horizontal tab
statements:
C has three types of statements. They are
1) Expression statements
2) Compound statement
3) Control statements
Expression statements:
These statements are used to assign one value to the one variable, increment and decrement value of
the variable, calculating arithmetic expression and calling the functions.
ex: 1) a=10;
2) a++;
3) a--;
4) c=a+b;
5) clrscr();
Compound statement:
Two or more statements enclosed with in flower brackets { } is called compound statement.
ex:
a=10;
b=20;
12
c=a+b;
Control statements:
Default, the statements in the program are execute in sequential order. This sequential order can control
using control statements.
ex: goto, if, switch, while, do while, for
Methods of controlling:
The sequential order can be control in three methods useing control statements. They are
1. Jumping
2. Branching
3. Looping
Jumping:
In this method, the control will transfer from one place to another place with in the same program. This
method control statement is 'goto'. The syntax is
goto labelname;
here labelname is used to identify the place to transfer the control. A label name can defined as
labelname:
ex: ----------;
---------;
goto abc;
---------;
---------;
---------;
abc:
---------;
---------;
Branching:
In this method, C has two control statements. They are
1. if statement
13
2. switch statement
if statement:
C has three types of if statement.
i) Simple if statement
ii) if .... else statement
iii) ladder if statement
Simple if statement:
It is used to define one alternative set of statements. This set of statements are select and execute
depends on condition. The syntax is
if (condition)
statements set
i=1
abc:
print i
i=i+1
if (i<=10)
goto abc;
if ... else statement:
It is used to define maximum two alternative set of statements. At the time of program execution, one
statements set are select and execute depends on condition.
The syntax is
if (condition)
14
statements set1
else
statements set2
Ladder if statement:
It is multiple branching control statement. It is used to define more than two alternative set of
statements. At the time of program execution , one set of statements are select and execute.
The syntax is
if (condition1)
statements set1
else if (condition2)
statements set2
else if (condition3)
statements set3
else
15
statements setn
charector library functions:
i) isalpha() - To test the given letter is alphabet or not.
ii) isupper() - To test the given letter is uppercase alphabet or not.
iii) islower() - To test the given letter is lowercase alphabet or not.
iv) isdigit() - To test the given letter is numerical digit or not.
v) tolower() - To convert the given letter in to lowercase alhabet.
vi) toupper() - To convert the given letter in to uppercase alphabet.
Note: header file name is ctype.h
switch statement:
It is another multiple branching control statement. It is used to define more than two alternative set of
statements. At the time of program execution , select and execute one statements set. It is equalent to the
ladder if statement. The syntax is
switch(variable)
case constant1:
statements set1
break;
case constant2:
statements set2
break;
:
16
default:
statements setn
Looping:
1. while statement
2. do while statement
3. for statement
while statement:
It is used to execute the given one set of statements repeatedly as long as the condition is satisfied.
The syntax is
while (condition)
statements set-Body of loop
ex: print 1 to 10 numbers
i=1;
while (i<=10)
printf("\n%d",i);
i++;
do while statement:
It is used to execute the given set of statements repeatedly as long as the condition is satisfied.
The syntax is
do
{
17
statements set-Body of loop
}while (condition);
ex: print 1 to 10 numbers
i=1;
do
printf("\n%d",i);
i++;
}while (i<=10);
for statement:
It is used to execute the given set of statements repeatedly as long as the condition is satisfied.
The syntax is
for(exp1;exp2;exp3)
statements set
exp1: Assignment statement
exp2: condition
exp3: modifier statement
ex: print 1 to 10 numbers
for(i=1;i<=10;i++)
printf("\n%d",i);
break statement:
It is used to jump from some where in the middle of loop to outside loop with force. The syntax is
break;
18
ex:
i=1;
while (i<=10)
printf("\n%d",i);
i++;
if (i>5)
break;
continue statement:
It is used to jump from some where in the middle of loop to begining of loop with force. The syntax is
continue;
ex:
i=1;
while (i<=10)
printf("\n%d",i);
i++;
if (i>5)
continue;
ARRAYS
It is another identifier. It is used to store several values of same type in the same name in the memory.
Types of arrays:
C has two types of arrays. They are
1) One dimensional array.
19
2) Two dimensional array.
One dimensional array:
An array which is used to store sevaral values of same type in the same name sequentially in one row
in the memory is called one dimensional array.
Declaring the one dimensional array:
Like variable, every one dimensional array must declare in the program. The syntax is
datatype arrayname[size];
here, size should be positive integer value. It specifies maximum number of values can store in the
arrayname.
ex: int n[5];
array elements n[0],n[1],...,n[4]
Inputting values in to array:
We can input the values in to one dimensional array by repeateing a library function scanf() with one
looping control statement.
ex: for(i=0;i<5;i++)
scanf("%d",&n[i]);
Outputting values of one dimensional array:
We can output the values of one dimensional array by repeateing a library function printf() with one
looping control statement.
ex: for(i=0;i<5;i++)
printf("%d",n[i]);
String:
In C, a string can store and process using one dimensional array of type char.
ex: char name[10];
20
we can store maximum size-1 that is 10-1=9 number of charectors in the array name. Because every
string in the array will end with null charector '\0'.
Inputting a string in to the array:
we can input a string in to the array useing a library function scanf() with conversion charector "%s".
ex: scanf("%s",name);
Outputting a string from the array:
we can output a string from array useing a function printf() with conversion charector "%s".
ex: printf("%s",name);
String manipulations:
1) Combine two strings.
2) Copy a string.
3) Determine length of the string.
4) Determine reverse string.
5) Compare two strings.
6) Convert upper string in to lower string.
7) Convert Lower string in to upper string.
String library functions:
1) strcat()-To combine two strings.
2) strcpy()-To copy a string.
3) strlen()-To find length of a string.
4) strrev()-To reverse the string.
5) strcmp()-To compare two strings.
6) strlwr()-To convert in to lower string.
7) strupr()-To convert in to upper string.
Note: header file name is string.h
21
Two dimensional array:
An array which is used to store sevaral values of same type in the same name in the order of rows and
columns is called two dimensional array.
In other words, A two dimensional array is group of sevaral one dimensional arrays.
Declare the two dimensional array:
The syntax is
datatype arrayname[size1][size2];
ex: int n[3][4];
now, the compiler will allocates 12 memory cells (3x4) in the order of 3 rows and 4 columns. that is
the memory cells are access with array elements. The array element will referenced with two index
numbers.
for example n[0][0], n[0][1],...., n[2][3]
Inputting values in to the two dimensional array:
we can input values in to two dimensional array by repeateing a library function scanf() with two
looping control statements.
ex: for(r=0;r<3;r++)
for(c=0;c<4;c++)
scanf("%d",&n[r][c]);
Outputting values of two dimensional array:
The values of two dimensional array can output by repeateing a library function printf() with two
looping control statements.
ex: for(r=0;r<3;r++)
for(c=0;c<4;c++)
{
22
printf("%d\t",n[r][c]);
printf("\n");
int n; -> variable declaration
int n=10; -> variable intialization
int n[5]; -> array declaration
int n[5]={10,20,30,40,50}; -> array intialization
int n[3][4]={
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
char name[10]={'a','n','i','l','\0'};
or
char name[10]="anil";
char name[3][10]={"anil","kumar","devid"};
FUNCTIONS (User Defined Functions)
The functions which are defined by the user are called user defined functions. Every user defined
function must have the following three steps.
1) Declare the function.
2) Define the function.
3) Call the function.
Declare the function:(Funtion prototype)
The syntax is
returndatatype functionname(arguments type);
23
ex: 1) void abc();
2) int get();
3) void xyz(int);
4) float rama(int,float);
Define the function:
The syntax is
returndatatype functionname(arguments declaration)
statements --> function body
ex: 1) void abc()
-----;
-----;
2) int get()
-----;
-----;
3) void xyz(int x)
----;
----;
4) float rama(int x,float y)
{
24
-----;
-----;
Call the function:
The syntax is
functionname(arguments);
ex: 1) abc();
2) n=get();
3) xyz(a);
4) n=rama(a,b);
Methods of function definitions:
1) A function with out arguments and with out return value.
2) A function with out arguments and with return value.
3) A function with arguments and with out return value.
4) A function with arguments and with return value.
Method1:
when this method of function is called, only program execution control will transfer from calling in to
definition. After executing statements, only program execution control will transfer from definition to the
calling.
Method2:
when this method of function is called, only program execution control will transfer from calling in to
definition. After executing statements, one value will transfer from definition to the calling.
C providing a statement 'return' to transfer one value at a time from definition to the calling. The syntax
is
return value;
or
return(value);
Method3:
when this method of function is called, some values are transfer from calling in to definition using
arguments or parameters. After executing statements in the definition, only control will transfer to calling.
25
The entries which are using in the brackets after functionname are called arguments or parameters.
1) Actual arguments
2) Formal arguments
The arguments which are using in the calling are called actual arguments and which are using in the
definition are called formal arguments.
Rules:
1) The number of actual arg. and number of formal arg. must equal.
2) The type of actual argument and type of formal argument must be same.
3) The actual arguments may represent contants,identifiers and expressions.
4) The formal arguments should be identifiers.
5) The both arguments should separated by ','
Method4:
when this method of function is called, some values are transfer from calling in to definition using
arguments or parameters. After executing statements, one value will transfer from definition to the calling
using a 'return' statement.
Function with array:
The arrays also can use as argument in the function. To pass all values at a time, an array name can be
used as actual arguments instead of invidual elements. but the corrosponding formal argument also must
declare as an array.
POINTERS
A pointer is a variable. It is used to store an address of another variable in the memory.
An address is an unsigned integer number genarated by OS to every byte of space in the RAM.
Processing variable with pointer:
the value of a variable can process indirectly with pointer variable. It was described with example as
follows:
now the variable 'n' can process with pointer useing three steps:
1) Declare the pointer variable
the syntax is
datatype *pointervariablename;
26
ex: int *p;
2) Determine an address of variable 'n' and store in the pointer variable 'p'. C providing an operator '&' to
determine an address. This operator is called address operator.
ex: p=&n;
3) Access the value of a variable 'n' with pointer variable 'p' including an operator '*'. This operator is
called indirection operator.
ex: int a;
a=*p;
Processing array with pointer:
Like variable, an array also can process with pointer indirectly. It was described with example as
follows:
now an array 'n' can process with pointer useing four steps.
1) Declare the pointer variable.
ex: int *p;
2) Determine base address of array and store in the pointer variable
base address: The first element address of array is called base address.
ex: p=&n[0];
3) Generate remaining addresses useing base address.
ex: p+1, p+2, p+3,p+4
4) Access the values of the array with pointer includeing an operator '*'.
ex: *(p+0), *(p+1),......
Memory allocations:
1) static memory allocation
2) Dynamic memory allocation
static memory allocation:
compile time allocation is called static memory allocation.
27
Dynamic memory allocation:
Run time allocation is called dynamic memory allocation.
malloc() function: It is used to allocate memory space to the pointer variable at the time of program
running.
The syntax is
pointervariable=(datatype *)malloc(n*sizeof(datatype));
ex: int *p;
p=(int *)malloc(5*sizeof(int));
free() library function:
It is used to release the space from pointer variable dynamically. The syntax is
free(pointervariable);
ex: free(p);
Note: header file is alloc.h
Types of function calling:
1) call by value
2) call by reference
call by value:
When a function is called , the values of actual arguments are transfer in to the definition(formal
arguments). If any modifications done in formal arguments , the actual arguments will not modified.
call by reference:
When a function is called , the address of actual arguments are transfer in to the definition(formal
arguments). If any modifications done in formal arguments , the actual arguments also are modified.
28
STRUCTURE
It is an user defined datatype.It is used to group different type of sevaral related data items in to one
name.
Define the structure:
Every structure must define to define the name of the structure, number of items and type of items to be
group in the structure.
The syntax is
struct structurename
datatype item1;
datatype item2;
};
here, struct is the keyword. structure name is user defined word. item1,item2,... are reprsents variables
and arrays of different types. These are called members of structure.
ex: struct emp
int c;
char name[10];
float sal;
};
Declare the structure variable:
The syntax is
struct structurename variable;
ex: struct emp p;
29
Access the members of the structure:
The members of structure can access with structure variable separated by . (dot) operator
The syntax is
structurevariable.member
ex: p.c
p.name
p.sal
Array of structure:
It is used to represent and process sevaral blocks of data at time with one name.
ex: struct emp p[5];
Pointer of structure:
The process of declareing pointer variable of structure is known as pointer of structure. In this case the
members are accessed separated '->' operator instead of '.' operator
ex: struct emp q,*p;
p=&q;
p->c
p->name
p->sal
Functions with structure:
We can use structure type variables , arrays, pointers as arguments in the functions.
ex:
struct emp p;
display(p);
30
DATA FILES
A file which is used to store data perminently in the perminent memory (disks) is known as data file.
Advantages:
1) Data can be perminent.
2) The large amount of data can be store and process.
3) The same data can use in sevaral programs.
Open the data file:
Every data file must open to access the data. In C, a data file can open useing a library function
"fopen()". The syntax is
pointervarible=fopen("filename","filemode");
ex: FILE *p;
p=fopen("abc.dat","w");
Close the data file:
Every open file must close at the end of the program to secure the data. A data file can close useing
another library function "fclose()". The syntax is
fclose(pointervaribel);
ex: fclose(p);
charector input/output operations:
The charector I/O operations on data file can perform useing two library functions.
1) getc()
2) putc()
getc():
It is used to input one charector at a time from data file. The syntax is
variable=getc(pointervariable);
ex: char n;
n=getc(p);
31
putc():
It is used to output one charector at a time in to the data file. The syntax is
putc(variable,pointervariable);
ex: putc(n,p);
Integer input/output opetrations:
1) getw()
2) putw()
getw():
It is used to input one integer number at a time from data file. The syntax is
variable=getw(pointervariable);
ex: int n;
n=getw(p);
putw():
It is used to output one integer number at a time in to the data file. The syntax is
putw(variable,pointervariable);
ex: putw(n,p);
Unformatted Input/Output operations:
1) fread()
2) fwrite()
fread() : It is used to input one block of data at a time from data file in to RAM. The syntax is
fread(&variable,sizeof(variable),1,pointervariable);
ex: fread(&a,sizeof(a),1,p);
fwrite(): It is used to output one block of data at a time from RAM in to the data file. The syntax is
32
fwrite(&variable,sizeof(variable),1,pointervariable);
ex: fwrite(&a,sizeof(a),1,p);
Defineing symbolic constant:
#define
#define symbolicname constantvalue
ex: #define PI 3.14
a=PI*r*r;
#define SIZE 100
int n[SIZE];
int sqr(int);
int sqr(int x)
int s=x*x;
return s;
n=sqr(5);
#define sqr(x) x*x
n=sqr(5);
#define print printf("\nanil kumar");
print
#define LOOP for(i=0;i<size;i++)\
{\
scanf("%d"&la[i]);\
}
33
#define PRINT printf("anil kumar");\
printf("\nAnjaiah road");\
printf("\nOngole");