Upload
anjan-prasad
View
218
Download
0
Embed Size (px)
Citation preview
7/30/2019 c progrmming(unit-III)
1/75
UNIT-III
3.1 arrays- definition, initialization, strings ascharacter arrays, two dimensional andmultidimensional arrays, and variable length arrays.
3.2 pointers- definition, pointer variable, pointer to apointer, memory mapping, arithmetic operations onpointers, relationship between arrays and pointers,pointers as arguments and return type of a function,pointers to a function, array of pointers and pointersto arrays, dynamic memory allocation.
3.3- input output functions, string handling functions.
7/30/2019 c progrmming(unit-III)
2/75
Definition of array Array- array is a collection of similar data types stored
contiguously in memory under the same name.
Array- it is a collection of homogeneous elements. HOW TO CREATE AN ARRAY
Before we create an array, we need to decide on twoproperties:
Element type: what kind of data will your array hold?ints, double, chars? Remember, we can only choose one type.
Array size: how many elements will your array contain?
When we initially write our program, we must pick an arraysize. An array will stay this size throughout the execution ofthe program. In other words, we can change the size of anarray at compile time, but cannot change it at run-time.
7/30/2019 c progrmming(unit-III)
3/75
USING ARRAYS IN C
In C, the arrays can be classified based on howthe data items are arranged for human
understanding Arrays are broadly classified into three
categories,
1. One dimensional arrays
2. Two dimensional arrays
3. Multi dimensional arrays
7/30/2019 c progrmming(unit-III)
4/75
1. ONE DIMENSIONAL ARRAYS
One dimensional array is a linear list consisting of
related and similar data items. In memory all the dataitems are stored in contiguous memory locations one
after the other.
syntax-Datatype array_name[size];
Ex- int a[10];
created an array of 10 integersfloat x[5];
created an array of 5 floating point values
double z[8];
created an array of 8 double values.
7/30/2019 c progrmming(unit-III)
5/75
Referencing\ accessing array elements Array elements are accessed with subscript, the
number in the brackets following the array name.
This number specifies the elements position in the
array.
The subscript would be 0,1,...... n-1.
7/30/2019 c progrmming(unit-III)
6/75
// C program to read an array elements and
find the sum of the elements.
#include
void main()
{ int a[10];
int i, SIZE, total=0;
printf(\n Enter the size of the array : );
scanf(%d,&size);
for (i = 0; i < SIZE ; i++)
scanf(%d,&a[i]);for (i = 0; i < SIZE ; i++)
total += a[i];
printf("Total of array element values is %d\n", total);
getch();}
7/30/2019 c progrmming(unit-III)
7/75
Few important points about the array
1. an array is a collection of similar elements.
2. the first element in the array is numbered
from 0 to n-1.
3. while declaring an array we have to specifytype and size(dimension) of an array to the
compiler.
4. however big an array all the elements are
always stored in contiguous memory location.
7/30/2019 c progrmming(unit-III)
8/75
Initializing array elements We have four options for initializing one dimensional arrays.
But, first, REMEMBER THIS: Just like regular variables, arrays that have not been initialized maycontain garbage values.
But, now, the stakes are even higher. If you declare an array with 31 elements, and forget to initialize it,you end up with 31 garbage values!
Option 1 Initializing all memory locations
If you know all the data at compile time, you can specify all your data withinbrackets:
int x [5] = {75, 79, 82, 70, 68};
during compilation, 5 contiguous memory locations are reserved by thecompiler for the variable temp and all these locations are initialized as shownin Fig. If the size of integer is 2 bytes, 10 bytes will be allocated for thevariable temp.
x[0] x[1] x[2] x[3] x [4]
1000 1002 1004 1006 1008 Address
Figure: Storage Representation of array
7/30/2019 c progrmming(unit-III)
9/75
Option 2 initialization without size
If we omit the size of your array, but specify an initial
set of data, the compiler will automatically determinethe size of your array. This way is referred as
initialization without size.
int X [] = {75, 79, 82, 70, 68};
the array size will be set of the total number of initialvalues specified.
Here, the compiler creates an array of 5 elements.
x[0] x[1] x[2] x[3] x [4]
1000 1002 1004 1006 1008 Address
Figure: Storage Representation of array
7/30/2019 c progrmming(unit-III)
10/75
Option 3 Partial Array Initialization
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 0 th
location. The remaining locations will be initialized to zeroautomatically.
Even though compiler allocates 5 memory locations, using this
declaration statement, the compiler initializes first three
locations with 75,70 and 82,the next set of memory locationsare automatically initialized to 0s by the compiler as shown in
figure
x[0] x[1] x[2] x[3] x [4]
1000 1002 1004 1006 1008 Address
Figure: Storage Representation of array
7/30/2019 c progrmming(unit-III)
11/75
Option 4
If you do not know any data ahead of time, but you want to
initialize everything to 0, just use 0 within { }.
For example:
int x [5] = {0};
x[0] x[1] x[2] x[3] x [4]
1000 1002 1004 1006 1008 Address
Figure: Storage Representation of array
For example:
int x [5] = {5};
x[0] x[1] x[2] x[3] x [4]
1000 1002 1004 1006 1008 Address
Figure: Storage Representation of array
7/30/2019 c progrmming(unit-III)
12/75
Array of characters(string) In c language array of characters are treated as
character string.
The size in the character array represents themaximum number of characters that the string canhold.
For example: char name[6];
Declares name as an character array (string) that canhold a maximum of 6 characters.
SNIST SN
I
S
T
\0
Whenever compiler find array of
characters , it terminates with the
additional character call it as nullcharacter.
so name[5]- contains the null character.
When declaring character arrays, we
must always use one extra space for the
null character.
7/30/2019 c progrmming(unit-III)
13/75
Initialization of character array
char name[]= {s,n,i,s,t,\0};
or char name[]=snist;- in this declaration compilerautomatically inserts the null character.
Bound checking
In c there is no check to see whether the subscriptused for an array exceeds the size of an array or not.
Ex- main()
{
int x[50];for(i=0;i
7/30/2019 c progrmming(unit-III)
14/75
TWO DIMENSIONAL ARRAYS
Whenever you want to store the tabular data use the
two dimensional array.
Example- store the student name, rollno, and
attendance.
The data must be in rows and columns.- matrix Declaration of two dimensional array
data type array_name[rows][columns];
7/30/2019 c progrmming(unit-III)
15/75
2- dimensional arrays Example
char names[3][4];
in the above declaration
char(dataType) specifies type of element in each slot names(array_name) specifies name of the array
[3](rows) specifies number of rows
[4](cols) specifies number of columns
Initialization of two dimensional arrays
An array may be initialized at the time of declaration:
char names [3][4] = {
{J, 'o', 'h', 'n'},
{M, 'a', 'r', 'y'},
{I, 'v', 'a', 'n'}};
While initializing a 2-d array it is necessary to mention the columndimension. so that compiler automatically arrange the elements in rowsand columns.
Row dimension is optional.
In memory all the elements are stored in row major- format.
7/30/2019 c progrmming(unit-III)
16/75
They are stored in memory as
col0 col1 col2
Row0 [0][0] [0][1] [0][2]Row1 [1][0] [1][1] [1][2]
Row2 [2][0] [2][1] [2][2]
7/30/2019 c progrmming(unit-III)
17/75
7/30/2019 c progrmming(unit-III)
18/75
int x[4][3]={{1, 2, 3},
{4, 5, 6},
{7, 8, 9},{10, 11, 12}};
int x[3][2]={1,2,3,4,5,6};
int x[][2]={1,2,3,4,5,6};
int x[2][]={1,2,3,4,5,6};
int x[][]={1,2,3,4,5,6};Never work
Initializing 2D arrays
An array may be initialized at the time of declaration:
I iti li i 2D
7/30/2019 c progrmming(unit-III)
19/75
Initializing 2D arrays
char names[3][4] = {
{ 'J', 'o', 'h', 'n'} ,
{ 'M', 'a', 'r', 'y'} ,{ 'I', 'v', 'a', 'n'}
};
An integer array may be initialized to
all zeros as follows:
This only works for zero.
int nums[3][4] = {0};
nums[0][0] = 16;
printf("%d", nums[1][2]); To access an element of a 2Darray, you need to specify both the
row and the column:
'J' 'o' 'h' 'n'
'M' 'a' 'r' 'y'
is stored:
'J'
'o'
'h''n'
'M'
'a'
'r'
'y'
Address
...
4269
4270
42714272
4273
4274
4275
4276...
Two-dimensional arrays in C are stored in "row-major format":the array is laid out contiguously, one row at a time:
7/30/2019 c progrmming(unit-III)
20/75
3. Multidimensional array
C allows arrays of three or more dimensions.
Syntax
Data type array_name[size1][size2]..[sizen];
Array declarations should understand from right-to-left
int a[10][3][2];
an array of ten arrays of three arrays of two ints
7/30/2019 c progrmming(unit-III)
21/75
int arr[3][4][2] ={
{ { {
{ 2, 4 }, { 7, 6 }, { 8, 9 },
{ 7, 8 }, { 3, 4 }, { 7, 2 },
{ 3, 4 }, { 5, 3 }, { 3, 4 },
{ 5, 6 } { 2, 3 } { 5, 1 }
}, }, }
};
array of three
arrays
Array of 4 rows and 2 columns
7/30/2019 c progrmming(unit-III)
22/75
7/30/2019 c progrmming(unit-III)
23/75
/* write a c program to calculate the addition of two
7/30/2019 c progrmming(unit-III)
24/75
/* write a c program to calculate the addition of two
matrices*/
#include
#include
void main()
{int m,n,i,j,a[10][10],b[10][10],c[10][10];
printf("\n enter the size of the matrix\n");
scanf("%d %d", &m,&n);
printf("enter the elements of matrix a\n");
for(i=0;i
7/30/2019 c progrmming(unit-III)
25/75
/ Fill 2 d array, print out values, sum the array. /#include #define M 3 /* Number of rows */#define N 4 /* Number of columns */int main(void){
int a [ M ] [ N ], i, j, sum = 0;for ( i = 0; i < M; ++i ) /* fill the array */
for (j = 0; j < N, ++j )a [ i ] [ j ] = i + j;
for ( i = 0; i < M; ++i ){ /* print array values */for (j = 0; j < N, ++j )
printf(a [ %d ] [ %d ] = %d , i, j, a[ i ] [ j ]);printf (\n);}for ( i = 0; i < M; ++i ) /* sum the array */
for (j = 0; j < N, ++j )sum += a[ i ] [ j ];
printf(\nsum = %d\n\n);return 0;} Produces the output:
a[ 0 ] [ 0 ] = 0 a[ 0 ] [ 1 ] = 1 a[ 0 ] [ 2 ] = 2 a[ 0 ] [ 3 ] = 3
a[ 1 ] [ 0 ] = 1 a[ 1 ] [ 1 ] = 2 a[ 1 ] [ 2 ] = 3 a[ 1 ] [ 3 ] = 4
a[ 2 ] [ 0 ] = 0 a[ 2 ] [ 1 ] = 3 a[ 2 ] [ 2 ] = 4 a[ 2 ] [ 3 ] = 5
sum = 30
7/30/2019 c progrmming(unit-III)
26/75
Variable length arrays
i
7/30/2019 c progrmming(unit-III)
27/75
pointers
Introduction
Pointers are one of the derived types in C. One of the powerfultool and easy to use once they are mastered.
Some of the advantages of pointers are listed below:
A pointer enables us to access a variable that is defined outside
the function. Pointers are more efficient in handling the data tables.
Pointers reduce the length and complexity of a program.
The use of a pointer array to character strings save data storage
space in memory.
The real power of C lies in the proper use of pointers.
As we know computers use their memory for storing the
7/30/2019 c progrmming(unit-III)
28/75
As we know, computers use their memory for storing theinstructions of a program, as well as the values of the variablesthat are associated with it.
The computers memory is a sequential collection of storage
cells. Each cell can hold one byte of information, has a unique
number associated with it called as address.
The computer addresses are numbered consecutively, startingfrom zero. The last address depends on the memory size.
Let us assume the size of the memory is 64K then,
The total memory locations = 64K
= 64 * 1K
= 64 * 1024 bytes
= 65536 bytes (locations)
So, here the last address is 65535(started with 0).
7/30/2019 c progrmming(unit-III)
29/75
Whenever we declare a variable, the system allocates , anappropriate location to hold the value of the variablesomewhere in the memory,.
Consider the following declaration, int i=10;
This declaration tells the C compiler to perform the followingactivities:
Reserve space in the memory to hold the integer value.
Associate the name i with this memory location.
Store the value 10 at this location.
We can represent is location in the memory by the followingmemory map:
10
i
65524
Variable name
Value at location
Address of variable
7/30/2019 c progrmming(unit-III)
30/75
We can print the address through following program.
main()
{int i=10;
printf(address of i=%u,&i);
printf(value of i=%d,i);
}
65524
10
& operator is used to print the address of the variable.
The format specifier of address is %u (unsigned integer),
the reason is addresses are always positive values.
We can also use %x to know the address of a variable.Note - & operator should not be used for the constant
expressions. &125 (Pointing at constant)
int a[10];
&a (pointing to array name)
&(x+y) (pointing at expressions)
Pointer definition
7/30/2019 c progrmming(unit-III)
31/75
Pointer- definition
A variable Which holds the address of some other variable is called
pointer variable.
A pointer variable should always contain the address only.
The * Operator (asterisk)
It is called as Value at address operator. It returns the value stored at a
particular address.
It is also Known as Indirection or Dereferencing Operator.
accessing a variable through pointerthe following sequence of operations have to be performed ,to use pointers.
1. Declare an ordinary variable.
2. Declare a pointer variable.
3. Initialize a pointer variable (Provide link between pointer
variable and ordinary variable).
4. Access the value of a variable using pointer variable.
D l i i bl
7/30/2019 c progrmming(unit-III)
32/75
Declaring a variable
the syntax for declaring a pointer variable
data type *ptr_name;
This tells the compiler three things about the variable ptr_name.
1. The asterisk(*) tells that the variable ptr_name is a pointer variable.
2.ptr_name needs a memory location.
3.ptr_name points to a variable of type data type.
For example,
int *p;
p is a pointer variable of type integer, which stored the address of the
integer variable. It doesnt mean the p is going to store the integer value
7/30/2019 c progrmming(unit-III)
33/75
Initialize a pointer variable
Once a pointer variable has been declared, it can be made topoint to a variable using statement such as
ptr_name=&var;
Which cause ptr_name to point to var.Now ptr_name containsthe address ofvar. This is known as pointer initialization.
Before a pointer is initialized it should not be used.
Access the value of a variable using pointer variable.
*ptr_name
7/30/2019 c progrmming(unit-III)
34/75
10 8342
i pi
8342 8338
output
The following code illustrates how to declare int ,char and float pointers.
7/30/2019 c progrmming(unit-III)
35/75
The following code illustrates how to declare int ,char and float pointers.
Here we have declared three variables of type int, float and char ,also three
pointer variables points to int, float and char. Remember here pf points to
the value of type float but its type is unsigned integer only
10 15.67Ai c f
6432 4562 8765
P i t t i t
7/30/2019 c progrmming(unit-III)
36/75
Pointer to pointer
A variable which contains the address of a pointer variable is
known as pointer to pointer.
Syntax for declaring pointer to pointerdata type **ptr_ptr;
main()
{
int i=100,*j,**k;j=&i;
k=&j;
printf(\n address of i=%u,&i);
printf(\n address of i=%u,j);
printf(\n address of i=%u,*k);
printf(\n address of j=%u,&j);printf(\n address of j =%u,k);
printf(\n address of k=%u,&k);
printf(\n value of i =%d,i);
Printf(\n valude of i=%d,*(&i));
printf(\n value of i=%d,*j);
Printf(\n value of i=%d,**k);}
100 20022000
k
2000 2002 2004
ji
7/30/2019 c progrmming(unit-III)
37/75
Lab programs- 22/11/2011
Write a c program for multiplication of two matrices.
Cycle- 7.1 a function that takes an integer n as argumnet andreturn 1 if it is prime number and 0 otherwise.
7.2 a function that takes a real number x and positive integer n
asarguments and return xn 7.3 a function that takes a positive integer n as an argument
and returns the nth fibonacci number
7.4 factorial of a number using recursive and non recursive
functions. 7.5 gcd of two numbers using recursion and non recursion.
7.6 Lcm of two numbers using recursion.
P i t t f ti
7/30/2019 c progrmming(unit-III)
38/75
Passing parameters to a function
Parameters to be passed to a function in two ways.
1. call by value
2. call by reference.
1. call by value-
in which values of the actual arguments are copied into the
corresponding formal arguments of the called function. whatever changes made in the formal arguments in the called
function have no effect on the values of the actual arguments.
2. call by reference-
In this method the addresses of the actual arguments in thecalling function are copied into the formal arguments of the
called function.
Whatever changes made in the formal arguments in the called
function can have effect to the value of the actual arguments inthe callin function.
Write a c program to swapping of two numbers using call by
7/30/2019 c progrmming(unit-III)
39/75
Write a c program to swapping of two numbers using call by
value and call by reference.Call by value
void swap(int,int);
void main()
{
int a=10,b=20;
swap(a,b);
printf(\n a and b values after swapping);
printf(\na=%d,b=%d,a,b);
}//main
// function definition
swap(int x,int y)
{
int temp;
temp=x;
x=y;y=temp;
printf(\nx=%d,y=%d,x,y);
}//swap()
Call by reference
void swap(int,int);
void main()
{
int a=10,b=20;
swap(&a,&b);
printf(\n and b values after swapping);
printf(\n a=%d b=%d,a,b);
}//main
//function definition
swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;*y=temp;
}//swap
W it f ti t t d i f f i l h i
7/30/2019 c progrmming(unit-III)
40/75
Write a function to compute are and circumference of a circle, having areaand circumference as pointer arguments and radius as an ordinary argument.
void main()
{int radius;
float area,circum;
printf(\n enter radius of a circle);
scanf(%d,&radius);
areacircum(radius, &area,&circum);
printf(area=%f, area);printf(\n circumference=%f,circum);
}//main
areacircum(int r,float *a, float *c)
{
*a=3.14*r*r;
*c=2*3.14*r;
}
A ith ti ti i t
7/30/2019 c progrmming(unit-III)
41/75
Arithmetic operations on pointers
void main()
{
int i=3,*x;
float j=1.5,*y;
char k=c,*z;
x=&i;
y=&j;
z=&k;
printf(\n address in x=%u,x);
printf(\n address in y=%u,y);
printf(\n address in z= %u,z);
x++;
y++;
z++;
printf(\n address in x=%u,x);printf(\n address in y=%u,y);
printf(\n address in z= %u,z);
}
address in x=65524address in y=65520
address in z=65519
Whenever a pointer is incremented
it points to the immediately next location
of its type.When an integer pointer x is incremented
it points to an address two locations after the
current location.
address in x=65526address in y=65524
address in z=65520
If pointer variable is decremented it points to
the earlier locations by its type.
The following operations we can perform on the pointers.
7/30/2019 c progrmming(unit-III)
42/75
The following operations we can perform on the pointers.
1. add number to a pointer.
void main()
{ int i=10,*j;
j=&i;
printf(j value=%u,j);
j=j+1;
printf(j value=%u,j);
j=j+3;
printf(j value=%u,j);
2. Subtract a number from a pointer
void main()
{
int i=10,*j;
j=&i;
printf(j value=%u,j);
j=j-1;printf(j value=%u,j);
j=j-3;
printf(j value=%u,j);
7/30/2019 c progrmming(unit-III)
43/75
3. Subtraction of one pointer from another pointer
one pointer can be subtracted from the other pointer both
must be point to elements of the same array.
The resultant value would be the number of elements
separating the corresponding array elements.
void main()
{
int a[]={10,20,30,40,50};int *i,*j;
clrscr();
i=&a[1];
j=&a[4];
printf("\nvalue of i=%u",i);
printf("\nvalue of j=%u",j);
printf("\n%d %d",j-i,*j-*i);
getch();
}
Out put3 30
7/30/2019 c progrmming(unit-III)
44/75
4. comparison of two pointer variables.
pointer variables can be compared if both the pointer
variables point to the objects of the same data type.
comparisions can be useful when both pointer variables
point to elements of the same array.
void main()
{int a[]={10,20,30,40,50,60};
int *x,*y;
clrscr();
x=&a[4];
y=&a[5];if(x
7/30/2019 c progrmming(unit-III)
45/75
The following operations should not be applied on thepointers.
1. addition of two pointers.
2. multiplication of a pointer with a constant3. division of a pointer with a constant.
int x=10,y=20;int *i,*j;
i=&x;
j=&y;
printf(%d,i+j);
A d i t
7/30/2019 c progrmming(unit-III)
46/75
Arrays and pointers
Print the elements of the array using
subscript
void main(){
int arr[]={10,20,30,40,50};
int i;
for(i=0;i
7/30/2019 c progrmming(unit-III)
47/75
a g a ay a a g We can pass array elements as arguments to the called function in two ways
1. pass individual array element at a time.
2. pass the entire array at a time
1. passing individual array elements.
we can pass individual array elements to the called functions by either call by
value or call by reference.
Call by value
void main(){
int i;
int arr[]={10,20,30,40,50};
for(i=0;i
7/30/2019 c progrmming(unit-III)
48/75
. pass t e e t e a ay at a t e
Fixed length array- the size of the array is known when the
program is written. int arr[10];
Variable length array- the size of the array is known when theprogram is run. int arr[n];
If u want to pass the fixed length array as argument to the called
function, simply pass the array name to called function.void display(int x[]);
void main()
{
int arr[5]={10,20,30,40,50};
clrscr();display(arr);
getch();
}//main
void display(int x[])
{
int i;
for(i=0;i
7/30/2019 c progrmming(unit-III)
49/75
In variable length array we have to pass array name as well as
size of the array to the called function.void display(int arr[],int size);
void main()
{
int a[10];
int n,i;
clrscr();printf("\n enter the size of the array");
scanf("%d",&n);
for(i=0;i
7/30/2019 c progrmming(unit-III)
50/75
7/30/2019 c progrmming(unit-III)
51/75
/* Demo: 2-D array is an array of arrays */
main( )
{
int s[4][2] = {
{ 1234, 56 },
{ 1212, 33 },
{ 1434, 80 },
{ 1312, 78 }
} ;
int i ;
for ( i = 0 ; i
7/30/2019 c progrmming(unit-III)
52/75
main( )
{
int s[4][2] = {
{ 1234, 56 },
{ 1212, 33 },{ 1434, 80 },
{ 1312, 78 }
} ;
int i, j ;
for ( i = 0 ; i
7/30/2019 c progrmming(unit-III)
53/75
Assign pointer to an 2-d array
void main( )
{
int s[4][2] = {
{ 1,10 },
{ 2, 20 },
{ 3, 30 },
{ 4, 40 }
} ;int ( *p )[2] ;
int i, j, *pint ;
for ( i = 0 ; i
7/30/2019 c progrmming(unit-III)
54/75
Array of pointers
7/30/2019 c progrmming(unit-III)
55/75
Array of pointers
void main( )
{
int *arr[4] ; /* array of integer pointers */int i = 31, j = 5, k = 19, l = 71, m ;
arr[0] = &i ;
arr[1] = &j ;
arr[2] = &k ;
arr[3] = &l ;
for ( m = 0 ; m
7/30/2019 c progrmming(unit-III)
56/75
int a[10];- it is an array of 10 integers
int *a[10];- it is an array of 10 integer
pointers
int (*a)[10];- it is a pointer to an array of 10
integers.
7/30/2019 c progrmming(unit-III)
57/75
Pointer as return type of a function
Called function returns pointer to the calling
function.
Example
void main()
{
int a,b;
Storage classes in c
7/30/2019 c progrmming(unit-III)
58/75
The storage class determines the part of member storage is allocated for anobject and how long the storage allocation continues to exit.
A scope specifies the part of the program which a variable name is visible,
that is the accessibility of the variable by its name. 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. How long variable exist in the program.
There are four types of storage classes:
1) Automatic storage class
2) Register storage class
3) Static storage class 4) External storage class
7/30/2019 c progrmming(unit-III)
59/75
2. Register storage class
7/30/2019 c progrmming(unit-III)
60/75
1. Automatic variables are allocated storage in the main memory of thecomputer; however, for most computers, accessing data in memory isconsiderably slower than processing directly in the CPU.
2. Registers are memory located within the CPU itself where data can be
stored and accessed quickly. Normally, the compiler determines whatdata is to be stored in the registers of the CPU at what times.
3. Thus, register variables provide a certain control over efficiency ofprogram execution.
4. Variables which are used repeatedly or whose access times are critical
may be declared to be of storage class register.5. Variables can be declared with keyword register as register int x;
6. Storage- variable stored in cpu registers rather than memory.
7. Default initial value- garbage value.
8. Scope- local to the block in which they are declared, including any
blocks nested within that block. For this reasons automatic variable arealso called local variable.
9. Life within the block in which the variable is defined.
3.Static storage class
7/30/2019 c progrmming(unit-III)
61/75
1. Variables must be declared with the key word static. static int x;
2. Storage- variable stored in computer memory.
3. Default initial value- zero.
4. Scope- local to the block in which they are declared.5. Lifevalue of the variable persists between the different function calls.
6. Static automatic variables continue to exist even after the block in whichthey are defined terminates. Thus, the value of a static variable in afunction is retained between repeated function calls to the same function.
7. In the case of recursive function calls we use static storage class.
8. Static variables may be initialized in their declarations; however, theinitializes must be constant expressions, and initialization is done onlyonce at compile time when memory is allocated for the static variable.
9. Static and auto variables are differ in their life and initial value.
Difference between static and auto
7/30/2019 c progrmming(unit-III)
62/75
Difference between static and auto
void main()
{
add();
add();
add();
getch();
}//mainadd()
{
auto int i=1;
printf(%d,i);
i++;}//add
void main()
{
add();
add();
add();
getch();
}//mainadd()
{
static int i=1;
printf(%d,i);
i++;}//add
Out put 1 1 1Out put 1 2 3
void main(){
static int i;
printf(%d,i);
}
Out put 0
4 External storage class
7/30/2019 c progrmming(unit-III)
63/75
4. External storage class1. All variables we have seen so far have had limited scope
(the block in which they are declared) and limited lifetimes
(as for automatic variables).2. These variables are declared with keyword extern as
extern int x; ( extern keyword may be omitted).
3. Storage- Memory.
4. Default initial value- zero.
5. Scope- as long as the programs execution.
6. Life- doesnt come to an end.
7. External variables are declared outside the all the functions.So that they are available to all the functions in a program.
7/30/2019 c progrmming(unit-III)
64/75
int i;void main()
{
printf(%d,i);->0add();--------1
add();------2
sub();------1
sub();----0
}//main()
add()
{
i++;
printf(i=%di);
}//add()
Sub(){
i--;
printf(i=%d,i);
}//sub()
extern int x=100;
void main()
{
int x=200;
printf(x=%d,x);
display();
}//
display(){
print(%d,x);
}
Note- local variable has highest
preference than global variable
so that local variable value gets
printed.
7/30/2019 c progrmming(unit-III)
65/75
int x=20;void main()
{
extern int y;
printf(%d %d,x,y);}
int y=20;
- If any variable declared out side of
the main is treated as extern variable/
global variable.
-If any variable declared as global
variable inside of the main that must
be declared with key word extern
and defined outside of the main.
7/30/2019 c progrmming(unit-III)
66/75
Initialization of strings
7/30/2019 c progrmming(unit-III)
67/75
Initialization of strings
char name[10]= hyderabad;or
char name[10]= {h,y,d,e,r,a,b,a,d,\0};
When character array is initialized we must explicitly specify
the null character. Character array initialized without specifying the size also.
char name[]=hyderabad;
Compiler automatically determines the size of this array based
on the no of elements initialized.
Reading string from terminal(keyboard)
7/30/2019 c progrmming(unit-III)
68/75
Reading string from terminal(keyboard)
scanf() can be used to read a string with format
specifier %s.char name[10];
Scanf(%s,name);
Scanf() function assigns the characters typed atkeyboard to array until the enter key is pressed.
once enter key is pressed, scanf() places a \0 character
at the end of an array.
Noteproblem with the scanf() function is that it terminates
input on the first white space it finds. So a string with multiple
words are not accepted by the string.
ex- hyderabad snist would not be accepted.
To read the multiple words of a string use another
f i
7/30/2019 c progrmming(unit-III)
69/75
function gets().
void main()
{
char name[30];
printf(enter the string);
gets(name);}
Writing\ display strings on to a screen
7/30/2019 c progrmming(unit-III)
70/75
Writing\ display strings on to a screen
We can use printf() function to display string on
to a screen with %s format specifier.
Printf(%s,name);
in this case also printf() function cannot print the
multiple words.
like hyderabad snist
To overcome this problem use puts() to display multipl
words.
unlike printf() function, puts() places the cursor to the
next line. \n is not necessar .
7/30/2019 c progrmming(unit-III)
71/75
Use puts and gets functions
void main()
{
char name[30];
puts(enter the string);
gets(name);
puts(name);}
7/30/2019 c progrmming(unit-III)
72/75
/* Program to demonstrate printing of a string */
void main( )
{
char name[ ] = "Klinsman" ;
int i = 0 ;
while ( i
7/30/2019 c progrmming(unit-III)
73/75
void main( ){
char name[ ] = "Klinsman" ;
int i = 0 ;while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;i++ ;
}
}
With the help of null character
7/30/2019 c progrmming(unit-III)
74/75
void main( ){
char name[ ] = "Klinsman" ;
char *ptr ;ptr = name ; /* store base address of string */
while ( *ptr != `\0' )
{
printf ( "%c", *ptr ) ;
ptr++ ;
}
With the help of pointers to print string
7/30/2019 c progrmming(unit-III)
75/75