CS 102 (Fall '03)
[Schedule] [Examples] [Programs] [Notes & Reference] [Syllabus] [Lab & TA] [Tests] [Grades]

Other Variable types, White space, ASCII table,

Other Variable Types: Character, Float, Double, and Long Variables

So far we've seen integer variables. We can use other data types to store other kinds of values.

Type Description Size in Bytes
char single characters 1
int integer (whole) numbers 2
bool boolean (true or false) values 1
float floating point (non-whole) numbers 4
double bigger float numbers 8
long double biggest float numbers 16

Note that the sizes (in bytes) shown in the table above are machine dependent. Beware of type conversions, such as when trying to store a floating point number into an integer variable. Consult your text for more details on this.

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.
cout<< 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 

White Space

Consider what happens when extra space (spaces or carriage returns, known as white space) is left in the input. When using cin, white space (blanks, tabs, carriage-returns) are skipped. For example:

1
2
3
4
5
// Program to read and display some characters
char c1, c2, c3, c4, c5;
int x;
cin>> c1 >> c2 >> x >> c3 >> c4 >> c5;
cout<< c1 << c2 << " " << x*2 << " " << c3 << c4 << c5 << "\n";

And we get the following input and output:

Input:
1234 5678
Output:
12 68 567

To force a value to be interpreted as a character we can use a type cast such as in:

char c1=66;
cout<< (char)65 << c1;	   // Displays output of: AB
Iin C++ you must use cin.get(c1) to read a single character into variable c1. Using cin by itself skips whitespace in C++.

ASCII Table

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:

cout<< "\n";	// Advance output to beginning of next line
cout<< "\b";	// Backup one space in the output
cout<< "\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:

cout<< (char)10;	// Advance output to beginning of next line
cout<< (char)8;		// Backup one space in the output
cout<< (char)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 g++ chew.c
 */

#include <stdio.h>
// #include <cstdlib>  // 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]