CSC270 Sept 30 Tutorial: More C
Conversion between types of variables
[King 7.5]
What happens when you assign a float to an int?
int i;
float x;
x = 3.14;
i = x;
C converts the float into an int before assigning it. Typically, C
will truncate any fractional part of the float.
Numeric operators must have arguments of the same type. If we add
an int and a float, the int gets `promoted' to a float before the
addition takes place.
y = i + x;
Watch out for divisions with integers! If you divide integer i by
integer j and *then* assign the result to a float x, the value i/j
will be an integer which is converted to a float *upon* being
assigned. This means that the fractions part of the quotient will be
lost:
int i, j;
float x;
i = 7; j = 2;
x = i/j;
printf( "%g\n", x );
--> 3
You must explicitly convert one of the ints to a float *before* doing
the division. If one argument of the division is a float, the other
will be automatically converted to a float and the quotient will be a
float.
To convert a variable to a new type, add the type name in parentheses
in from of the variable:
x = i / (float) j;
printf( "%g\n", x );
--> 3.5
The explicit transformation into a new type is called a `cast'. A
variable (or expression) is said to be `cast' into a new type.
Arrays
[King 8]
An array hold many elements of the same type. Each element is
associated with an index. In C, indices always start with *zero*.
To declare an array of 10 integers or floats or chars:
int i[10];
float f[10];
float c[10];
The array can be thought of as
i
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| | | | | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
i[0] i[1] i[2] i[3] i[4] i[5] i[6] i[7] i[8] i[9]
The variable `i' denotes the *whole* array, whereas `i[k]' denotes
element k of the array. This is often pronounced ``i at k''. `k' is
called the index. The index can be any integer expression.
To initialize an array:
#define N 10
int a[ N ];
for (i=0; i < N; i++)
a[i] = 0;
To compute Fibonacci numbers (recall f(i) = f(i-1) + f(i-2)):
a[0] = 1;
a[1] = 1;
for (i=2; i < N; i++)
a[i] = a[i-1] + a[i-2];
Note that the array indices go from 0 to N-1, *not* from 1 to N!
The `switch' statement
[King 5.3]
Sometimes, we want to do many different things based upon the many
possible values of an expression. The `switch' statement does this:
switch (expr) {
case value1:
...
case value2:
...
case value3:
...
default:
...
}
1. The `expr' is evaluated *once* and its value is compared with each of
the case values (value1, value2, value3, ... above) in turn, starting
from the top.
2. When the first value that matches the expression is found, the
statments under that case (... above) are executed.
3. If no case value is found, the statements after `default' are
executed. The `default' case doesn't have to be present.
The statements (... above) almost always end with a `break' statement:
switch ( transaction_type ) {
case 1:
printf( "Deposit\n" );
break;
case 2:
printf( "Withdrawl\n" );
break;
case 3:
printf( "Transfer\n" );
break;
case 4:
printf( "Bill payment\n" );
break;
default:
printf( "Unrecognized transaction type: %d\n", transaction_type );
}
If the `break' is left out, the program will continue executing the
statements in the rest of the switch.
For example, if all the `break's were left out of the code and
the transaction_type was 3, the output would be
switch ( transaction_type ) {
case 1:
printf( "Deposit\n" );
case 2:
printf( "Withdrawl\n" );
case 3:
printf( "Transfer\n" );
case 4:
printf( "Bill payment\n" );
default:
printf( "Unrecognized transaction type: %d\n", transaction_type );
}
--> Transfer
Bill payment
Unrecognized transaction type: 3