Download presentation
Presentation is loading. Please wait.
Published byBrianna Burns Modified over 9 years ago
1
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does not retain its value after exit from the function. –External (extern): Global variables. Same named local variables get precedence over global variables.
2
Program structure –Static (static): retain their values throughout the life of the program, but can be accessed only within the function. (I.e. fibonacci series) –Register (register)
3
Multifile programs An external function will be recognized throughout the entire program, whereas a static function will be recognized only within the file in which it is defined. First file extern void output(void); main() { output(); } Second file extern void output(void) { printf(“Hello World”); }
4
Multifile programs External variables can be defined in one file and accessed in another file. First file int a=1,b=2,c=3; Main() { … } Second File extern int a,b,c;
5
Array To represent multiple data items that have common characteristics All share the same name, but indices are different A one dimensional array definition may be expressed as storage-class data-type array[expression]; int x[100]; static char message[MAX];
6
Array definitions Array definitions can include the assignment of initial values if desired int digits[10]={1,2,3,4,5}; char color[3]={‘R’,’E’,’D’}; Array size need not be specified explicitly when initial values are included as part of an array definition. int digits[]={1,2,3,4,5};
7
Array declarations Following definitions are not same: char color[3]=“RED”;//size is 3,no \0 char color[]=“RED”;//size is 4,\0 included Arrays can be declared (because the array is defined elsewhere in the program) in the same manner as array definition except: –The square brackets may be empty –Initial values cannot be included
8
Array declaration example First file int c[]={1,2,3]; … Second file extern int c[]; …
9
Problems Store some in an array and fine the average of the numbers and deviation. Reverse a string Case conversion Reordering a list of numbers
10
Passing arrays to functions main() {int n; float list[100],avg; …. avg=average(n,list); …} float average(int a,float x[]) { } When a array is passed to a function, the values are not passed to the function. Rather array name is interpreted as the address of the 1 st array element. So the formal arg becomes a pointer.
11
Reordering a list of numbers Nested for loop Call reorder function and pass array as reference Call swap function and pass the values as reference Insertion and deletion of numbers Binary search
12
Multidimensional array float table[10][5];//a table of 10 rows and five columns int values[2][3]={1,2,3,4,5,6}; or int values[2][3]={{1,2,3}, {4,5,6}}; int values[2][3]={{1,2}, {3,4}}; int values[3][4]={1,2,3,4,5,6};
13
Problems Adding two tables of numbers Multiplying two matrices Store marks in a multidimensional array such that 10 students each having 5 courses and each course having 3 exams. Reordering a list of strings
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.