CS 102
[Schedule]
[Examples] [Programs]
[Notes & Reference] [Syllabus]
[Lab & TA] [Tests]
[Grades]
So far we've seen integer variables. We can use other data types to store other kinds of values.
| Type | Description | Size in Bytes | Format Descriptor |
| char | single characters | 1 | %c |
| int | integer (whole) numbers | 2 | %d |
| float | floating point (non-whole) numbers | 4 | %f |
| double | bigger float numbers | 8 | %f |
| long double | biggest float numbers | 16 | %f |
Note that the sizes (in bytes) shown in the table above are machine dependent. Use the "%e" format specifier for scientific notation, such as in printf("%e", .0314159e2). Beware of type conversions, such as when trying to print a floating point number using an integer ("%d") format specifier. Consult your text for a complete list of format specifiers.
Let's say we want to store a single character in a variable. This might look like:
char c; // Creates a one-byte cell holding a single character
c = 'A'; // Store a character there.
printf("%c", c); // Display it
The first line above declares a character variable c. The next line initializes it to the letter 'A'. Declaration and initialization can be combined into a single step which looks like:
char c = 'A'; // Both create and initialize
Consider what happens when extra space (spaces or carriage returns, known as white space) is left in the input. When using scanf with %d, white space is skipped until an integer is found. However when using scanf with %c for characters, white space is not skipped. For example:
1 2 3 4 5// Program to read and display some characters char c1, c2, c3, c4, c5; int x; scanf ("%c%c%d%c%c%c", &c1, &c2, &x, &c3, &c4, &c5); printf(" %c%c%d%c%c%c\n", c1,c2,x,c3,c4,c5);
And we get the following input and output:
| Input: |
1234 5678 |
| Output: |
1234 56 |
Using the "%c" format specifier interprets an integer value (in one byte) as a character, such as in:
char c1=66;
printf("%c %c\n", 65, c1); // Displays output of: AB
In C++ you would use cin.get(c1)
to read a single character into variable c1. Using cin
by itself skips whitespace in C++.
See the ASCII table or do a "man ascii" on UNIX to see the different numerical codes corresponding to different characters. Note that a numerical value can be expressed in C using different bases. The following all store the same numerical value into character variable c1:
char c1; c1 = 'A'; // Store character 'A', which is really just 65 c1 = 65; // Store the numerical code corresponding to 'A' c1 = '\x41'; // Hexadecimal (base 16) 41 = decimal 65 c1 = '\101'; // Octal (base 8) 101 = decimal 65
There is no corresponding special notation for binary (base 2) numbers. In the ASCII table referenced above, we can see that some of the characters are control characters and perform some action. For instance:
printf("%c", '\n'); // Advance output to beginning of next line
printf("%c", '\b'); // Backup one space in the output
printf("%c", '\t'); // Advance the output to next tab position
Consult your text for other such special character codes. Note that we can use a numerical value to represent the special characters as well. The code below gives the equivalent to the code above, only using the numerical values for the special characters:
printf("%c", 10); // Advance output to beginning of next line
printf("%c", 8); // Backup one space in the output
printf("%c", 9); // Advance the output to next tab position
To illustrate one use of special characters take a look at the examples/chew.c
program in the course account.
/* chew.c
This program prints a message, then backspaces
and deletes the message. Compile with: gcc chew.c
*/
#include <stdio.h>
// #include <stdlib.h> // May be needed for "system" function call
main()
{
int i,j;
printf("Let's chew this up..."); fflush(stdout);
// the fflush forces output to appear and not be buffered
i=0;
while (i<7) {
system( "sleep 1"); // Go to sleep for 1 second
// Delete three characters using BS SP BS
printf("\007"); // Ring bell
j=0;
while (j<3) {
printf("\010"); // Octal for backspace character
printf("%c", 32); // ASCII code for space
printf("\x08"); // Hex for backspace character
j = j + 1;
}
fflush(stdout); // Flush output buffer
i = i + 1;
}
printf(" Yum \n"); // We're done
}
We can force leading white space to be skipped before character input by leaving a space between the opening quote and the percent sign in the format specifier as follows:
char c1;
scanf("%c", &c1); // White space not skipped
scanf(" %c", &c1); // White space is skipped
This is very useful when reading input from the keyboard as characters, since
the carriage-return is itself a character and must be handled explicitly. Putting
the space between the opening quote and the "%" skips white space (which
includes the carriage-return) and so takes care of this problem. See examples
with and without
this space.
[CS Dept] [UIC]
[Prof. Reed]