The focus of this section is be on expressions: a very important concept in the language. This is rather natural, since C handles almost everything as expressions (borrowing a little of that Lisp functionality).
Let's start with a very simple program:
void main() { int result; result = 10 * 3 + 100 / 2; printf ("result: %d\n", result); }
There are two new things here. One is the way we print numbers using printf(). The function interprets a set of format specifiers, all of which start with a percent sign (%). The specifier is replaced by the value of a variable, passed to printf() as an additional parameter. You can have any number of specifiers and any number of additional parameters, as long as their numbers are equal: weird things will happen if you have more specifiers than parameters! You can print other things except numbers, too1. Here's a short table:
| Specifier | Description |
|---|---|
%d
|
Print an int
|
%x
|
Likewise, but in hex |
%f
|
Print a float
|
%s
|
Print a string |
%c
|
Print a single character |
C, as usual, doesn't care what parameter you pass. It will try to print a string as an int (and fail completely), so be careful in matching specifiers and parameters!
The second and most important element of our program is an expression: we assign the result of a calculation to a variable named result (yes, C variable names can be of any length). If you know the first thing about programming, this will be quite obvious to you. The syntax is no different than BASIC's. The interesting part is that the expression is result = 10 * 3 + 100 / 2, and not 10 * 3 + 100 / 2 as the case would be in BASIC.
This happens because in C, a variable assignment (denoted by =) is an operator, and not a statement (as in BASIC). The = operator calculates the expression at its right hand side, and assigns the result to the variable at the left hand side. It then returns the value assigned. This gives C its cryptic style, because you can actually write x = (y = 10). In this case, y will be assigned the value 10, and x will be assigned the value assigned to y2.
Getting used to this fact of life (or C, rather) is fundamental. It makes most of the difference between beginners and seasoned programmers. It also accounts for most of C bugs, but that is another story. Apart from this (and some of the weird symbols used), C expressions are everyday programming language expressions. Another point: C does not have any conditions as such: like conditions in Oric BASIC, if statements, loops, etc. evaluate an expression, and consider it true if it is non-zero. In BASIC, you could write:
Exactly the same thing goes on in C. Tests for equality and inequality are (surprise!) operators which return 1 if the test succeeds (BASIC functions return -1), and 0 if it fails.
Now, you'll probably begin to wonder: since even assignments are expressions, what happens to the result of an assignment? In (old, non-Turbo) Pascal, you can't have a result that isn't used. Well, in C you can do that. Once a semicolon (;) is met, the result of the expression is thrown away. In this sense, result=3*10+100/2 evaluates the expression, stores the outcome in the named variable, and then throws it away. Strangely enough, the same thing is done with function calls: printf() could well return some value (it doesn't, really), but we'll never know what that is, because it's discarded. Yes, you could do result = printf ("blah"), but there would be no point: the function returns void (i.e. doesn't return anything), so you'd get a warning or error from the compiler.
Finally, here's another thing: until you get accustomed to C expressions and their little quirks (they have many), use plenty of parentheses. They won't make your program slower and they won't make it bigger. They will make clear to the compiler what you want evaluated first. Here's a quick list of the types of operators:
- Arithmetic: addition, subtraction, etc. Include a couple of special operators for increasing/decreasing variables.
- Conditionals and logic: equality, inequality and Boolean operators (AND, OR, NOT). No surprises apart from some strange symbols.
- Bitwise operators: bitwise AND, OR, NOT, XOR (quite different from Boolean operators!), bitwise shifts (left and write).
- Special operators: assignment and more obscure ones.
- 1. In larger compilers, format specifiers are a lot more complicated. For example, you can specify padding, number formats, etc. The Oric libraries are small, however, and do not implement a complete version of
printf()yet. - 2. In fact, the parentheses are unnecessary, since assignment works from right to left, and has very low precedence.




Add new comment