- The Structure of a C Program
- Variables
- Operators
- Expressions and Statements
- Program Flow
- Preprocessor
- Command Line Compiling and Debugging
- Summary
- Exercises
Expressions and Statements
Expressions and statements in C are the rough equivalent of phrases and sentences in a natural language.
Expressions
The simplest expressions are just single constants or variables:
14 bananasPerBunch
Every expression has a value. The value of an expression that is a constant is just the constant itself: The value of 14 is 14. The value of a variable expression is whatever value the variable is holding: The value of bananasPerBunch is whatever value it was given when it was last set by initialization or assignment.
Expressions can be combined to create other expressions. The following are also expressions:
j + 14 a < b distance = rate * time
The value of an arithmetic or logical expression is just whatever you would get by doing the arithmetic or logic. The value of an assignment expression is the value given to the variable that is the target of the assignment.
Function calls are also expressions:
SomeFunction()
The value of a function call expression is the return value of the function.
Evaluating Expressions
When the compiler encounters an expression, it creates binary code to evaluate the expression and find its value. For primitive expressions, there is nothing to do: Their values are just what they are. For more complicated expressions, the compiler generates binary code that performs the specified arithmetic, logic, function calls, and assignments.
Evaluating an expression can cause side effects. The most common side effects are the change in the value of a variable due to an assignment, or the execution of the code in a function due to a function call.
Expressions are used for their value in various control constructs to determine the flow of a program (see Program Flow). In other situations, expressions may be evaluated only for the side effects caused by evaluating them. Typically, the point of an assignment expression is that the assignment takes place. In a few situations, both the value and the side effect are important.
Statements
When you add a semicolon (;) to the end of an expression, it becomes a statement. This is similar to adding a period to a phrase to make a sentence in a natural language. A statement is the code equivalent of a complete thought. A statement is finished executing when all of the machine language instructions that result from the compilation of the statement have been executed, and all of the changes to any memory locations the statement affects have been completed.
Compound Statements
You can use a sequence of statements, enclosed by a pair of curly brackets, any place where you can use a single statement:
{ timeDelta = time2 – time1; distanceDelta = distance2 – distance1; averageSpeed = distanceDelta / timeDelta; }
There is no semicolon after the closing bracket. A group like this is called a compound statement or a block. Compound statements are very commonly used with the control statements covered in the next sections of the chapter.