Declarations
All variables must be declared before they are used. They must be declared at the top of a block (a section of code enclosed in brackets { and }) before any statements. They may be initialised by a constant or an expression when declared. The following are a set of example declarations.
{ /* bracket signifies top of a block */
int lower, upper, step; /* 3 uninitialised ints */
char tab = ’\t’; /* a char initialised with ’\t’ */
char buf[10]; /* an uninitialised array of chars */
int m = 2+3+4; /* constant expression: 9 */
int n = m + 5; /* initialised with 9+5 = 14 */
float limit = 9.34f;
const double PI = 3.1416;
The general form of a declaration6 is
<qualifier> <type> <identifier1> = <value1>, <identifier2> = <value2>, ... ;
where the assignment to an initial value is optional (see also Section 5.5).
2.7 Arithmetic Operations
The arithmetic (or numerical) operators come in two varieties: unary and binary. The binary operators are plus +, minus −, multiply ∗, divide /, and the modulus operator %. The first four operators can be used on integer or floating-point types, although it is important to notice that integer division truncates any fractional part (e.g., 17/5 is equal to 3). The modulus operator is valid only for non-floating-point types (e.g., char, int, etc), and x % y produces the remainder from the division x / y (e.g., 18 % 7 is equal to 4).
Note. For negative integers, the direction of truncation for /, and the sign for the result of %, are implementation defined (i.e., they may have di erent results on di erent platforms). A portable work-around for this is shown in Section 10.3.2.
The unary operators plus + and minus - can be used on integer or floating-point types, and are used as follows.
int ispositive = +34;
double isnegative = -56.3;
The unary + is a redundant operator as numbers are positive by default. It exists only for symmetry with the unary - operator.
An important set of unary operators are the increment ++ and decrement -- operators. These operators add 1 to a variable and subtract 1 from a variable, respectively. Thus, the expression x++ is equivalent to x = x + 1. An unusual quality of ++ and -- is that they may be used prefix ++x or postfix x++ with di erent characteristics. For example,
double x = 3.2;
double y = ++x;
double z = x++;
6A variable definition is usually synonymous with its declaration. However, there is a subtle di erence when it comes to external variables, as discussed in Section 5.2.
In the first case, called preincrement, the value of x is increased to 4.2 and then assigned to y, which then also equals 4.2. In the second case, called postincrement, the value of x is first assigned to z, and subsequently increased by 1; so, z equals 4.2 and x equals 5.2.
The precedence of the arithmetic operators is as follows: ++, --, and unary + and − have the highest precedence; next comes ∗, /, and %; and finally, binary + and − have the lowest precedence.
int a=2, b=7, c=5, d=9;
printf("a*b + c*d = %d\n", a*b + c*d); /* prints a*b + c*d = 59 */
Two common errors can occur with numerical operations: divide-by-zero and overflow. The first occurs during a division operation z = x / y where y is equal to zero; this is the case for integer or floating-point division. Divide-by-zero errors can also occur with the modulus operator if the second operand is 0. The second error, overflow, occurs when the result of a mathematical operation cannot be represented by the result type. For example,
int z = x + 1;
will overflow if the value of x is the largest representable value of type int. The value of z following a divide-by-zero or overflow error will be erroneous, and may be di erent on di erent platforms.
No comments:
Post a Comment