1 5 10 Random Number Generation rand function
1 5. 10 Random Number Generation § rand function – Load <stdlib. h> – Returns "random" number between 0 - 32767 i = rand(); – Pseudorandom - Preset sequence of "random" numbers - Same sequence for every function call § Scaling – To get a random number between 1 and n 1 + ( rand() % n ) - rand() % n returns a number between 0 and n - 1 - Add 1 to make random number between 1 and n 1 + ( rand() % 6) number between 1 and 6 2007 Pearson Education, Inc. All rights reserved.
Outline 2 fig 05_07. c 2007 Pearson Education, Inc. All rights reserved.
Outline 3 fig 05_08. c (1 of 3 ) 2007 Pearson Education, Inc. All rights reserved.
Outline 4 fig 05_08. c (2 of 3 ) 2007 Pearson Education, Inc. All rights reserved.
Outline 5 fig 05_08. c (3 of 3 ) 2007 Pearson Education, Inc. All rights reserved.
6 5. 12 Storage Classes § Storage class specifiers – Storage duration = Lifetime how long an object exists in memory – Scope – where object can be referenced in program – Linkage – specifies the files in which an identifier is known (more in Chapter 14) § Automatic storage – Object created and destroyed within its block – auto: default for local variables auto double x, y; – register: tries to put variable into high-speed registers - Can only be used for automatic variables register int counter = 1; 2007 Pearson Education, Inc. All rights reserved.
7 5. 12 Storage Classes § Static storage – Variables exist for entire program execution – Default value of zero – static: local variables defined in functions. - Keep value after function ends - Only known in their own function – extern: default for global variables and functions - Known in any function 2007 Pearson Education, Inc. All rights reserved.
8 Variable types § Local variable: scope=lifetime Local variables are declared within the body of a function, and can only be used within that function. § Static variable: scope<lifetime Another class of local variable is the static type. It is specified by the keyword static in the variable declaration. The most striking difference from a non-static local variable is, a static variable is not destroyed on exit from the function. § Global variable: scope =<lifetime A global variable declaration looks normal, but is located outside any of the program's functions. So it is accessible to all functions. 2007 Pearson Education, Inc. All rights reserved.
9 5. 13 Scope Rules § File scope – – Identifier defined outside function, known in all functions Used for global variables, function definitions, function prototypes § Function scope – Can only be referenced inside a function body – Used only for labels (start: , case: , etc. ) 2007 Pearson Education, Inc. All rights reserved.
10 5. 13 Scope Rules § Block scope – Identifier declared inside a block - Block scope begins at definition, ends at right brace – Used for variables, function parameters (local variables of function) – Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block § Function prototype scope – Used for identifiers in parameter list 2007 Pearson Education, Inc. All rights reserved.
Outline 11 fig 05_12. c (1 of 4 ) 2007 Pearson Education, Inc. All rights reserved.
Outline 12 fig 05_12. c (2 of 4 ) 2007 Pearson Education, Inc. All rights reserved.
Outline 13 fig 05_12. c (3 of 4 ) 2007 Pearson Education, Inc. All rights reserved.
Outline 14 fig 05_12. c (4 of 4 ) 2007 Pearson Education, Inc. All rights reserved.
char g = 'S’; void f 1(){ char fred = 'B'; printf( " %cadn", g ); g = fred; 15 } void f 2(char cg){ g=cg; } void main( void ){ char k 1 = 'D‘; char k 2 = ‘d’; f 1(); g = k 1; f 1(); f 2(k 2); printf( " %cadn", g ); } 2007 Pearson Education, Inc. All rights reserved.
int i=33; void ff(){ printf(“n i= %d”, i); } 16 void main(){ int i = 99; int sum; { int i; sum = 0; for ( i=1; i<=10; ++i ) sum+= i*i; } printf( "%dn", sum ); printf( "%dn", i ); ff(); } 2007 Pearson Education, Inc. All rights reserved.
int id = 1; int f 1( int a){ 17 /* What is the output*/ int id = a; return ++id; } void f 2(){ static int a = 4; id = ++a; printf(“n%d - %d”, a, id); } void f 3(int b){ f 2(); id = f 1(b); f 2(); id = f 1(b); } void main(){ int c = 10; { int c; c=11; f 3(c); c = id; printf(“n%d”, c, id); } printf(“n%d”, c, id); } 2007 Pearson Education, Inc. All rights reserved.
int id = 1; int f 1( int a){ int id = a; 18 /*ASS - What is the output*/ void main(){ int c = 10; return ++id; } void f 2(){ static int a = 4; id = ++a; printf(“n%d - %d”, a, id); } void f 3(int b){ f 2(); id = f 1(b); { int id = 9; id = f 1(b); printf(“n%d - %d”, id, b); } f 2(); } { int c; c=11; f 3(c); c = id; printf(“n%d”, c, id); } printf(“n%d”, c, id); } 2007 Pearson Education, Inc. All rights reserved.
- Slides: 18