Академический Документы
Профессиональный Документы
Культура Документы
ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UNIT-II
Function:
Function means, a large program can be divided into a series of individual related programs called
modules. These modules are called functions. A function is a program segment that carries out some specific
and well defined tasks.
1 : Reusability : The function write once and use many times is called Reusability.
2 : Modular Program Appraoch : A large program is divided into smaller sub programs . so that each sub
program performs a specific task. This approach makes the program development more manageable.
3 : Efficiency : means by avoiding redundant instructions the size of program can be reduced. Which increase
efficiency of program.
4 : Function sharing : A function can be shared by many programmers
1 : Built-in Functions (OR) Library Functions : The standard C library is a collection of various types of
functions which perform some standared and predefined tasks. These function which part of the C compiler that
have been written for general purpose are called library functions. They are alo called built-in functions.
Examples :
1 : The function sqrt() is used for to find squre root of a given number.
2 : The function scanf() is used to read data from the keyboard.
3 : The function printf() is used to print the result on the display.
Advantages :
1
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 : The programmer’s job is made easier because the functions are already available.
2 : The library functions can be used whenever required.
Disadvantages : The standared library functions are limited, programmes can not completely on these library
functions. The programmer has to write his own programs.
This disadvantages of standred library functions can be overcome by using user-defined function.
Ex : #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n=36,s;
clrscr();
s=sqrt(n);
printf(“Squre root of %f:”,s);
getch();
}
2 : User Defined Functions : These functions which are written by the programmer to do some specific tasks
are called User Defined Functions.
1 : Reusability : The function write once and use many times is called Reusability.
2 : Modular Program Appraoch : A large program is divided into smaller sub programs . so that each sub
program performs a specific task. This approach makes the program development more manageable.
3 : Efficiency : means by avoiding redundant instructions the size of program can be reduced. Which increase
efficiency of program.
4 : Function sharing : A function can be shared by many programmers.
2
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Declaration (Function Prototype) : Like variable, all functions in a C program must be declared,
before they are invoked. The declaration of each function should end with semicolon. Ths is called function
prototype or function declaration.
The function declaration/prototype tells the compiler about the return type, the name of the function
and type of parameters which will be called and defined later in the program.
Syntax : function-name();
function-name(parameters);
return value = function-name(parameters);
Where
type : return type can be int ,float,double,void etc. This is the type of the value that the function is expected to
return. If the function is not returning any value, then we need to specify the return types as void.
function-name : is the name of the function.
parameter-list : Parameter list declares the variables that will receive the data sent by the calling program.
#include<stdio.h>
void add(); //function declaration/prototype
void main()
{
add(); //Function call
}
void add() // function definition
{
int a,b,c;
printf(“Enter the values of a and b”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“%d”,c);
}
1 : Actual Parameters : These are the parameters transferred from the calling function (main program) to
the called function (function).
2 : Formal Parameters :These are the parameters transferred into the calling function (main program) from
the called function(function).
Ex : main()
{
4
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fun1( a , b ); //Calling function
}
fun1( x, y ) //called function
{
..... .
}
Where
a, b are the Actual Parameters
x, y are the Formal Parameters
1 : Local Variable.
2 : Global Variable.
1 : Local Variable : The local variables are defined within the body of the function or block. These
variables can acess by that function only, other functions can not acess these variables.
Ex : fun1(int a,int b)
{
int c,d; // Here c and d are the Local variable
5
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
2 : Global Variable : Global variables are defined outside the main() function and multible functions can
use these variables.
Return Statement : The return statement may or may not send back any values to the calling function(main
program).
A function can contain more than one return statements, when the return value can return values
based on certain condition.
If a function can return some values other than int type, then we must specify the data type to be
return.
Ex : #define PI 3.142
#define MAX_MARKS 100
#define MIN_MARKS 35
Where identifier is the name of macro function with the list of macro formal parameters
var1,var2,var3,…varn like the formal parameters in a function definition.
80 90 45 99 100
1000 1002 1004 1006 1008
9
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
To refer an item in the array, we specify the name of the array along with position of the item. The
position of the item must be written within squre brackets( [] ). The position of the item enclosed within squre
brackets is called “subscript” or “index”.
marks of 5 students are stored. The marks of each student can be accessed as
Advantages of an Array :
1:
2:
3:
Where
- data_type : can be a basic data type such as int,float , char etc.
- array_name : is the name of the array.
- Size : size should be evaluated to a positive integer only. It can also be an integer constants.
Ex : int a[5];
80 90 45 99 100
1000 1002 1004 1006 1008
16) How to initialization of array with example?
10
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Initilization of Array : assigning the required information to a variable before processing is called
initialization.we can initialize the individual elements of an array. Array elements can be initialized at the time
of declaration.
Syntax : data_type array_name[size]={v1,v2,…vn};
Where
- data_type : can be a basic data type such as int,float , char etc.
- array_name : is the name of the array.
- Size : size should be evaluated to a positive integer only. It can also be an integer constants.
- v1,v2,..vn are the values and should be enclosed within ‘{‘ and ‘}’ separated by commas. The value v1
is assigned to the 0th location, v2 is assigned to 1st location and so on.
Ex:
int a[5]={10,20,30,40,50};
char b[8] = {‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
int a[5]={10,15};
char b[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
char b[] = “COMPUTER”;
1 : Built-in Functions (OR) Library Functions : The standard C library is a collection of various types of
functions which perform some standared and predefined tasks. These function which part of the C compiler that
have been written for general purpose are called library functions. They are alo called built-in functions.
11
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Examples :
1 : The function sqrt() is used for to find squre root of a given number.
2 : The function scanf() is used to read data from the keyboard.
3 : The function printf() is used to print the result on the display.
Advantages :
1 : The programmer’s job is made easier because the functions are already available.
2 : The library functions can be used whenever required.
Disadvantages : The standared library functions are limited, programmes can not completely on these library
functions. The programmer has to write his own programs.
This disadvantages of standred library functions can be overcome by using user-defined function.
Ex : #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n=36,s;
clrscr();
s=sqrt(n);
printf(“Squre root of %f:”,s);
getch();
}
2 : User Defined Functions : These functions which are written by the programmer to do some specific tasks
are called User Defined Functions.
1 : Reusability : The function write once and use many times is called Reusability.
2 : Modular Program Appraoch : A large program is divided into smaller sub programs . so that each sub
program performs a specific task. This approach makes the program development more manageable.
3 : Efficiency : means by avoiding redundant instructions the size of program can be reduced. Which increase
efficiency of program.
4 : Function sharing : A function can be shared by many programmers.
12
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2) Explain category of functions with examples?
CATEGORY OF FUNCTIONS : A function, depending on whether arguments are present or not and
whether a value is returned or not. The following are the function prototypes.
1 : Functions with no Parameters and no Return Values.
2 : Functions with no Parameters and Return Values.
3 : Functions with Parameters and no Return Values.
4 : Functions with Parameters and Return Values.
1 : In this category, there is no data transfer between the calling function and called function.
2 : But there is flow of control from calling function to the called function.
3 : When no parameters are there , the function cannot receive any value from the calling function.
4: When the function does not return a value, the calling function cannot receive any value from the called
function.
Ex #include<stdio.h>
#include<conio.h>
void sum();
void main()
{
sum();
getch();
}
void sum()
{
int a,b,c;
printf("enter the values of a and b");
scanf("%d%d",&a,&b);
c=a+b;
printf("sum=%d",c);
}
2 : Functions with no Parameters and Return Values.
1 : In this category, there is no data transfer between the calling function and called function.
2 : But there is data transfer from called function to the calling function.
3 : When no parameters are there , the function cannot receive any values from the calling function.
4: When the function returns a value, the calling function receives one value from the called function.
Ex : #include<stdio.h>
13
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<conio.h>
int sum();
void main()
{
int c;
clrscr();
c=sum();
printf("sum=%d",c);
getch();
}
int sum()
{
int a,b,c;
printf("enter the values of a and b");
scanf("%d%d",&a,&b);
c=a+b;
return c;
}
1 : In this category, there is data transfer from the calling function to the called function using parameters.
2 : But there is no data transfer from called function to the calling function.
3 : When parameters are there , the function can receive any values from the calling function.
4: When the function does not return a value, the calling function cannot receive any value from the called
function.
Ex : #include<stdio.h>
#include<conio.h>
void sum(int a,int b);
void main()
{
int m,n;
clrscr();
printf("Enter m and n values:");
scanf("%d%d",&m,&n);
sum(m,n);
getch();
}
void sum(int a,int b)
{
int c;
c=a+b;
printf("sum=%d",c);
14
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
1 : In this category, there is data transfer from the calling function to the called function using parameters.
2 : But there is no data transfer from called function to the calling function.
3 : When parameters are there , the function can receive any values from the calling function.
4: When the function returns a value, the calling function receive a value from the called function.
Ex :
#include<stdio.h>
#include<conio.h>
int sum(int a,int b);
void main()
{
int m,n,c;
clrscr();
printf("Enter m and n values");
scanf("%d%d",&m,&n);
c=sum(m,n);
printf("sum=%d",c);
getch();
}
int sum(int a,int b)
{
int c;
c=a+b;
return c;
}
1 : Pass by value (OR) Call by value : When a function is called with actual parameters, the values of actual
parameters are copied into formal parameters. If the values of the formal parametes changes in the function, the
15
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
values of the actual parameters are not changed. This way of passing parameters is called pass by value or call
by value.
Ex :
#include<stdio.h>
#include<conio.h>
void swap(int a,int j);
void main()
{
int i,j;
printf("Enter i and j values:");
scanf("%d%d",&i,&j);
printf("Before swapping:%d%d\n",i,j);
swap(i,j);
printf("After swapping:%d%d\n",i,j);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
2 : Pass by reference (OR) Call by Reference :
In pass by reference, a function is called with addresses of actual parameters. In the function header, the
formal parameters receive the addresses of actual parameters. Now the formal parameters do not contain values,
instead they contain addresses. Any variable if it contains an address, it is called a pointer variable. Using
pointer variables, the values of the actual parameters can be changed. This way of passing parameters is called
call by reference or pass by reference.
Ex : #include<stdio.h>
16
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<conio.h>
void swap(int a,int j);
void main()
{
int i,j;
printf("Enter i and j values:");
scanf("%d%d",&i,&j);
printf("Before swapping:%d%d\n",i,j);
swap(&i ,&j);
printf("After swapping:%d%d\n",i,j);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
17
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The calling and called function are two separate entities, they need to communicate to exchange
data. The data flow between the calling function and called functions can be divided into 3 strategies.
1 : Downward Function : In downward communication, the calling function sends data to the called function.
In this strategy, the values of the calling function are copied into the called function. The called function may
changes the values passed, but the original values in the calling function can not be changed.
In Downward communication in C is a pefect solution for the Call by value (OR) Pass by value.
Downward communication is one-way communication. The calling function can send data to the
called function, but the called function cannto send any data to the calling function.
Ex : #include<stdio.h>
void downFun(int x,int y);
void main()
{
int a=5;
downFun(a,15);
printf(“%d”,a);
}
void downFun(int x,int y)
{
x=x+y;
}
2 : Upward Function : Upward communication, when the called function sends data back to the calling
function without receiving any data from it.
18
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
In Upward communication in C is a pefect solution for the Call by reference (OR) Pass by
reference.
Ex : #include<stdio.h>
void upFun(int *m,int *n);
void main()
{
int a=10,b=20;
upFun(&a,&b);
printf(“%d%d”,a,b);
}
void upFun(int *m,int *n)
{
*m=40;
*n=60;
}
Ex : #include<stdio.h>
void upFun(int *m,int *n);
void main()
{
int a=10,b=20;
upFun(&a,&b);
printf(“%d%d”,a,b);
}
void upFun(int *m,int *n)
{
*m=*m+40;
*n=*n / *m;
}
19
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5) Explain storage classes with examples?
STORAGE CLASSES
Storage : Any variable declared in a program can be stored either in memory or registers. Registers are small
amount of storage in CPU. The data stored in registers has fast acess compared to data stored in memory.
Storage class of a variable gives information about the location of the variable in which it is
stored; initial value of the variable, if storage class is not specified; scope of the variable; life of the variable.
Ex : void function1();
void function2(); OUTPUT
void main() 10
{ 0
int x=100; 100
function2();
printf(“%d”,x);
}
void function1()
{
int x=10;
printf(“%d”,x);
20
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
void function2()
{
int x=0;
function1();
printf(“%d”,x);
}
2 :Register Srorage class : To define a variable as register storage class, the keyword register is used. If CPU
cannot store the variables in CPU registers, then the variables are assumed as auto and stored in the memory.
When a variable is declared as register, it is stored in the CPU registers. The default value of the variable will be
garbage value. Scope of the variable within the block where it is defined and the life of the variables is until the
control remains within the block.
3 : Static Storage class : When a variable is declared as static, it is stored in the memory. The default value of
the variable will be zero. Scope of the variable is within the block where it is defined and the life of the variable
persists between different function calles.To define a variable as static storage class, the keyword static is used.
A static variable can be initialized only once, it cannot be reinitialized.
Syntax : static data_type variable_name;
static int i;
21
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ex : void demo(); OUTPUT
void main() 20
{ 21
demo(); 22
demo();
demo();
}
void demo()
{
static int i=20;
printf(“%d”,i);
i++;
}
Here A static variable is initialized only once. When the program is compiled. It is never initialized again.
4 : External Storage class : When a varaiable is declared as extern, it is stored in the memory. The default
value is initialized to zero. The scope of the variable is global and the life of the variable is until the program
execution comes to an end. To define a variable as external storage class, the keyword extern is used. An extern
variable is also called as a global variable.
22
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOPE RULES :
Storage Keyword Storage Location LifeTime Initial Value
Class
Automatic Auto Memory Until the end of Garbage value
function or Block
Register register CPU Registers Until the end of Garbage value
function or Block
Static Static Memory Zero
External Extern Memory Zero
PRE-PROCESSOR COMMANDS
Any program execution needs certain steps. They are 1) the C program is written in the editor 2)
compilation 3) linking 4) executable code is generated. But in between these stages there also involves one
more stage. i.e preprocessor. The preprocessor are used to programmer supplied to command to perform a
source program to the compilation.
One of the most important features of C language is preprocessor commands. The preprocessor
commands are always initialized at the beginning of the program. It begins with a symbol # (pound sign). It
can be placed anywhere but it is declared at the beginning before the main() function or any particular function.
23
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 : Macro substitution without arguments : It is a process to substitute the constant or value in the place of an
identifier. It is possible to achieve this with the help of directive or macro definition statement #define.
Ex : #define PI 3.142
#define MAX_MARKS 100
#define MIN_MARKS 35
#include<stdio.h>
#define PI 3.142
void main()
{
int r;
float area;
printf(“Enter the radius of circle”);
scanf(“%d”,&r);
area=PI*r*r;
printf(“the area of a circle is%d”,area);
}
#define A (20*10)
#define B (200-100)
void main()
{
int div;
div=A/B;
printf(“the division of two numbers%d”,div);
}
24
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
Where identifier is the name of macro function with the list of macro formal parameters
var1,var2,var3,…varn like the formal parameters in a function definition.
Nesting of Macros : it is also possible to use one macro in the definition of another macro. This is called as
nesting of macros.
#include<stdio.h>
#include add.c
void main()
{
void add(); //FUNCTION PROTOTYPE/ DECLARATION.
add(); //FUNCTION CALLING
}
25
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The file add1.c contains the function definition as follows.
void add()
{
int a,b,c;
printf(“enter two numbers”);
scanf(%d%d”,&a,&b);
c=a+b;
printf(“c value is:%d”,c);
}
These are used to select a particular segment of code for compilation depending on the condition.
Ex : #define TEST 1
void main()
{
#ifdef TEST
{
printf(“This is compiled”);
}
#else
{
printf(“This is not compiled”);
}
#endif
}
26
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#else
{
ch=’f’;
printf(“This is not compiled”);
}
#endif
}
Array: An array is a collection of similar data items. All the data items of an array are stored in
continuous memory locations in RAM. The elements of an array are of same data type and each item
can be accessed using the same name.
80 90 45 99 100
1000 1002 1004 1006 1008
To refer an item in the array, we specify the name of the array along with position of the item. The
position of the item must be written within squre brackets( [] ). The position of the item enclosed within squre
brackets is called “subscript” or “index”.
marks of 5 students are stored. The marks of each student can be accessed as
27
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Advantages of an Array :
1. It is used to represent multiple data items of same type by using only single name.
2. It can be used to implement other data structures like linked lists, stacks, queues, trees, graphs etc.
3. 2D arrays are used to represent matrices.
Declaration of Array :
Where
Ex : int a[5];
80 90 45 99 100
1000 1002 1004 1006 1008
Initilization of Array : assigning the required information to a variable before processing is called
initialization.we can initialize the individual elements of an array. Array elements can be initialized at the time
of declaration.
Where
1 : Initilizing all specified memory locations : If the number of values to be initialized is equal to size of array.
Arrays can be initialized at the time of declaration. Array elements can be initialized with data items of type
int,float,char, etc.
int a[5]={10,20,30,40,50};
During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and
all these locations are initialized.
10 20 30 40 50
1000 1002 1004 1006 1008
If the size of integer is 2 bytes, 10 bytes will be allocated for the variable a.
29
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C O M P U T E R
Other Examples : char b[5]={‘J’,’B’,’R’,’E’,’C’,’B’};
//error : number of initial values are more than the size of array.
//error : Number of initial values are more than the size of array.
2 : Partial Array Initilization : partial array initialization is possible in C language. If the number of values to
be initialized is less than the size of the array, then the elements are initialized in the order from 0th location.
The remaining locations will be initialized to zero automatically.
int a[5]={10,15};
Eventhough compiler allocates 5 memory locations, using this declaration statement, the
compiler initializes first two locations with 10 and 15, the next set of memory locations are automatically
initialized to zero.
10 15 0 0 0
1000 1002 1004 1006 1008
3 : Initilization without size : consider the declaration along with the initialization
char b[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
In this declaration, eventhough we have not specified exact number of elements to be used in
array b, the array size will be set of the total number of initial values specified. So, the array size will be set to 8
automatically.
The array b is initialized as
30
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C O M P U T E R
4) Array Initilization with a String : consider the declaration with string initialization.
C O M P U T E R
The string “COMPUTER” contin 8 charactes, because it is a string. It always ends with null character. So,
thearray is 9 bytes (i.e string length+1 byte for null character).
accessing of an array:
accessing of array values on the display screen.
Syntax: for(i=0;i<n;i++)
printf(“control string”, array_var[i]);
1 : Single Dimentional Array : is also called one-dimentional array) is a linear list consisting of related and
similar data items. In memory all the data items are stored in continuous memory locations one ofter the other.
31
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Where
Ex : int a[5];
80 90 45 99 100
1000 1002 1004 1006 1008
Initilization of Single-Dimentional Arrays : assigning the required information to a variable before processing
is called initialization.we can initialize the individual elements of an array. Array elements can be initialized at
the time of declaration.
Where
32
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 : Intilization without size.
4 : String initialization.
1 : Initilizing all specified memory locations : If the number of values to be initialized is equal to size of array.
Arrays can be initialized at the time of declaration. Array elements can be initialized with data items of type
int,float,char, etc.
int a[5]={10,20,30,40,50};
During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and
all these locations are initialized.
10 20 30 40 50
1000 1002 1004 1006 1008
If the size of integer is 2 bytes, 10 bytes will be allocated for the variable a.
C O M P U T E R
Other Examples : char b[5]={‘J’,’B’,’R’,’E’,’C’,’B’};
//error : number of initial values are more than the size of array.
//error : Number of initial values are more than the size of array.
33
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 : Partial Array Initilization : partial array initialization is possible in C language. If the number of values to
be initialized is less than the size of the array, then the elements are initialized in the order from 0th location.
The remaining locations will be initialized to zero automatically.
int a[5]={10,15};
Eventhough compiler allocates 5 memory locations, using this declaration statement, the
compiler initializes first two locations with 10 and 15, the next set of memory locations are automatically
initialized to zero.
10 15 0 0 0
1000 1002 1004 1006 1008
3 : Initilization without size : consider the declaration along with the initialization
char b[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
In this declaration, eventhough we have not specified exact number of elements to be used in
array b, the array size will be set of the total number of initial values specified. So, the array size will be set to 8
automatically.
The array b is initialized as
C O M P U T E R
34
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4) Array Initilization with a String : consider the declaration with string initialization.
C O M P U T E R
The string “COMPUTER” contin 8 charactes, because it is a string. It always ends with null character. So,
thearray is 9 bytes (i.e string length+1 byte for null character).
Represents a two-dimensional array consisting of 5 rows and 3 coloums. So the total number of elements
which can be stored in this array are 5*3 i.e, 15.
Declaration of Two-Dimentioal Array :
Syntax : type array_name[row-size][column_size];
Where,
1 : type : represents the data type of array elements.
2 : array_name : is name of the array.
3 : row_size : represents the number of rows in array.
4 : column_size : represents number of coloumns of the array.
Both row-size and coloumn_size must be integer constants.
Examples :
1 : int xyz[2][5];
Here, xyz is an array consisting of 2 rows with each row consisting of5 integer constants.
2 : float a[5][5];
Here, a is an array consisting of 5 rows with each row consisting of 5 floating point numbers.
35
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here , first element of each row is initialized to zero while other elements have some garbase values.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,m,n;
clrscr();
printf("enter the rows and colums of two matrics:\n");
scanf("%d%d",&m,&n);
printf("\nenter the elements of A matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
}
printf("\nenter the elements of B matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\nThe elements of A matrics");
36
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe elements of B matrics");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe additon of two matrics");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("\t%d",c[i][j]);
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,m,n,k,r,s;
clrscr();
printf("enter the rows and colums of A matrics:\n");
scanf("%d%d",&m,&n);
printf("enter the rows and colums of B matrics:\n");
scanf("%d%d",&r,&s);
printf("\nenter the elements of A matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
37
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
printf("\nenter the elements of B matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\nThe elements of A matrics");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe elements of B matrics");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
38