Unit 3 - Structure of A C Program
Unit 3 - Structure of A C Program
As its name implies, a constant is a value that never changes. A variable, on the other hand,
can be used to present different values.
You can think of a constant as a music CD-ROM; the music saved in the CD-ROM is never
changed. A variable is more like an audio cassette: You can always update the contents of
the cassette by simply overwriting the old songs with new ones.
You can see many examples in which constants and variables are in the same statement.
where the symbol 1 is a constant because it always has the same value (1), and the symbol i
is assigned the constant 1. In other words, i contains the value of 1 after the statement is
executed. Later, if there is another statement, i = 10; after it is executed, i is assigned the
value of 10. Because i can contain different values, it’s called a variable in the C language.
Expressions
Similarly, the expression 10 * (4 + 5) yields 90. The 80/4 expression results in 20.
Here are some other examples of expressions:
Expression Description
6 An expression of a constant.
i An expression of a variable.
6+i An expression of a constant plus a variable.
exit(0) An expression of a function call.
Operators
As you’ve seen, an expression can contain symbols such as +, *, and /. In the C language,
these symbols are called arithmetic operators.
Symbol Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder (or modulus)
You might already be familiar with all the arithmetic operators, except the remainder (%)
operator. % is used to obtain the remainder of the first operand divided by the second
operand. For instance, the expression
6%4
The remainder operator, %, is also called the modulus operator. Among the arithmetic
operators, the multiplication, division, and remainder operators
have a higher precedence than the addition and subtraction operators. For example, the
expression
2 + 3 * 10
yields 32, not 50, because of the higher precedence of the multiplication operator.
3 * 10 is calculated first, and then 2 is added into the result of the multiplication.
As you might know, you can put parentheses around an addition (or subtraction) to force the
addition (or subtraction) to be performed before a multiplication, division, or modulus
computation. For instance, the expression
(2 + 3) * 10
performs the addition of 2 and 3 first before it multiplies the result by 10.
Other operators, which are used for syntax, include the comma and semicolon. The
semicolon is generally used to indicate the end of a statement, as you will see later. The
comma is used in certain instances where a statement is comprised of a list of expressions
or declarations.
Identifiers
Along with numbers (such as the constant 7) and operators (such as the symbol +),
expressions can also contain words that are called identifiers. Function names (such as exit)
and variable names (such as i), as well as reserved keywords, are all identifiers in C.
The following is the set of characters you can use to make a valid identifier. Any characters
or symbols that do not follow these rules are illegal to use in an identifier.
For instance, stop_sign, Loop3, and _pause are all valid identifiers.
The following are illegal characters; that is, they do not meet the above set of rules for
identifiers:
Some invalid identifiers, for example, are 4flags, sum-result, method*4, and
what_size?.
Statements
In the C language, a statement is a complete instruction, ending with a semicolon. In many
cases, you can turn an expression into a statement by simply adding a semicolon at the end
of the expression.
i = (2 + 3) * 10;
i = 2 + 3 * 10;
j = 6 % 4;
k = i + j;
Statement Blocks
A group of statements can form a statement block that starts with an opening brace ({) and
ends with a closing brace (}). A statement block is treated as a single statement by the C
compiler.
for(. . .) {
s3 = s1 + s2;
mul = s3 * c;
remainder = sum % c;
}
3
is a statement block that starts with { and ends with }. Here for is a keyword in C that
determines the statement block.
A statement block provides a way to group one or more statements together as a single
statement. Many C keywords can only control one statement. If you want to put more than
one statement under the control of a C keyword, you can add those statements into a
statement block so that the block is considered as one statement by the C keyword.