1.3. A description of Example 1.11.3.1. What was in itEven such a small example has introduced a lot of C. Among other
things, it contained two functions, a 1.3.2. Layout and commentThe layout of a C program is not very important to the compiler, although for readability it is important to use this freedom to carry extra information for the human reader. C allows you to put space, tab or newline characters practically anywhere in the program without any special effect on the meaning of the program. All of those three characters are the same as far as the compiler is concerned and are called collectively white space, because they just move the printing position without causing any ‘visible’ printing on an output device. White space can occur practically anywhere in a program except in the middle of identifiers, strings, or character constants. An identifier is simply the name of a function or some other object; strings and character constants will be discussed later—don't worry about them for the moment. Apart from the special cases, the only place that white space must be
used is to separate things that would otherwise run together and become
confused. In the example above, the fragment Comment is introduced to a C program by the pair of characters
It is common practice to make a comment stand out by making each line of
multi-line comment always start with a 1.3.3. Preprocessor statementsThe first statement in the example is a preprocessor directive. In days gone by, the C compiler used to have two phases: the preprocessor, followed by the real compiler. The preprocessor was a macro processor, whose job was to perform simple textual manipulation of the program before passing the modified text on to be compiled. The preprocessor rapidly became seen as an essential aspect of the compiler and so has now been defined as part of the language and cannot be bypassed. The preprocessor only knows about lines of text; unlike the rest
of the language it is sensitive to the end of a line and though it is
possible to write multi-line preprocessor directives, they are uncommon and
a source of some wonder when they are found. Any line whose first visible
character is a In Example 1.1 the preprocessor directive
1.3.3.1. Define statementsAnother of the preprocessor's talents which is widely exploited is the
#define IDENTIFIER replacement which says that the name represented by #define PI 3.141592 #define SECS_PER_MIN 60 #define MINS_PER_HOUR 60 #define HOURS_PER_DAY 24 and to use them like this
circumf = 2*PI*radius;
if(timer >= SECS_PER_MIN){
mins = mins+1;
timer = timer - SECS_PER_MIN;
}
the output from the preprocessor will be as if you had written this:
circumf = 2*3.141592*radius;
if(timer >= 60){
mins = mins+1;
timer = timer - 60;
}
SummaryPreprocessor statements work on a line-by-line basis, the rest of C does not.
1.3.4. Function declaration and definition1.3.4.1. DeclarationAfter the By declaring 1.3.4.2. DefinitionRight at the end of the program is the function definition itself; although it is only three lines long, it usefully illustrates a complete function. In C, functions perform the tasks that some other languages split into
two parts. Most languages use a function to return a value of some sort,
typical examples being perhaps trigonometric functions like sin, cos, or
maybe a square root function; C is the same in this respect. Other similar
jobs are done by what look very much like functions but which don't return
a value: FORTRAN uses subroutines, Pascal and Algol call them
procedures. C simply uses functions for all of those jobs, with
the type of the function's return value specified when the
function is defined. In the example, the function
The use of The type of the function is For something whose essence is emptiness, abnegation and rejection,
The body of the function is a compound statement, which is a
sequence of other statements surrounded by curly
brackets It is reasonable to ask whether or not the brackets are strictly needed, if their only job is to bind multiple statements into one, yet all that we have in the example is a single statement. Oddly, the answer is yes—they are strictly needed. The only place in C where you can't put a single statement but must have a compound statement is when you are defining a function. The simplest function of all is therefore the empty function, which does nothing at all:
void do_nothing(void){}
The statement inside show_message is a call of the library function
SummaryDeclarations are used to introduce the name of a function, its return type and the type (if any) of its arguments. A function definition is a declaration with the body of the function given too. A function returning no value should have its type declared as
A function taking no arguments should be declared with 1.3.5. StringsIn C, strings are a sequence of characters surrounded by quote marks: "like this" Because a string is a single element, a bit like an identifier, it is not allowed to continue across a line—although space or tab characters are permitted inside a string. "This is a valid string" "This has a newline in it and is NOT a valid string" To get a very long string there are two things that you can do. You could take advantage of the fact that absolutely everywhere in a C program, the sequence ‘backslash end-of-line’ disappears totally. "This would not be valid but doesn't have \ a newline in it as far as the compiler is concerned" The other thing you could do is to to use the string joining feature, which says that two adjacent strings are considered to be just one. "All this " "comes out as " "just one string" Back to the example. The sequence ‘ To support people working in environments that use character sets which are ‘wider’ than U.S. ASCII, such as the shift-JIS representation used in Japan, the Standard now allows multibyte characters to be present in strings and comments. The Standard defines the 96 characters that are the alphabet of C (see Chapter 2). If your system supports an extended character set, the only place that you may use these extended characters is in strings, character constants, comment and the names of header files. Support for extended character sets is an implementation defined feature, so you will have to look it up in your system's documentation. 1.3.6. The main functionIn Example 1.1 there are actually two functions,
This is a much more realistic function now, because there are several
statements inside the function body, not just one. You might also have
noticed that the function is not declared to be
The most important thing about 1.3.7. DeclarationsThe first statement is this: int count; which is not an instruction to do anything, but simply introduces a
variable to the program. It declares something whose name is
As a result of that declaration the compiler now knows that there is
something that will be used to store integral quantities, and that its name
is (Note for pedants: unless you specifically ask, the declaration of a
variable like 1.3.8. Assignment statementMoving down the example we find a familiar thing, an assignment
statement. This is where the first value is assigned to the variable
So far then, we have declared a variable and assigned the value of zero to it. What next? 1.3.9. The while statementNext is one of C's loop control statements, the while statement. Look
carefully at its form. The formal description of the
while(expression)
statement
Is that what we have got? Yes it is. The bit that reads count < 10 is a relational expression, which is an example of a valid
expression, and the expression is followed by a compound statement, which
is a form of valid statement. As a result, it fits the rules for a properly
constructed What it does must be obvious to anyone who has written programs
before. For as long as the relationship There are just two statements in the body of the loop. The first one is a
function call, where the function /* call a function with several arguments */ function_name(first_arg, second_arg, third_arg); and so on. The call of The last statement in the loop is another assignment statement. It adds
one to the variable 1.3.10. The return statementThe last statement that is left to discuss is the return expression; where the expression is optional. The example uses a common stylistic convention and puts the expression into parentheses, which has no effect whatsoever. The return causes a value to be returned from the current function to its
caller. If the expression is missing, then an unknown value is passed back
to the caller—this is almost certainly a mistake unless the function
returns
int main(){
and exactly the same results would have occurred. You can't use the same feature to get a default type for variables because their types must be provided explicitly. What does the value returned from SummaryThe Returning from Returning 1.3.11. Progress so farThis example program, although short, has allowed us to introduce several important language features, amongst them:
although of course none of this has been covered rigorously. |
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |