To make a decision from the number of choices
Decision variable is a single value
Each case will end with break
Syntax:
switch(Integer expression)
{
case condition 1:
Statement 1;
....
break;
case condition 2:
Statement 1;
....
break;
default:
Statement 1;
....
break;
}
Example:
main()
{
int a;
printf(“\n Enter a value:”);
scanf(“%d”,&a);
switch(a)
{
case 1:
printf(“ \nThis is case 1”);
break;
case 2:
printf(“ \nThis is case 2”);
break;
case 3:
printf(“ \nThis is case 3”);
break;
default:
printf(“\nDefault case”);
}
}
Output:
Enter a value:2
This is case 2
NOTE: Without using break in switch case statement executes the case where a match found and all subsequent statement cases and default as well.
Example:
main()
{
int a;
printf(“\n Enter a value:”);
scanf(“%d”,&a);
switch(a)
{
case 1:
printf(“ \nThis is case 1”);
case 2:
printf(“ \nThis is case 2”);
case 3:
printf(“ \nThis is case 3”);
default:
printf(“\nDefault case”);
}
}
Output:
Enter a value:2
This is case 2
This is case 3
Default case
Thursday, September 24, 2009
STRUCTURES
Structure is a collection of different datatype variables under a single name.
Structure is a method of packing data of different datatypes.
Syntax:
struct
{
datatype var1;
....
datatype var n;
} ;
Example:
struct stud
{
int studno;
char studname[20];
char classname[10];
} stud1,stud2;
Structure elements are stored in contiguous memory locations.
Number of bytes occupied in memory is:
int studno : 2bytes.
char studname : 20bytes.
char classname : 20bytes.
Total bytes occupied in memory is: 42 bytes(2+20+20).
Access fields of a structure with the “.” notation.
Example:
stud1.studno=10;
strcpy(stud1.studname,”Ram”);
strcpy(stud1.classname,”First”);
The values of the structure variables can be assigned to another structure variable of the same type using the assignment operator.
Example:
stud1=stud2;
One can be nested within another.
Example:
struct studper
{
float studpercen;
struct stud studinfo;
}studper1;
Example program:
main()
{
struct stud
{
int sno;
char sname[20];
}stud1;
printf(“\nEnter student number:”);
scanf(“%d”,stud1.sno);
printf(“\nEnter student name:”);
scanf(“%s”,stud1.sname);
printf(“\nStudent number:%d”,stud1.sno);
printf(“\nStudent name:%s”,stud1.sname);
}
Output:
Enter student number:1
Enter student name:RAJA
Student number:1
Student name:RAJA
Structure is a method of packing data of different datatypes.
Syntax:
struct
{
datatype var1;
....
datatype var n;
}
Example:
struct stud
{
int studno;
char studname[20];
char classname[10];
} stud1,stud2;
Structure elements are stored in contiguous memory locations.
Number of bytes occupied in memory is:
int studno : 2bytes.
char studname : 20bytes.
char classname : 20bytes.
Total bytes occupied in memory is: 42 bytes(2+20+20).
Access fields of a structure with the “.” notation.
Example:
stud1.studno=10;
strcpy(stud1.studname,”Ram”);
strcpy(stud1.classname,”First”);
The values of the structure variables can be assigned to another structure variable of the same type using the assignment operator.
Example:
stud1=stud2;
One can be nested within another.
Example:
struct studper
{
float studpercen;
struct stud studinfo;
}studper1;
Example program:
main()
{
struct stud
{
int sno;
char sname[20];
}stud1;
printf(“\nEnter student number:”);
scanf(“%d”,stud1.sno);
printf(“\nEnter student name:”);
scanf(“%s”,stud1.sname);
printf(“\nStudent number:%d”,stud1.sno);
printf(“\nStudent name:%s”,stud1.sname);
}
Output:
Enter student number:1
Enter student name:RAJA
Student number:1
Student name:RAJA
STORAGE CLASSES
The storage class determines the part of member storage is allocated for an object and how long the storage allocation continues to exit.
Storage class tells us:
1) Where the variable is stored.
2) Initial value of the variable.
3) Scope of the variable.Scope specifies the part of the program which a variable is accessed.
4) Life of the variable.
There are four types of storage classes:
1) Automatic storage class
2) Register storage class
3) Static storage class
4) External storage class
AUTOMATIC STORAGE CLASS:
In this automatic storage class,
Variable is stored in memory.
Default value is garbage value
Scope is local to the block
Life is, with in the block in which the variable is defined
Example 1:
main()
{
auto int i=10;
printf(“%d”,i);
}
Output:
10
Example 2:
main()
{
auto int i;
printf(“%d”,i);
}
Output:
1002
In example 1, i value is initialised to 10.So,output is 10.
In example 2, i value is not initialised.So,compiler reads i value is a garbage value.
REGISTER STORAGE CLASS:
Variable is stored in CPU registers.
Default value is garbage value.
Scope is local to the block.
Life is,with in the block in which the variable is defined.
We can not use register storage class for all types of variables.
For example:
register float f;
register double d;
register long l;
Example :
main()
{
register int i=10;
printf(“%d”,i);
}
Output:
10
STATIC STORAGE CLASS:
Variable is stored in memory.
Default value is zero.
Scope is local to the block.
Life is,value of the variable persists between different function calls.
Example :
main()
{
add();
add();
}
add()
{
static int i=10;
printf(“\n%d”,i);
i+=1;
}
Output:
10
11
EXTERNAL STORAGE CLASS:
Variable is stored in memory.
Default value is zero.
Scope is local to the block.
Life is,as long as the program execution doesn’t come to an end.
Example :
int i=10;
main()
{
int i=2;
printf(“%d”,i);
display();
}
display()
{
printf(“\n%d”,i);
}
Output:
2
10
In above example,i declared in two places.One is above the main(),second is within main().Within main() i is used in within main() only.Before main() i is used to outof the main() functions.i.e, display() uses the i value as 10.
NOTE:
Static and auto storage classes both are different in the case of life.Remaining all r same.
In the case of recursive function calls we use static storage class.
Register storage classes are used in the case of frequently used variables.Because these are stored in CPU registers.
Extern storage class for only those variables are used almost all functions in the program.This would avoid unnecessary passing of the variables.
Storage class tells us:
1) Where the variable is stored.
2) Initial value of the variable.
3) Scope of the variable.Scope specifies the part of the program which a variable is accessed.
4) Life of the variable.
There are four types of storage classes:
1) Automatic storage class
2) Register storage class
3) Static storage class
4) External storage class
AUTOMATIC STORAGE CLASS:
In this automatic storage class,
Variable is stored in memory.
Default value is garbage value
Scope is local to the block
Life is, with in the block in which the variable is defined
Example 1:
main()
{
auto int i=10;
printf(“%d”,i);
}
Output:
10
Example 2:
main()
{
auto int i;
printf(“%d”,i);
}
Output:
1002
In example 1, i value is initialised to 10.So,output is 10.
In example 2, i value is not initialised.So,compiler reads i value is a garbage value.
REGISTER STORAGE CLASS:
Variable is stored in CPU registers.
Default value is garbage value.
Scope is local to the block.
Life is,with in the block in which the variable is defined.
We can not use register storage class for all types of variables.
For example:
register float f;
register double d;
register long l;
Example :
main()
{
register int i=10;
printf(“%d”,i);
}
Output:
10
STATIC STORAGE CLASS:
Variable is stored in memory.
Default value is zero.
Scope is local to the block.
Life is,value of the variable persists between different function calls.
Example :
main()
{
add();
add();
}
add()
{
static int i=10;
printf(“\n%d”,i);
i+=1;
}
Output:
10
11
EXTERNAL STORAGE CLASS:
Variable is stored in memory.
Default value is zero.
Scope is local to the block.
Life is,as long as the program execution doesn’t come to an end.
Example :
int i=10;
main()
{
int i=2;
printf(“%d”,i);
display();
}
display()
{
printf(“\n%d”,i);
}
Output:
2
10
In above example,i declared in two places.One is above the main(),second is within main().Within main() i is used in within main() only.Before main() i is used to outof the main() functions.i.e, display() uses the i value as 10.
NOTE:
Static and auto storage classes both are different in the case of life.Remaining all r same.
In the case of recursive function calls we use static storage class.
Register storage classes are used in the case of frequently used variables.Because these are stored in CPU registers.
Extern storage class for only those variables are used almost all functions in the program.This would avoid unnecessary passing of the variables.
LOOPS
To execute a set of instructions repeatedly until a particular condition is being satisfied.
Three types of looping statements are there
1) For Loop
2) While Loop
3) Do while Loop
FOR LOOP:-
In for looping statement allows a number of lines represent until the condition is satisfied
Syntax:
for(initialize counter variable ; condition ; increment/decrement the counter variable)
{
Statement1;
...
Statement n;
}
main()
{
int counter,a[10];
for(counter=0;counter<10;counter++)
{
printf(“Enter a[%d]=”,counter);
scanf(“%d\n”,a[counter]);
}
printf(“Array elements:”);
for(counter=0;counter<10;counter++)
printf(“%d”,a[counter]);
}
Explanation:
In above program array a length 10.It occupies 20bytes in memory.
Counter is normal integer variable. It is used to read and display the array values.
First FOR LOOP is used to read the array values.
Second FOR LOOP is used to display the array values. In this second FOR LOOP using one line. So, no need of brace brackets
WHILE LOOP:
In While looping statement allows a number of lines represent until the condition is satisfied
Syntax:
while( condition)
{
Statement1;
...
Statement n;
}
main()
{
int counter,a[10];
counter=0;
while(counter<10)
{
printf(“Enter a[%d]=”,counter);
scanf(“%d\n”,a[counter]);
counter+=1;
}
printf(“Array elements:”);
counter=0;
while(counter<10)
{
printf(“%d”,a[counter]);
counter+=1;
}
}
Explanation:
In above program array a length 10.It occupies 20bytes in memory.
Counter is normal integer variable. It is used to read and display the array values.
Before using counter variable to initialize counter. Then use it in WHILE LOOP.
First WHILE LOOP is used to read the array values.
Second WHILE LOOP is used to display the array values.
DO WHILE LOOP:
In DO WHILE LOOP first execute the statements then it checks the condition.
Syntax:
do
{
Statement1;
...
Statement n;
}while(condition);
main()
{
int counter;
counter=0;
do
{
printf(“%d”,counter);
}while(counter>0);
}
CONCLUTION:
In FOR LOOP:
No need to initialize variable before the LOOP
In WHILE LOOP:
To initialize the variable before the LOOP
Increment/decrement the variable within the LOOP
In DO WHILE LOOP:
Once it execute If the condition is TRUE/FALSE.
Three types of looping statements are there
1) For Loop
2) While Loop
3) Do while Loop
FOR LOOP:-
In for looping statement allows a number of lines represent until the condition is satisfied
Syntax:
for(initialize counter variable ; condition ; increment/decrement the counter variable)
{
Statement1;
...
Statement n;
}
Example program using for loop:
main()
{
int counter,a[10];
for(counter=0;counter<10;counter++)
{
printf(“Enter a[%d]=”,counter);
scanf(“%d\n”,a[counter]);
}
printf(“Array elements:”);
for(counter=0;counter<10;counter++)
printf(“%d”,a[counter]);
}
Explanation:
In above program array a length 10.It occupies 20bytes in memory.
Counter is normal integer variable. It is used to read and display the array values.
First FOR LOOP is used to read the array values.
Second FOR LOOP is used to display the array values. In this second FOR LOOP using one line. So, no need of brace brackets
WHILE LOOP:
In While looping statement allows a number of lines represent until the condition is satisfied
Syntax:
while( condition)
{
Statement1;
...
Statement n;
}
Example program using while loop:
main()
{
int counter,a[10];
counter=0;
while(counter<10)
{
printf(“Enter a[%d]=”,counter);
scanf(“%d\n”,a[counter]);
counter+=1;
}
printf(“Array elements:”);
counter=0;
while(counter<10)
{
printf(“%d”,a[counter]);
counter+=1;
}
}
Explanation:
In above program array a length 10.It occupies 20bytes in memory.
Counter is normal integer variable. It is used to read and display the array values.
Before using counter variable to initialize counter. Then use it in WHILE LOOP.
First WHILE LOOP is used to read the array values.
Second WHILE LOOP is used to display the array values.
DO WHILE LOOP:
In DO WHILE LOOP first execute the statements then it checks the condition.
Syntax:
do
{
Statement1;
...
Statement n;
}while(condition);
Example program using for loop:
main()
{
int counter;
counter=0;
do
{
printf(“%d”,counter);
}while(counter>0);
}
CONCLUTION:
In FOR LOOP:
No need to initialize variable before the LOOP
In WHILE LOOP:
To initialize the variable before the LOOP
Increment/decrement the variable within the LOOP
In DO WHILE LOOP:
Once it execute If the condition is TRUE/FALSE.
FUNCTIONS
Definition:
A function is self contained blocks which perform a single task.
Any one of the task (number statements) can be repeatedly used. Then they are keeping in one function. i.e., without rewriting the same code repeatedly.
In function mainly contained three steps:
1) Function declaration
2) Function calling
3) Function definition
Local and Global variables of the function:
Local variables:
These variables are local to the block.
These variables are existing to the function itself.
They are unknown to the main() program.
Each time recreated when the function is call or executed.
Global variables:
These variables can be accessed in any of the function in the program.
Each time they do not create when the program call or executed.
If a variable of same name is declared within a function and outside of the function, the function will use local variable and ignore global variable.
Function declaration:
It is used to identify function return type, number of arguments, type of argument and function name.
It is declared above the main() .
Syntax:
Return type function name (argument1,argument2,..);
Example:
int add(int i,int j);
Function calling:
If we want to use functions using with the function call.
One function can call another function also.
Syntax:
With return value:
Return type variable=Function name (variable/value,...);
Without return value:
Function name (variable/value,.....);
Function definition:
In function definition to write the actual task (code of the function).
Syntax:
Function name (argument1,....)
{
Statement1;
....
Statement n;
return(value/variable);
}
If there is no return type then the statement is return 0;
Example 1:
Without return type and without arguments:
add(); //Function declaration
main()
{
add(); //Function calling
}
add() //Function definition
{
int i,j,res; //local variables to the function
printf(“\n Enter i and j values:”);
scanf(“%d %d”,&i,&j);
res=i+j;
printf(“\nResult :%d”,res);
}
Output:
Enter i and j values:2 3
Result:5
Example 2:
With return type and without arguments:
int add(); //Function declaration. int is return type
main()
{
int result;
result=add(); //Function calling. Here return value will accept result variable
printf(“Result i s:%d”,result);
}
add() //Function definition
{
int i,j,res; //local variables to the function
printf(“\n Enter i and j values:”);
scanf(“%d %d”,&i,&j);
res=i+j;
printf(“\nResult :%d”,res);
}
Enter i and j values:2 3
Result:5
Example3:
With return type and with arguments:
int add(int ,int); //Function declaration. int is return type
main()
{
int result,i,j;
printf(“\nEnter i and j values:”);
scanf(“%d %d”,&i,&j);
result=add(i,j); //Function calling. Here return value will accept result variable
printf(“Result i s:%d”,result);
}
add(int i,int j) //Function definition
{
int res;
res=i+j;
return res;
}
Output:
Enter i and j values: 2 3
Result is:5
Example 4:
With return type and with arguments using with global variables:
int add(int ,int); //Function declaration. int is return type
int subt(int ,int);
int i,j,result;
main()
{
printf(“\nEnter i and j values:”);
scanf(“%d %d”,&i,&j);
result=add(); //Function calling. Here return value will accept result variable
printf(“\n Add() Result i s:%d”,result);
result=subt();
printf(“\n sub() Result:%d”,result);
}
add(int i,int j) //Function definition
{
result=i+j;
return result;
}
subt(int i,int j) //Function definition
{
if(i>j)
result=i-j;
else
result=j-i;
return result;
}
Output:
Enter i and j values:2 4
Add() Result is:6
Subt() Result is:2
A function is self contained blocks which perform a single task.
Any one of the task (number statements) can be repeatedly used. Then they are keeping in one function. i.e., without rewriting the same code repeatedly.
In function mainly contained three steps:
1) Function declaration
2) Function calling
3) Function definition
Local and Global variables of the function:
Local variables:
These variables are local to the block.
These variables are existing to the function itself.
They are unknown to the main() program.
Each time recreated when the function is call or executed.
Global variables:
These variables can be accessed in any of the function in the program.
Each time they do not create when the program call or executed.
If a variable of same name is declared within a function and outside of the function, the function will use local variable and ignore global variable.
Function declaration:
It is used to identify function return type, number of arguments, type of argument and function name.
It is declared above the main() .
Syntax:
Return type function name (argument1,argument2,..);
Example:
int add(int i,int j);
Function calling:
If we want to use functions using with the function call.
One function can call another function also.
Syntax:
With return value:
Return type variable=Function name (variable/value,...);
Without return value:
Function name (variable/value,.....);
Function definition:
In function definition to write the actual task (code of the function).
Syntax:
Function name (argument1,....)
{
Statement1;
....
Statement n;
return(value/variable);
}
If there is no return type then the statement is return 0;
Example 1:
Without return type and without arguments:
add(); //Function declaration
main()
{
add(); //Function calling
}
add() //Function definition
{
int i,j,res; //local variables to the function
printf(“\n Enter i and j values:”);
scanf(“%d %d”,&i,&j);
res=i+j;
printf(“\nResult :%d”,res);
}
Output:
Enter i and j values:2 3
Result:5
Example 2:
With return type and without arguments:
int add(); //Function declaration. int is return type
main()
{
int result;
result=add(); //Function calling. Here return value will accept result variable
printf(“Result i s:%d”,result);
}
add() //Function definition
{
int i,j,res; //local variables to the function
printf(“\n Enter i and j values:”);
scanf(“%d %d”,&i,&j);
res=i+j;
printf(“\nResult :%d”,res);
}
Enter i and j values:2 3
Result:5
Example3:
With return type and with arguments:
int add(int ,int); //Function declaration. int is return type
main()
{
int result,i,j;
printf(“\nEnter i and j values:”);
scanf(“%d %d”,&i,&j);
result=add(i,j); //Function calling. Here return value will accept result variable
printf(“Result i s:%d”,result);
}
add(int i,int j) //Function definition
{
int res;
res=i+j;
return res;
}
Output:
Enter i and j values: 2 3
Result is:5
Example 4:
With return type and with arguments using with global variables:
int add(int ,int); //Function declaration. int is return type
int subt(int ,int);
int i,j,result;
main()
{
printf(“\nEnter i and j values:”);
scanf(“%d %d”,&i,&j);
result=add(); //Function calling. Here return value will accept result variable
printf(“\n Add() Result i s:%d”,result);
result=subt();
printf(“\n sub() Result:%d”,result);
}
add(int i,int j) //Function definition
{
result=i+j;
return result;
}
subt(int i,int j) //Function definition
{
if(i>j)
result=i-j;
else
result=j-i;
return result;
}
Output:
Enter i and j values:2 4
Add() Result is:6
Subt() Result is:2
CONDITIONAL STATEMENTS
Conditional statements controls the sequence of statements, depending on the condition
Relational operators:
Relational operators allow comparing two values.
== is equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
Complex logical operators:
It can combine expressions to get complex logical expressions
&& and
|| or
1) Simple If statement
2) If-else statement
3) if-else if statement
4) Nested if statement
SIMPLE IF STATEMENT:
It execute if condition is TRUE
Syntax:
if(condition)
{
Statement1;
.....
Statement n;
}
main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b)
printf(“\n a is greater than b”);
}
OUTPUT:
Enter a,b values:20
10
a is greater than b
IF-ELSE STATEMENT:
It execute IF condition is TRUE.IF condition is FLASE it execute ELSE part
Syntax:
if(condition)
{
Statement1;
.....
Statement n;
}
else
{
Statement1;
.....
Statement n;
}
main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b)
printf(“\n a is greater than b”);
else
printf(“\nb is greater than b”);
}
OUTPUT:
Enter a,b values:10
20
b is greater than a
IF-ELSE IF STATEMENT:
It execute IF condition is TRUE.IF condition is FLASE it checks ELSE IF part .ELSE IF is true then execute ELSE IF PART. This is also false it goes to ELSE part.
Syntax:
if(condition)
{
Statement1;
.....
Statementn;
}
else if
{
Statement1;
.....
Statementn;
}
else
{
Statement 1;
.....
Statement n;
}
Example:
main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b)
printf(“\n a is greater than b”);
else if(b>a)
printf(“\nb is greater than b”);
else
printf(“\n a is equal to b”);
}
OUTPUT:
Enter a,b values:10
10
a is equal to b
NESTED IF STATEMENT:
To check one conditoin within another.
Take care of brace brackets within the conditions.
Synatax:
if(condition)
{
if(condition)
{
Statement 1;
...
Statement n;
}
}
else
{
Statement 1;
....
Statement n;
}
Example:
main()
{
int a,b;
printf(“\n Enter a and b values:”);
scanf(“%d %d ”,&a,&b);
if(a>b)
if((a!=0) && (b!=0))
printf(“\na and b both are positive and a is greater than b);
else
printf(“\n a is greater than b only”)
else
printf(“ \na is less than b”);
}
Output:
Enter a and b values:30
20
a and b both are positive and a is greater than b
Relational operators:
Relational operators allow comparing two values.
== is equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
Complex logical operators:
It can combine expressions to get complex logical expressions
&& and
|| or
1) Simple If statement
2) If-else statement
3) if-else if statement
4) Nested if statement
SIMPLE IF STATEMENT:
It execute if condition is TRUE
Syntax:
if(condition)
{
Statement1;
.....
Statement n;
}
Example:
main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b)
printf(“\n a is greater than b”);
}
OUTPUT:
Enter a,b values:20
10
a is greater than b
IF-ELSE STATEMENT:
It execute IF condition is TRUE.IF condition is FLASE it execute ELSE part
Syntax:
if(condition)
{
Statement1;
.....
Statement n;
}
else
{
Statement1;
.....
Statement n;
}
Example:
main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b)
printf(“\n a is greater than b”);
else
printf(“\nb is greater than b”);
}
OUTPUT:
Enter a,b values:10
20
b is greater than a
IF-ELSE IF STATEMENT:
It execute IF condition is TRUE.IF condition is FLASE it checks ELSE IF part .ELSE IF is true then execute ELSE IF PART. This is also false it goes to ELSE part.
Syntax:
if(condition)
{
Statement1;
.....
Statementn;
}
else if
{
Statement1;
.....
Statementn;
}
else
{
Statement 1;
.....
Statement n;
}
Example:
main()
{
int a,b;
printf(“Enter a,b values:”);
scanf(“%d %d”,&a,&b);
if(a>b)
printf(“\n a is greater than b”);
else if(b>a)
printf(“\nb is greater than b”);
else
printf(“\n a is equal to b”);
}
OUTPUT:
Enter a,b values:10
10
a is equal to b
NESTED IF STATEMENT:
To check one conditoin within another.
Take care of brace brackets within the conditions.
Synatax:
if(condition)
{
if(condition)
{
Statement 1;
...
Statement n;
}
}
else
{
Statement 1;
....
Statement n;
}
Example:
main()
{
int a,b;
printf(“\n Enter a and b values:”);
scanf(“%d %d ”,&a,&b);
if(a>b)
if((a!=0) && (b!=0))
printf(“\na and b both are positive and a is greater than b);
else
printf(“\n a is greater than b only”)
else
printf(“ \na is less than b”);
}
Output:
Enter a and b values:30
20
a and b both are positive and a is greater than b
ARRAYS
It is data structures which contains or hold multiple values of same data type.
Array is a collection of similar data elements.
Before using an array its type and dimension must be declared.
Arrays elements can be stored in contiguous memory locations.
In C, there is no boundary checking for arrays.
Size indicates the maximum number of elements that can be stored inside the array.
Array elements starts with ZERO.
To enter data into array elements using with LOOPS.
Syntax of single dimension array:
Datatype arrayname[size of the array]={list of values};
Example:
int n[10];
size of the array is 10.
Type of the array is int.
Declaration of array:
int n[10]={1,2,3,4,5,6,7,8,9,10};
Read the array variables using FOR LOOP:
int i,n[10];
for(i=0;i<9;i++)
scanf(“%d”,&n[i]);
It reads 10 array elements start with 0 to 9 and stored in memory.
MULTIDIMENTIONAL ARRAYS:
It allows one or more dimensional arrays also.
To store and manipulate two dimensional data structures such as matrices and tables.
Declaration of two dimensional arrays:
Datatype arrayname[rowsize][columnsize];
Example:
int n[2][3];
In above example 2 is the row size and 3 is column size.
Total memory length is, row*column*sizeof the datatype = 2*3*2 = 12 bytes.
Initialization of the two dimensional array:
int n[2][3]={ {1,1,1},{2,2,2}};
Declaration of multidimensional array:
Datatype arrayname[r1][r2].....[r n];
Example of single dimensional array:
To read single dimensional array and add the array elements.
main()
{
int a[10],i,sum; //variable declaration
for(i=0;i<9;i++) //reading of array elements
{
printf(“\n Enter a[%d]:”,i);
scanf(“%d”,&a[i]);
}
sum=0;
for(i=0;i<9;i++)
sum+=a[i]; //adding of array elements
printf(“\nSum of array elements is:%d”,sum);
}
Output:
Enter a[0]=1
Enter a[1]=2
Enter a[2]=3
Enter a[3]=4
Enter a[4]=5
Enter a[5]=6
Enter a[6]=7
Enter a[7]=8
Enter a[8]=9
Enter a[9]=10
Sum of array elements is:55
Example of double dimensional array:
To read and write two dimensional array.
main()
{
int a[5][5],i,j; //variable declaration
for(i=0;i<5;i++) //for row elements
for(j=0;j<5;j++)//for column
{
printf(“\n Enter a[%d][%d]:”,i,j);
scanf(“%d”,&a[i][j]);
}
printf(“Display of array elements:\n”);
for(i=0;i<5;i++) //for row elements
{
for(j=0;j<5;j++)
printf(“ %d\t”,i,j,a[i][j]);
printf(“\n”);
}
}
Output:
Enter a[0][0]:1
Enter a[0][1]:2
Enter a[0][2]:3
Enter a[0][3]:4
Enter a[0][4]:5
Enter a[1][0]:1
Enter a[1][1]:2
Enter a[1][2]:3
Enter a[1][3]:4
Enter a[1][4]:5
Enter a[2][0]:1
Enter a[2][1]:2
Enter a[2][2]:3
Enter a[2][3]:4
Enter a[2][4]:5
Enter a[3][0]:1
Enter a[3][1]:2
Enter a[3][2]:3
Enter a[3][3]:4
Enter a[3][4]:5
Enter a[4][0]:1
Enter a[4][1]:2
Enter a[4][2]:3
Enter a[4][3]:4
Enter a[4][4]:5
Display array of elements:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Array is a collection of similar data elements.
Before using an array its type and dimension must be declared.
Arrays elements can be stored in contiguous memory locations.
In C, there is no boundary checking for arrays.
Size indicates the maximum number of elements that can be stored inside the array.
Array elements starts with ZERO.
To enter data into array elements using with LOOPS.
Syntax of single dimension array:
Datatype arrayname[size of the array]={list of values};
Example:
int n[10];
size of the array is 10.
Type of the array is int.
Declaration of array:
int n[10]={1,2,3,4,5,6,7,8,9,10};
Read the array variables using FOR LOOP:
int i,n[10];
for(i=0;i<9;i++)
scanf(“%d”,&n[i]);
It reads 10 array elements start with 0 to 9 and stored in memory.
MULTIDIMENTIONAL ARRAYS:
It allows one or more dimensional arrays also.
To store and manipulate two dimensional data structures such as matrices and tables.
Declaration of two dimensional arrays:
Datatype arrayname[rowsize][columnsize];
Example:
int n[2][3];
In above example 2 is the row size and 3 is column size.
Total memory length is, row*column*sizeof the datatype = 2*3*2 = 12 bytes.
Initialization of the two dimensional array:
int n[2][3]={ {1,1,1},{2,2,2}};
Declaration of multidimensional array:
Datatype arrayname[r1][r2].....[r n];
Example of single dimensional array:
To read single dimensional array and add the array elements.
main()
{
int a[10],i,sum; //variable declaration
for(i=0;i<9;i++) //reading of array elements
{
printf(“\n Enter a[%d]:”,i);
scanf(“%d”,&a[i]);
}
sum=0;
for(i=0;i<9;i++)
sum+=a[i]; //adding of array elements
printf(“\nSum of array elements is:%d”,sum);
}
Output:
Enter a[0]=1
Enter a[1]=2
Enter a[2]=3
Enter a[3]=4
Enter a[4]=5
Enter a[5]=6
Enter a[6]=7
Enter a[7]=8
Enter a[8]=9
Enter a[9]=10
Sum of array elements is:55
Example of double dimensional array:
To read and write two dimensional array.
main()
{
int a[5][5],i,j; //variable declaration
for(i=0;i<5;i++) //for row elements
for(j=0;j<5;j++)//for column
{
printf(“\n Enter a[%d][%d]:”,i,j);
scanf(“%d”,&a[i][j]);
}
printf(“Display of array elements:\n”);
for(i=0;i<5;i++) //for row elements
{
for(j=0;j<5;j++)
printf(“ %d\t”,i,j,a[i][j]);
printf(“\n”);
}
}
Output:
Enter a[0][0]:1
Enter a[0][1]:2
Enter a[0][2]:3
Enter a[0][3]:4
Enter a[0][4]:5
Enter a[1][0]:1
Enter a[1][1]:2
Enter a[1][2]:3
Enter a[1][3]:4
Enter a[1][4]:5
Enter a[2][0]:1
Enter a[2][1]:2
Enter a[2][2]:3
Enter a[2][3]:4
Enter a[2][4]:5
Enter a[3][0]:1
Enter a[3][1]:2
Enter a[3][2]:3
Enter a[3][3]:4
Enter a[3][4]:5
Enter a[4][0]:1
Enter a[4][1]:2
Enter a[4][2]:3
Enter a[4][3]:4
Enter a[4][4]:5
Display array of elements:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
PREPROCESSORS:
Preprocessor directives or commands begin in a ‘ # ’.
Preprocessor process the code before it passes through compiler.
Advantages of Preprocessors:
Easy to develop
Easy to understand
Preprocessor directives are,
#define --- Defines a macro substitution or constants.
#undef ---- Undefines a macro previously created by #define.
#include ---- Specifies a file to be included.
#endif ---- Specifies the end of #if.
#ifndef ---- Tests whether the macro is not def.
#if ----- Code will compiled only if a specified condition is met.
#else ---- Specifies alternatives when #if condition fails.
Preprocessors can be divided into three categories:
1. Macro Definition and Substitution.
2. File inclusion.
3. Conditional compilation.
1. Macro Definition and substitution:
Macro substitution is a process where an identifier in a program is replaced by a predefined string.
Writing macro definition in capitals is a convention not a rule.
Ex: #define PI 3.14159
#define AREA 12.36 // Macro substitution
#define CUBE(x) (x*x*x) // Macro as an argument
2. File inclusion:
#include with the system header file of that name,which controls console input and output.
This file inclusion also be written using double quotes.
#include “conio.h”
If the filename is enclosed within the angle brackets,the file searched for in the standard compiler include paths.
If the filename is enclosed within the double quotes,the search path is expanded to the current source directory.
3. Condition compilation:
In conditional compilation a piece code can be compiled, when the specified condition is true.
Syntax:
#ifdef name
Program text
#else
Program text
#endif
Example:
#ifdef MAX_LENGTH
#define MAX_LENGTH 100
#endif
char str[MAX_LENGTH];
Preprocessor process the code before it passes through compiler.
Advantages of Preprocessors:
Easy to develop
Easy to understand
Preprocessor directives are,
#define --- Defines a macro substitution or constants.
#undef ---- Undefines a macro previously created by #define.
#include ---- Specifies a file to be included.
#endif ---- Specifies the end of #if.
#ifndef ---- Tests whether the macro is not def.
#if ----- Code will compiled only if a specified condition is met.
#else ---- Specifies alternatives when #if condition fails.
Preprocessors can be divided into three categories:
1. Macro Definition and Substitution.
2. File inclusion.
3. Conditional compilation.
1. Macro Definition and substitution:
Macro substitution is a process where an identifier in a program is replaced by a predefined string.
Writing macro definition in capitals is a convention not a rule.
Ex: #define PI 3.14159
#define AREA 12.36 // Macro substitution
#define CUBE(x) (x*x*x) // Macro as an argument
2. File inclusion:
#include
This file inclusion also be written using double quotes.
#include “conio.h”
If the filename is enclosed within the angle brackets,the file searched for in the standard compiler include paths.
If the filename is enclosed within the double quotes,the search path is expanded to the current source directory.
3. Condition compilation:
In conditional compilation a piece code can be compiled, when the specified condition is true.
Syntax:
#ifdef name
Program text
#else
Program text
#endif
Example:
#ifdef MAX_LENGTH
#define MAX_LENGTH 100
#endif
char str[MAX_LENGTH];
Subscribe to:
Posts (Atom)