In this episode of “Did you know..?” we take another look at some C language tips.
- Character and string constants
- Not initializing pointers
- Wrong loops
Character and string constants
The C language considers character and string constants as very different things.
Character constants should be enclosed in single quotes.
Mixing them up will result in a compiler error. String constants should be enclosed in double quotes.
(String constants act as a pointer to the string.)
Prototypes will tell the compiler important features of a function: the return type and the parameters
of the function. If no prototype is given, the compiler assumes that the function returns an int and
can take any number of parameters of any type. So a prototype must be used if the function does not return an int.
Not initializing pointers
Anytime you use a pointer; you should make sure that it points to a variable.
If a pointer doesn’t point to a variable you most likely get a segmentation fault / coredump error on UNIX/Linux or
a general protection fault under Windows.
You have to be careful that you don’t put a semicolon behind a loop. Example: while(z > 10); The semicolon after the while statement will cause an infinite loop. Another common loop error is to iterate one to many times or one too few. So make sure that your loop conditions are correct.