An expression in C programming is defined as any valid combination of different entities like constants, variables, array elements or reference to functions. It can consist of some combination of such entities that are interconnected by one or more operators. In C, every expression has a value and this is an important feature of this language. To find the value, different operations are performed in the order dictated by operator precedence.
A simple example of an expression is
q = 2 * 10, in which the result of the multiplication between 2 and 10 is stored in q. Another example of an expression is
q < 30, whose value is 1 if true and 0 if false. Below is a table of some expressions and their values which will make things clear:
|-2 + 8||6|
|d = 5 + 5||10|
|4 > 9||0 (False)|
|6 + (c = 3 + 8)||17|
There are four types of expressions in C Programming; they are:
- Simple expressions
- Complex expressions
- Logical expressions
- Simple expressions: Simple expressions consist of a single item, i.e. a simple variable, literal constant or symbolic constant. For example:
‘A’ – A literal constant -2.5 – Another literal constant sum – A variable PI – A symbolic constant (defined in the program)
- Complex expressions: Complex expressions contain simple expressions connected by different operators. For example,
9 + 10is an expression that consists of the sub expressions
10and the addition operator
+. Another example of complex expressions can be
a - (b * (c + d) / e).
- Logical expressions: There are logical expressions which represent logical conditions that are either true or false. In C programming, true and false conditions are represented by the integer values 1 and 0 respectively. The examples of logical or relational expressions are
b > a,
k >= l,
x == y.
Logical, or relational expressions, consist of a relational operator with an operand on each side. If the relation is true, the expression has the value 1. If the relation is false, the expression has the value 0.
- Whitespace: Whitespaces are the expressions that are ignored by the compiler. They are the blank lines, tabs and spaces that are present in the source code. When the compiler reads a statement in a source code, it looks for the characters in the statement and for the semicolon, that terminates the statement, but ignores the whitespace.
Escape sequence is a special sequence of backslash (\), followed by an apostrophe (‘), a backslash (\) or a character used for formatting the output of a program or to include different special characters in the program so as to make them printable. For example, we cannot directly include a backslash in our program. For that, we create an escape sequence, i.e. ‘\\’ A character constant written in the form of escape sequence is called backslash character constant. The following escape sequences allow special characters to be included in the source code:
|\a||Alert (bell)||Produces an audible or visible alert.|
|\b||Backspace||Moves the cursor back one position (non-destructive).|
|\f||Form feed||Moves the cursor to the first position of the next page.|
|\n||New line||Moves the cursor to the first position of the next line.|
|\r||Carriage returns||Moves the cursor to the first position of the current line.|
|\t||Horizontal tab||Moves the cursor to the next horizontal tabular position.|
|\v||Vertical tab||Moves the cursor to the next vertical tabular position.|
|\’||Apostrophe||Produces a single quote.|
|\”||Double quote||Produces a double quote.|
|\?||Question mark||Produces a question mark.|
|\\||Backslash||Produces a single backslash.|
|\0||Null character||Produces a null character.|