next up previous
Next: Operators Up: Scientific programming in C Previous: Variables

Expressions and statements

An expression represents a single data item--usually a number. The expression may consist of a single entity, such as a constant or variable, or it may consist of some combination of such entities, interconnected by one or more operators. Expressions can also represent logical conditions which are either true or false. However, in C, the conditions true and false are represented by the integer values 1 and 0, respectively. Several simple expressions are given below:
a + b
x = y
t = u + v
x <= y
The first expression, which employs the addition operator (+), represents the sum of the values assigned to variables a and b. The second expression involves the assignment operator (=), and causes the value represented by y to be assigned to x. In the third expression, the value of the expression (u + v) is assigned to t. The fourth expression takes the value 1 (true) if the value of x is less than or equal to the value of y. Otherwise, the expression takes the value 0 (false). Here, <= is a relational operator that compares the values of x and y. The final example causes the value of j to be increased by 1. Thus, the expression is equivalent to
j = j + 1
The increment (by unity) operator ++ is called a unary operator, because it only possesses one operand.

A statement causes the computer to carry out some definite action. There are three different classes of statements in C: expression statements, compound statements, and control statements.

An expression statement consists of an expression followed by a semicolon. The execution of such a statement causes the associated expression to be evaluated. For example:

a = 6;
c = a + b;
The first two expression statements both cause the value of the expression on the right of the equal sign to be assigned to the variable on the left. The third expression statement causes the value of j to be incremented by 1. Again, there is no restriction on the length of an expression statement: such a statement can even be split over many lines, so long as its end is signaled by a semicolon.

A compound statement consists of several individual statements enclosed within a pair of braces { }. The individual statements may themselves be expression statements, compound statements, or control statements. Unlike expression statements, compound statements do not end with semicolons. A typical compound statement is shown below:

  pi = 3.141593;
  circumference = 2. * pi * radius;
  area = pi * radius * radius;
This particular compound statement consists of three expression statements, but acts like a single entity in the program in which it appears.

A symbolic constant is a name that substitutes for a sequence of characters. The characters may represent either a number or a string. When a program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence. Symbolic constants are usually defined at the beginning of a program, by writing

#define  NAME  text
where NAME represents a symbolic name, typically written in upper-case letters, and text represents the sequence of characters that is associated with that name. Note that text does not end with a semicolon, since a symbolic constant definition is not a true C statement. In fact, during compilation, the resolution of symbolic names is performed (by the C preprocessor) before the start of true compilation. For instance, suppose that a C program contains the following symbolic constant definition:
#define  PI  3.141593
Suppose, further, that the program contains the statement
area = PI * radius * radius;
During the compilation process, the preprocessor replaces each occurrence of the symbolic constant PI by its corresponding text. Hence, the above statement becomes
area = 3.141593 * radius * radius;
Symbolic constants are particularly useful in scientific programs for representing constants of nature, such as the mass of an electron, the speed of light, etc. Since these quantities are fixed, there is little point in assigning variables in which to store them.

next up previous
Next: Operators Up: Scientific programming in C Previous: Variables
Richard Fitzpatrick 2006-03-29