CSC270 Sept 30 Lecture II: And even more C




Constants

[King 2.6] Constants can be defined with the directive #define CONSTANT_NAME constant_value Constants are usually written in uppercase letters to make their purpose clear in the program. For example: #define PI 3.14159 #define NUM_INPUTS 100 The constants can be used anywhere in the program: for (i=0; i < NUM_INPUTS; i++) { scanf( "%d", &x ); sum += x; }

Reading input with scanf

[King 2.5] The `scanf' statement reads input: int i; scanf( "%d", &i ); \ / \/ \/ note the ampersand (&) ! format string specifying integer to read Notes on the ampersand: &i means ``address of variable i''. It's the address of the block of memory where i is stored. We must tell scanf the address so that scanf knows *where* to store the value that it reads. * The ampersand is usually, but *not always* necesssary. * Always use the ampersand with numbers. * Forgetting the ampersand sometimes results in a Segmentation fault Reading a float: float x; scanf( "%f", &x ); Reading more than one thing: int i; float x; scanf( "%d %f", &i, &f );

More Functions

[King 9]

Parameters versus Arguments

`Parameters' are what appear in the *definition* of a function, between the parentheses. Above, x and y are parameters. `Arguments' are what appear in the *call* of a function, again between the parentheses. Below, j/3.0 and i are arguments. x = f( j/3.0, i ); Note that parameters are variables names, while arguments are expressions that have a particular value when the function is called.

Call-by-value

When a function is called, C does several things: 1. Compute values for all the arguments. 2. Set each parameter to the value of the corresponding argument. 3. Execute the function body. If an argument is a variable name, the *value* of the variable is passed in to the function, not the variable itself. This means that any changes the function makes to parameters are *not* returned from the function. int square( int x ) { x = x * x; return x; } ... i = 9; j = square( i ); printf( "i = %d, j = %d\n", i, j ); --> i = 9, j = 81 In the example above, the parameter x is given the value of argument i before the function is executed. When executed, the function changes the value of its parameter. However, the change is not brought outside the function.

Variable scope

[King 10.4] Note that the variable name `i' occurs twice in the program below. These are two different local variables, one in `main' and the other in `print_lots'. void print_lots( int n ) { int i; for (i=n; i>0; i--) printf( "." ); printf( "\n" ); } main() { int i; for (i=0; i<20; i++) print_lots( i ); } The `scope' of a variable is the region of the program in which it is recognized. The scope of a variable is the code between the smallest enclosing braces { } around the declaration of the variable. Above, the scope of `i' is the function body of `print_lots' (for one of the variables) and the body of `main' (for the other variable). There is one exception: if these braces contain another set of braces in which the same variable name is declared, then the scope of the variable does not include the region between the inner braces: main() { int i, j; for (i=0; i<10; i++) { for (j=0; j<10; j++) { int i; i = j * 10; printf( "inner i = %d\n", i ); } printf( "-- outer i = %d\n", i ); } } The scope of the outer `i' is the body of `main' EXCEPT that part between the innermost braces. Inside the innermost braces is another variable `i' (same name, different variable) whose scope is the code between the innermost braces.