210 likes | 329 Views
Data Structure and c Part-5. Mean. An array Symbol [] An array is a group of related data items that stare a common name. Example:- Salary [10]: . Salary 1 [10]; Salary 2[15]; Salary 3[20]; Salary 4[25].
E N D
Mean An array Symbol [] An array is a group of related data items that stare a common name. Example:- • Salary [10]:
Salary 1 [10]; Salary 2[15]; Salary 3[20]; Salary 4[25]
Initializing array It have to specify the index of the subscript with the square brackets. Syntax; Storage class data type array [size] = {value1, value2, value3} Example:- Int num [5] = {3,4,6,7,10}
Unsized Array The compiler will count the number of initialize and substitute that number of the size of the array Example:- Int even [ ] = {2,4,6,8,10} Int Odd [ ] = {1,3,5,7,9}
One-Dimensional array 1-D Example:- One dimensional array int * [6]; Syntax; data type array name [size];
Two dimensional arrays2-D • Two dimensional arrays is also called matrix • It has two subscripts Syntax; Storage class data type array [s1] [s2]; Example:- Int X [i]; Int Y [i]; OR int a [2] [2];
Array of String It string is defined as a null terminated character array. An array should be defined with an additional byte for holding this null character. Syntax; Char array name [size]; Example:- Char name [6] = “india” ;
STORAGE CLASSES • Storage Classes define a variable one needs to mention not only its ‘type’. • But also its ‘storage class’ Storage class refers to the permanence of a variable.
Automatic storage class • Storage - Memory • Default initial value - Garbage value • Scope - Local to the block in which the variable isdefined • Life - Till the control remains within the block in which it is defined The keyword for this storage class is auto.
Example of Code Program Example: main ( ) { increment ( ); increment ( ); increment ( ); getch ( ); } Increment ( ); { auto int i = 1; print (“%d\n”,i); i = i+1; } The output of the program is: 1 1 1
Register storage class • Storage - CPU registers. • Default initial value - Garbage value. • Scope - Local to the block in which the variable is defined. • Life - Till the control remains within the block in it isdefined.
Example of Code Program Example: main ( ) { register int i; for (i=1;<=10;i++) print (“%d\t”,i); getch ( ); } The output is 1 2 3 4 5 6 7 8 9 10
Static storage class • A storage class with persistent duration is static. • A static variable declared inside a function retains its value after the function terminates. • Storage -Memory • Default initial value -Zero • Scope -Local to the block in which the variable is defined • Life -Value of the variable persists between differentFunction calls.
Example of Code Program Example: # include <stdio.h> main ( ) { increment ( ); increment ( ); increment ( ); getch ( ); } increment ( ); { static int i = 1; print (“%d\n”,i); i = i+1; }
External storage class The features of a variable whose storage class has been defined as extern are as follows: • Storage -Memory • Default initial value -Zero • Scope -Global • Life -As long as the program execution doesn’tcome to an end
Example of Code Program Example: int main ( ) { extern int z; int i; ……… ……… return 0 ; }
FUNCTIONS of TYPES Functions are the building blocks of the C programming language. The most important of the C functions is the main( ) function. Some of the other functions are given below:-
printf( ) • scanf( ) • getchar( ) • getch( )