CSC270: Miscellaneous C

Pre- and Post- Increment and Decrement

[King 4.3] i++; is a statement, but can also be used in an expression where a *value* is needed. The value of i++ is the value of i, but i is incremented after its value is used. i = 5; printf( "%d\n", i ); --> 5 printf( "%d\n", i++ ); --> 5 printf( "%d\n", i ); --> 6 With the expression ++i, i is incremented *before* its value is used. i = 5; printf( "%d\n", i ); --> 5 printf( "%d\n", ++i ); --> 6 printf( "%d\n", i ); --> 6 We can also decrement things with i-- and --i, which decrement after and before the value of i is used, respectively. When the ++ or -- appears before the variable it is called `pre-increment' or `pre-decrement'. When ++ or -- appear after the variable it is called `post-increment' or `post-decrement'.

Conditional Expressions

[King page 72] C allows us to incorporate an if-then-else into an expression. The conditional expression expr1 ? expr2 : expr3 first evaluates expr1. If it's true then the value of the whole thing is the value of expr2. If it's not true then the value is that of expr3. For example, (a < b) ? 4 : i++ has value 4 if a < b and value i if a >= b. Note that i is post-incremented if a >= b. This is most often used in assignments: x = (d != 0) ? n / d : ((n > 0) ? MAXFLOAT : -MAXFLOAT); Here, x is assigned the value of n/d if d is not zero. If d is zero, the value assigned to x is that of the *second* conditional expression, which is MAXFLOAT if n is positive and -MAXFLOAT if n is negative. The MAXFLOAT constant is defined in the value.h header file with #include <value.h>

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 * We'll discuss the ampersand more when we talk about pointers. Reading a float: float x; scanf( "%f", &x ); Reading more than one thing: int i; float x; scanf( "%d %f", &i, &f );

Loops and `break'

[King 6.4] The `break' statement causes the program to break out of the nearest enclosing loop. while (1) { scanf( "%d", &i ); if (i == 0) break; } printf( "0 was found\n" ); The loop above keeps executing since the conditional expression (1) is always true (in C, 1 = true and 0 = false). As soon as an integer zero is read from the input, the program break out of the loop and prints the message. `break' only jumps out of the nearest enclosing loop, as shown by the output of the following code. for (i=0; i<3; i++) for (j=0; j<99; j++) { if (j == 2) break; printf( "i = %d, j = %d\n", i, j ); } } --> i = 0, j = 0 i = 0, j = 1 i = 1, j = 0 i = 1, j = 1 i = 2, j = 0 i = 2, j = 1 Remember the difference between `=' and `==' !