Site Sections => About Us | Consultancy | Training | Software | Publications | Open Source | Support | Open Standards | FAQ | Jobs
Site Style Info

2.5. Declaration of variables

You may remember that in Chapter 1 we said that you have to declare the names of things before you can use them (the only exceptions to this rule are the names of functions returning int, because they are declared by default, and the names of labels). You can do it either by using a declaration, which introduces just the name and type of something but allocates no storage, or go further by using a definition, which also allocates the space used by the thing being declared.

The distinction between declaration and definition is an important one, and it is a shame that the two words sound alike enough to cause confusion. From now on they will have to be used for their formal meaning, so if you are in doubt about the differences between them, refer back to this point.

The rules about what makes a declaration into a definition are rather complicated, so they will be deferred for a while. In the meantime, here are some examples and rule-of-thumb guidelines which will work for the examples that we have seen so far, and will do for a while to come.

/*
* A function is only defined if its body is given
* so this is a declaration but not a definition
*/

int func_dec(void);

/*
* Because this function has a body, it is also
* a definition.
* Any variables declared inside will be definitions,
* unless the keyword 'extern' is used.
* Don't use 'extern' until you understand it!
*/

int def_func(void){
     float f_var;            /* a definition */
     int counter;            /* another definition */
     int rand_num(void);     /* declare (but not define) another function */

     return(0);
}

Exercise 2.1. Why are trigraphs used?

Exercise 2.2. When would you expect to find them in use, and when not?

Exercise 2.3. When is a newline not equivalent to a space or tab?

Exercise 2.4. When would you see the sequence of ‘backslash newline’ in use?

Exercise 2.5. What happens when two strings are put side by side?

Exercise 2.6. Why can't you put one piece of comment inside another one? (This prevents the technique of ‘commenting out’ unused bits of program, unless you are careful.)

Exercise 2.7. What are the longest names that may safely be used for variables?

Exercise 2.8. What is a declaration?

Exercise 2.9. What is a definition?

Now we go on to look at the type of variables and expressions.

The C Book

This 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
Bradford Design Exchange
34 Peckover Street
BRADFORD
BD1 5BD
West Yorkshire
United Kingdom

consulting@gbdirect.co.uk

Training: 0800 651 0338
General: +44 (0)870 200 7273
Finance: +44 (0)1353 615 174

Please call between 0900 and 1700 (UK time) on Monday to Friday


South East Regional Office

GBdirect Ltd
18 Lynn Rd
ELY
CB6 1DA
Cambridgeshire
United Kingdom

consulting@gbdirect.co.uk

Training: 0800 651 0338
General: +44 (0)870 200 7273
Finance: +44 (0)1353 615 174

Please call between 0900 and 1700 (UK time) on Monday to Friday


Please note:
Non-training enquiries should be directed, initially, to our UK national office in Bradford (West Yorkshire), even if the enquiry concerns services delivered in London or South/East England. Clients in London and the South East will typically be handled by staff working in the London or Cambridge areas.