Did you know..? C Programming Language Tips 2

In this episode of “Did you know..?” we take another look at some C language tips.

  • Character and string constants
  • Prototypes
  • 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

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.


Wrong loops

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.

This entry was posted in Programming Tips. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.