C Language
C Language
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.
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
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.
Rules:
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.
A Numerical constant with one decimal point(.) is called floating point numerical constant.
Character constant:
Any single character enclosed with in single quotation marks ' ' is called charector constant.
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.
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
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
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
-------------------------------------------------
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 - >, <, >=, <=
Relational operators:
R.O Meaning
>
--------------------------------------------------------
6
Equality operators:
E.O Meaning
== Equal to
!= Not equal to
Logical operators:
L.O Meaning
|| logical OR
! logical NOT
Bitwise operators:
BIT - BInary digiT(0,1)
B.O Meaning
------------------------------------
| Bitwise OR
~ Bitwise compliment
---------------------------------------------
Special operators:
1) Unary operators
2) Ternary operators
7
3) Comma operator
4) Other operators
Unary operators:
U.O Meaning
--------------------------------
++ Increment
-- Decrement
---------------------------------------------
Ternary operators:
The ternary operators are ?, : . They are equalent to the if ... else statement. These operators are also
called conditional operators.
Comma operator:
Other operators:
Variable:
It is an identifier. It is used to store one value at a time in one name in the memory RAM.
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
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.
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>
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
3) %c - for character.
4) %s - for string.
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
Every C program must have one function main(). It indicates beginning and ending of program
execution.
main()
Local declaration;
statement1;
statement2;
:
10
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).
start->run->cmd
cd \tc
clrscr();
Note: This library function was defined in the header file conio.h
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.
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
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;
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
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)
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
}while (condition);
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
exp2: condition
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
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: for(i=0;i<5;i++)
scanf("%d",&n[i]);
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.
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'.
ex: scanf("%s",name);
ex: printf("%s",name);
String manipulations:
1) Combine two strings.
2) Copy a string.
In other words, A two dimensional array is group of sevaral one dimensional arrays.
The syntax is
datatype arrayname[size1][size2];
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.
ex: for(r=0;r<3;r++)
for(c=0;c<4;c++)
scanf("%d",&n[r][c]);
ex: for(r=0;r<3;r++)
for(c=0;c<4;c++)
{
22
printf("%d\t",n[r][c]);
printf("\n");
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"};
The syntax is
2) int get();
3) void xyz(int);
4) float rama(int,float);
-----;
-----;
2) int get()
-----;
-----;
3) void xyz(int x)
----;
----;
{
24
-----;
-----;
functionname(arguments);
ex: 1) abc();
2) n=get();
3) xyz(a);
4) n=rama(a,b);
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.
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.
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.
now the variable 'n' can process with pointer useing three steps:
the syntax is
datatype *pointervariablename;
26
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;
now an array 'n' can process with pointer useing four steps.
base address: The first element address of array is called base address.
ex: p=&n[0];
4) Access the values of the array with pointer includeing an operator '*'.
Memory allocations:
1) static 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));
p=(int *)malloc(5*sizeof(int));
free(pointervariable);
ex: free(p);
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.
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.
int c;
char name[10];
float sal;
};
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.
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
p=&q;
p->c
p->name
p->sal
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.
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");
p=fopen("abc.dat","w");
fclose(pointervaribel);
ex: fclose(p);
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);
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);
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);
a=PI*r*r;
int n[SIZE];
int sqr(int);
int sqr(int x)
int s=x*x;
return s;
n=sqr(5);
n=sqr(5);
{\
scanf("%d"&la[i]);\
}
33
printf("\nAnjaiah road");\
printf("\nOngole");