Data Structure and c Part-5

21
Data Structure and c Part-5

description

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]; Salary2[15]; Salary3[20]; Salary4[25]. - PowerPoint PPT Presentation

Transcript of Data Structure and c Part-5

Page 1: Data Structure and c Part-5

Data Structure and cPart-5

Page 2: 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]:

Page 3: Data Structure and c Part-5

Salary 1 [10]; Salary 2[15]; Salary 3[20]; Salary 4[25]

Page 4: Data Structure and c Part-5

Initializing arrayIt 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}

Page 5: Data Structure and c Part-5

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}

Page 6: Data Structure and c Part-5

Three several types of Arrays

Page 7: Data Structure and c Part-5

One-Dimensional array 1-D

Example:-One dimensional array

int * [6];Syntax;

data type array name [size];

Page 8: Data Structure and c Part-5

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];

Page 9: Data Structure and c Part-5

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” ;

Page 10: Data Structure and c Part-5

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.

Page 11: Data Structure and c Part-5

Types of Storage Class

Page 12: Data Structure and c Part-5

Automatic storage class

Storage - MemoryDefault initial value - Garbage valueScope - Local to the block in

which the variable is definedLife - Till the control remains

within the block in which it is defined The keyword for this storage class is auto.

Page 13: Data Structure and c Part-5

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:111

Page 14: Data Structure and c Part-5

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 is defined.

Page 15: Data Structure and c Part-5

Example of Code Program

Example:main ( ){

register int i;for (i=1;<=10;i++)print (“%d\t”,i);getch ( );

}

The output is1 2 3 4 5 6 7 8 9 10

Page 16: Data Structure and c Part-5

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 -MemoryDefault initial value -ZeroScope -Local to the block in

which the variable is definedLife -Value of the variable

persists between different Function calls.

Page 17: Data Structure and c Part-5

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;}

Page 18: Data Structure and c Part-5

External storage class

The features of a variable whose storage class has been defined as extern are as follows:Storage -MemoryDefault initial value -ZeroScope -GlobalLife -As long as the program execution doesn’t come to an end

Page 19: Data Structure and c Part-5

Example of Code ProgramExample:

int main ( ){

extern int z;

int i;………

………

return 0 ;}

Page 20: Data Structure and c Part-5

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:-

Page 21: Data Structure and c Part-5

printf( )

scanf( )

getchar( )

getch( )