Types and Variables
Computations with ints and doubles
-
Include the following code segment in a C program, compile the program,
and observe the results:
int a = 5; int b = 6; int c = a/b; int d = b/a; int e = (a + b) / 10; int f = (a + b) % 10; double x = (a + b) / 10; double y = (a + b) / 10.0; printf ("a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, x=%lf, y=%lf\n", a, b, c, d, e, f, x, y);- Review what is printed and explain each result.
-
What happens if one changes the computation for
variables
fandyto(a + b) % 10.0;? Explain why the compiler produces this result.
Observation: You may recall there is a fourth
type, float, that was not involved in Step
1. doubles are often preferred to floats,
because a double is stored more accurately than
a float. Also, constants, such as 3.14 are
almost always stored as doubles in C. An exception
arises when memory is scarce, and one wants to use as little space as
possible within a computer's memory. With the size of main memory in
today's computer, however, size often is not a consideration,
and doubles typically are used rather
than floats.
We will discuss details of the various data types, including
both doubles and floats, in a few weeks.
Assignment and Casting
In Step 1, the computations a/b and b/a
were performed with ints. That is, the result was an
integer, and any fraction of an integer was discarded. Also, in the
case of computing y, the presence of
the double 10.0 caused the machine to convert (a +
b) to double and then perform division. In this case, the
implicit type 10.0 caused conversion to double before the
computation took place.
In C, we can assign values implicitly to int and
double variables, but we also can explicitly
ask the computer to change the data type before an operation. This explicit
specification of a change in type is called casting
-
Include the following code segment in a C program, compile the program, and
observe the results:
int k = 5.0; int m = 7.7; double n = 5; int p = k/m; double q = k/m; printf ("k = %d, m=%d, n=%lf, p=%d, q=%lf\n", k, m, n, p, q); double r = (double) (k/m); double s = (double) k / m; double t = k / (double) m; double u = (double) k / (double) m; printf ("r=%lf, s=%lf, t=%lf, u=%lf\n", r, s, t, u);- Review what is printed.
-
For each result printed, explain whether the division is done
with
ints ordoubles and which, if any, numbers are converted todoubles when.
Observation: Sometimes there can be a danger in casting. Types have sizes. If you try to put a number that is bigger than the size of the type you are casting it to, you may get undesired results.
Operator Precedence
After deciding what operator you will use, you need to put them in certain order or in parentheses because the program will be read in a certain order and so the operators will be applied in a certain order.
-
Copy this code in a program. What do you think the result will be? Test
this out by printing the value and explain the results.
int v; v = 5 + 4 * 3 + 1; -
Copy the code below into the same program. Think about your
expected result and then see what the program prints. Explain the results.
int w; w = (5 + 4) * (3 + 1);
Incrementing and Decrementing Numbers
-
Examine the output of the following code, in which variables are
incremented as the only part of an expression.
In this code theint a = 1; int b = 7; printf ("a = %d, b = %d\n", a, b); a++; ++b; printf ("a = %d, b = %d\n", a, b);++operation is the only part of the statement. Note and explain what results are obtained. -
Now consider the following example that combines the increment operators
with other activities (e.g., assignment)
Examine the output and explain the result. For example, explain any difference observed between the two increment operators in this context.int a,b,c; a = 0; b = a++; c = ++a; printf(" a = %d b = %d c = %d\n", a,b,c); a = 0; b = ++a; c = a++; printf(" a = %d b = %d c = %d\n", a,b,c); -
The next block combines the increment operators within a print and within
arithmetic expressions. Examine the output to determine whether or not
there is a difference between the two increment operators in this context.
int r, s; r = 5; s = 7; printf(" r = %d s = %d\n", ++r, s++); printf(" r = %d s = %d\n", r, s); int t = r++ + s++; printf(" r = %d s = %d, t = %d\n", r, s, t); int u = ++r + ++s; printf(" r = %d s = %d, u = %d\n", r, s, u); -
In addition to the increment operators,
++aanda++, C provides pre- and post-decrement,--aanda--, that subtract one from the variable.Repeat Step 7, replacing
++by--throughout. Again, examine the output and explain what is printed.
Conclusion: When combined with other operations, the increment operations ++a (pre-increment) and a++ (post-increment) cause different values to be used in processing. This observation yields the following programming observation and suggestion:
- The use of pre- and post-increment operators in the context of other operations is widely observed to be error prone.
- Avoid combining pre- and post-increment operators with activities in the same statement.
Characters
-
The
program
ascii-print.cprints many of the ASCII values along with their corresponding characters. Codes smaller than 32 correspond to characters that are not printable (e.g., a backspace, tab, line feed, return, etc.). Similarly, codes larger than 126 may not print properly in terminal windows (e.g., code 177 is ±, code 181 is µ, and code 216 is Ø). Since only codes between 32 and 126 (inclusive) reliably print in a terminal window, the program uses aforloop that starts with code 32, the code for a space character, and ends at 126.- What is the corresponding character to the ASCII value 85?
- What is the ASCII value for the character 'A'?
- What is the ASCII value for the character 'a'?
- What is the integer difference between the values of 'A' and 'a' ?
- What relationship can you observed for the codes for 'a', 'b', 'c', 'd', and the other lower case letters?
- What relationship can you observed for the codes for '0', '1', '2', '3', and the other digit characters?
- What relationship can you observed for the codes for 'A', 'B', 'C', 'D', and the other upper case letters?
-
What code is used for the character
0? (Note that the code forchar 0is not zero!)
Adding ints to chars
Because characters are stored according to their integer codes, it is
possible to add an integer value to a char value.
-
Include the following lines of code within a program:
char ch = 'a'; ch = ch + 7; printf("ch + 7 = '%c'\n", ch);- Save, compile, and run the program again.
- What is the value of
ch?
-
Add 7 to
chagain (you can just copy the last two lines again) and a print out the value.- What is printed?
- Is that what you expected?
-
In C, a
charis considered a type of smallint, so it is possible to assign acharvariable an integer value. Setchto 48 and print out the result:
Explain what is printed.ch = 48; printf("char ch = '%c'\n", ch); -
Set
chequal to the integer 0 and print it again.You may notice the print statement doesn't print out
chas being zero; rather the two single quotes are printed next to each other. The reason is that the character code for the integer 0 is called a "NULL"; when printed the cursor does not move in any way—effectively printing of the character for code 0 is ignored in the output.
