Previous: 5.3.2 Enumeration
Up: 5.3 Scalar Data Types
Previous Page: 5.3.2 Enumeration
Next Page: 5.4 Operators and Expression Evaluation
The C language provides a facility for defining synonyms for data types to make programs more readable. New data types that are equivalent to existing data types may be created using the typedef declaration. The syntax is:
typedef int age; age yrs;The variable yrs can have age type values. In this case, the primary difference is that we can have more meaningful names for data types than the generic name int.
A typedef definition is also commonly used to ``hide the details'' of more complicated declarations:
typedef enum { FALSE, TRUE } boolean;The type definition defines data type, boolean which is a synonym for an enumerated type consisting of two constant values FALSE and TRUE. Variables of type boolean can now be defined, and they can be assigned one of the enumerated values. In fact, the name, boolean, can be used like any other data type. Functions can have boolean parameters and can return boolean values. For example, we could write:boolean flag;
flag = TRUE;if (flag) printf("Flag is true\n");
Let us consider the task of a simple calculator. It should read two numbers and then read an operator that is to be applied to the operands. The operator should be applied to the operands and the result printed. (When an operator appears after the operands, the expression is said to be in postfix form). The algorithm for a postfix calculator is:
repeat until end of file or error in reading numbers read two numbers and an operator apply operator to the numbers and get result print result
The program must make sure that two valid numbers and an operator are read correctly. We will ensure that two numbers are read correctly by examining the value returned by scanf(). The buffer will then be scanned and flushed until a valid operator is found. The program is shown in Figure 5.8.
The while loop continues until scanf() is unable to read two numbers. If scanf() reads two numbers, it returns a value of 2, and the loop is executed. In the loop, we use get_operator() to get a valid operator. The function, get_operator() will scan each new character in the keyboard buffer until an acceptable operator is found. Once an operator is read, an error flag of type boolean is initialized to FALSE.
A switch statement is used to determine the result of applying the operator to the operands. The division operator can lead to trouble if oprnd2 is zero; divide by zero is a fatal error and the program would be aborted. We trap this error by testing for a zero value of oprnd2, in which case we set error to TRUE. If there is no error, the result is printed ; otherwise, an error message is printed. The loop repeats until scanf() does not read 2 floats (including detecting EOF).
The function get_operator() consists of a loop that continues
to read a character until a valid operator is read; skipping over any white
space and any erroneous characters. It uses a boolean type function,
operatorp(), to test if an argument is an acceptable operator.
Figure 5.9 shows the required functions.
Sample Session: