UNIT-2 (1)
UNIT-2 (1)
UNIT-2 (1)
Introduction
When large amount of similar data has to be stored in memory and this data has to be referenced in uniform manner,
some means of representation is required to store and manipulate data efficiently. If we use ordinary variables to
hold each data item, handling of such data will be confusing and lengthy. Hence, we go for arrays which makes this
entire task easier.
One-dimensional array:
It is the simplest form of array in which list of elements are stored in contiguous memory locations and accessed
using only one subscript.
Example:
int abc[10];
here abc is an array of 10 integers.
Declaration of one-dimensional array:
The general format of declaring arrays is as follows:
type array-name [size];
Where, type is the data type of array elements, array-name is the name of array variable and size specifies the
number of elements in the array. The size of array is fixed at compile time and it cannot be changed during
execution of program hence, it is a constant integer value.
e.g. int a[50];
Two-dimensional arrays
When the data must be stored in the form of a matrix we use two-dimensional arrays. For example,
int a[5] [3];
represents a two-dimensional array consisting of 5 rows and 3 columns. So the total number of elements which can
be stored in this array are 5 X 3 i.e.15.
Declaration of two-dimensional array:
As shown in the above example, the general format of declaring a two-dimensional array is:
type array-name[row-size][column-size];
Where type represents the data type of array elements. array-name is name of the array, row-size represents the
number of rows in array and column-size represents number of columns of the array. Both row-size and column size
must be integer constants.
Prepared by : M V B REDDY 20
e.g :
int xyz[2][5];
Here, xyz is an array consisting of two rows with each row consisting of 5 integer elements.
float a[5][5];
Here, a is an array consisting of 5 rows with each row consisting of 5 floating point numbers.
Multi-dimensional arrays:
A three dimensional array can be thought of an array of arrays. It is a collection of two-dimensional arrays.
Representation of 3-D array
nd
2 2-D array
2 1 3
7 10 12
Strings :
A string is an array of characters. Any array of characters defined between double quotation marks is a constant
string.
e.g
“I love my country” is a constant string
Declaration of string variables:
The general format of declaring string variable is as follows:
char string-name [size]
Example :
Char name[20];
The string ‘name’ consists of 20 characters. When the compiler assigns a character string to a character array, it
automatically supplies a null character ‘\0’ at the end of the string.
Prepared by : M V B REDDY 21
Operations on strings
The various operations that can be performed on strings are:
Reading and writing strings
Combining strings together
Copying strings (one string to another)
Comparing strings for equality
Extracting a portion of a string
The C library provides a large number of string handling functions that can be used for performing string
manipulations. These functions are available in the file string.h. This header file is used or included whenever these
functions are used.
Table of strings
Till now we have seen how to read and manipulate individual strings. Now we will see how to declare and use array
to strings.
Prepared by : M V B REDDY 22
“students”,
“I year “};
Functions
A function is a self contained program segment that performs specific and well defined task. The C language
provides two types of functions: Library functions and user defined functions.
Library functions
Library functions are predefined functions which are mostly provided to perform common computations such as
sin(x), cos(x), pow(x,i)(to compute xi), sqrt(x) etc. the code of these functions is not available to the user so they
cannot modify these functions.
These functions allow user to define their own functions according to the requirement of program.
Argument declaration
local variable;
executable _smt 1;
executable_stmt 2;
_______________
_____________
The statement which is used to call a function is known as function call and the actual code of a function is known
as function definition.
Prepared by : M V B REDDY 23
E.g.
void main( )
The function from which another function is called is known as calling function and this another function is known
as called function.
As shown in the program below main( ) is calling function and mul(a,b) is called function.
Function declaration
If a function call is made before its definition then its prototype must be given. In this example mul(a,b); is called
before its definition, so its prototype is given in function main( ). Otherwise this prototype need not be given.
Depending on the location of argument in functions, they are categorized as actual parameters and formal
parameters.
Actual parameters
The parameters which are passed in function call are know as actual parameters. They are defined in calling
functions and they can be expressions, constants or variables.
Prepared by : M V B REDDY 24
Types of functions
Depending on the return value and argument passed, functions can be classified into 4 categories.
Functions with no arguments and no return values:
These are the functions in which no parameters are passed from calling function to called function and no values are
returned from called function to calling function.
These are the functions in which arguments are passed from calling function to called functions but no values are
returned from called function to calling function.
In this no arguments are passed from calling function to called function but values are returned from called function
to calling function.
These are the functions in which parameters are passed from calling function to called function and values are
returned from called function to calling function.
Call by value
When an argument is passed by value, the data in the actual parameters is copied to the formal parameters. Any
modifications done to formal parameters will not be reflected back to actual parameters.
Call by reference :
This is also known as call by address. In this mechanism, the address of actual parameters are passed to formal
parameters. So any changes made to the formal parameters causes change in actual parameters also.
Prepared by : M V B REDDY 25
Passing array as arguments
Array can be passed as arguments. There are two ways of passing arrays as parameters
Nesting of functions
The nesting of functions is permitted in C. As main ( ) is a function which calls another function 1 which inturn calls
fucntion2 which inturn calls function 3 and so on.
Recursion
In the recursive procedure given above, M is the main procedure from which recursive function is called.
Two conditions must be satisfied by a recursive function.
a) A recursive function must always call a simple case of itself.
b) There should be a terminating condition otherwise it will go into infinite loop
Storage classes
When you declare a variable, it is associated with some data type. Each declared variable is associated with some set
rules which determine where the variable can be used., when it can be sued and where it is stored. These features are
defined using the following terms:
Storage :
Prepared by : M V B REDDY 26
Any variable declare in a program can be stored either in memory or registers. Registers are small amounts of
storage in CPU. The data stored in registers has faster access compared to data stored in memory normally,
compilers determine what data is to be stored in registers of CPU.
Scope:
Scope of a variable is the part of program in which it can be used. The different scopes of variables can be :
Function’
Block
File
Storage class
The storage classes are used to control storage, scope and lifetime of variable. Different storage classes available in
C language are
Automatic
External
Static
Register
Prepared by : M V B REDDY 27
QUIZ QUESTIONS
1. int a[5]={1,2,3};
The value of a [4] will be
a) 2 b) 3 c) garbage value d) 0
2. int a[ ]={‘a’,’b’,’c’,’d’}
size of array a is
a) 4 b) 5 c) cannot be determined d)error
7. char S[ ] =”welcome”;
char P[ ] =”world”;
strcat(S,P);
what will be the value of S?
a) welcome world b) world welcome
c) error due to insufficient size d) none
Prepared by : M V B REDDY 28
The value a[1][1] is
a) 1 b) 5 c) 2 d) garbage value
Prepared by : M V B REDDY 29
15. A character array CSE can be initialized as ;
a) char S{ ] =-- "CSE"
b) char S[ ] = {'C'. 'S', 'E'}
c) char S[ ]= { 'C,’S ‘,'E ', ‘\0’)
d) both a and c are correct
Prepared by : M V B REDDY 30
24. Storage class controls
a) life time of variable b) scope of variable
c) storage of variable d) all the above
25. void main ( )
{
if (……)
{
int i,j=10;
printf("%3d",j );
printf ("%3d%3d", i,j);
}
The output will be
a) 5 10 5 b) 5 10 10 c) error d) none
Prepared by : M V B REDDY 31
30. A static variable is one which
a) retains its value throughout the life of program
b) is same as automatic variable
c) cannot be initialized
d) none
31. Static variables are stored in
a) memory b) register c) both a and b d) none
Prepared by : M V B REDDY 32
k = i +j;
pnntf("0/od", k);
The output will be
a) compiler error h) run time error c) 15 d) none
39. void main( )
{
auto int a = 50;
register char b = 'a';
b = b + 5;
printf("% c", b);
}
The output will be
a) f b) a c) error d) none
40. The parameters used in function call are known as
a) formal parameters b) actual parameters
c) user defined parameters d) all the above
Prepared by : M V B REDDY 33
PREVIOUS QUESTIONS
Prepared by : M V B REDDY 34
21. Write a function that will round a floating-point number to an indicated decimal place. For e.g., the
number 17.457 would yield the value 17.46 when it is rounded-off to two decimal places.
22, Distinguish between the following: Actual and formal arguments.
23. Distinguish between the following: Global and local variables
24. Distinguish between the following: Automatic and static variables.
25. Explain in detail about pass by values and pass by reference. Explain with a sample program.
26.What do you mean by functions. Give the structure of the functions and explain about the arguments
and their return values.
27. Write a C Program that uses a function to sort an array of integers.
Prepared by : M V B REDDY 35