-->
Page 461
numbers give a greater amount of precision to mathematical calculations than integers can; with floating-point numbers, 3/2 equals 1.5. Floating-point numbers can be represented by a decimal number, such as 687.534, or with scientific notation, such as 8.87534E+2. For larger numbers, scientific notation is preferred. For even greater precision, the type double provides a greater range. Again, specific ranges are implementation dependent.
Characters are usually implemented as single bytes, although some international character sets require two bytes. One common set of character representations is ASCII, which is found on most U.S. computers.
You use arrays for sequences of values that are often position dependent. An array is particularly useful when you need a range of values of a given type. Related to the array is the pointer. Variables are stored in memory, and a pointer is the physical address of that memory. In a sense, a pointer and an array are similar, except when a program is invoked. The space needed for an array's data is allocated when the routine that needs the space is invoked. For a pointer, the space must be allocated by the programmer, or the variable must be assigned by dereferencing a variable. The ampersand (&) is used to indicate dereferencing, and an asterisk (*) is used to indicate when the value pointed at is required. Here are some sample declarations:
int i; Declares an integer char c; Declares a character char *ptr; Declares a pointer to a character double temp[16]; Declares an array of double-precision floating-point numbers with 16 values
Listing 23.2 shows an example of a program with pointers.
Listing 23.2. An example of a program with pointers.
int i; int *ptr; i=5; ptr = &i; printf("%d %x %d\n", i,ptr,*ptr);
The output of this program is as follows:
5 f7fffa6c 5
NOTE |
The middle value, f7fffa6c, is an address. This might be different on your system because the pointer variable will change from version to version of the libraries. A pointer is just a memory address and will tell you the address of any variable. |
Page 462
There is no specific type for a string. An array of characters is used to represent strings. They can be printed using an %s flag, instead of %c.
Simple output is created by the printf function. printf takes a format string and the list of arguments to be printed. A complete set of format options is presented in Table 23.2. Format options can be modified with sizes. Check the gcc documentation (man page or info file) for the full specification.
Table 23.2. Format conversions for printf.
Conversion | Meaning |
%% | Percentage sign |
%E | Double (scientific notation) |
%G | Double (format depends on value) |
%X | Hexadecimal (letters are capitalized) |
%c | Single character |
%d | Integer |
%e | Double (scientific notation) |
%f | Double of the form mmm.ddd |
%g | Double (format depends on value) |
%i | Integer |
%ld | Long integer |
%n | Count of characters written in current printf |
%o | Octal |
%p | Print as a pointer |
%s | Character pointer (string) |
%u | Unsigned integer |
%x | Hexadecimal |
Some characters cannot be included easily in a program. Newlines, for example, require a special escape sequence because there cannot be an unescaped newline in a string. Table 23.3 contains a complete list of escape sequences.
Table 23.3. Escape characters for strings.
Escape Sequence | Meaning |
\" | Double quote |
\' | Single quote |
Page 463
Escape Sequence | Meaning |
\? | Question mark |
\\ | Backslash |
\a | Audible bell |
\b | Backspace |
\f | Form feed (new page) |
\n | Newline |
\ooo | Octal number |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\xhh | Hexadecimal number |
A full program is a compilation of statements. Statements are separated by semicolons, and they can be grouped in blocks of statements surrounded by curly braces. The simplest statement is an assignment, in which a variable on the left side is assigned the value of an expression on the right.
At the heart of the C programming language are expressions. These are techniques to combine simple values into new values. The three basic types of expressions are comparison, numerical, and bitwise.
The simplest expression is a comparison. A comparison evaluates to a true or a false value. In C, true is a nonzero value, and false is a zero value. Table 23.4 contains a list of comparison operators.
Table 23.4. Comparison operators.
Operator | Meaning |
< | Less than |
> | Greater than |
== | Equal to |
<= | Less than or equal to |
>= | Greater than or equal to |
continues
Page 464
Table 23.4. continued
Operator | Meaning |
|| | Logical OR |
&& | Logical AND |
! | Logical NOT |
You can combine simple comparisons with ANDs and ORs to make complex expressions. For example, consider the definition of a leap year. In words, it is any year divisible by 4, except a year divisible by 100 unless that year is divisible by 400. Using year as the variable, you can define a leap year with the following expression:
((((year%4)==0)&&((year%100)!=0))||((year%400)==0))
On first inspection, this code might look complicated, but it isn't. The parentheses group the simple expressions with the ANDs and ORs to make a complex expression.
One convenient aspect of C is that expressions can be treated as mathematical values, and mathematical statements can be used in expressions. In fact, any statementeven a simple assignmenthas values that can be used in other places as an expression.
The mathematics of C is straightforward. Barring parenthetical groupings, multiplication and division have higher precedence than addition and subtraction. The operators are standard and are listed in Table 23.5.
Table 23.5. Mathematical operators.
Operator | Meaning |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Integer remainder |
There are also unary operators, which affect a single variable. These are ++ (increment by one) and -- (decrement by one). These are shorthand for var = var + 1 and var = var - 1, respectively.
There are also shorthands for situations in which you want to change the value of a variable. For example, if you want to add an expression to a variable called a and assign a new value to