Did you know..? C Programming Language Tips 1

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

  • scanf() wrong format for an operand
  • Array size and starting index
  • Wrong use: = instead of ==
  • scanf() forgetting the ampersand (&)


scanf() wrong format for an operand

C compilers do not check for format errors of arguments from a scanf() statement.
The most common errors are: mixing up %c and %s for characters and
strings and %f format for doubles (should be %lf).


Array size and starting index

The first index of an array, in the C language, starts at index zero. (Not one!).
So an array of ten integers, defined as int a[10], has valid indices from zero to nine.
(Not from one to ten!). This can lead to unpredictable results if you make a mistake.


Wrong use: = instead of ==

The C language = operator is only used for assignments. ( int x = 1; )
The == (is equal) operator is used for comparison. ( if ( x == 1) ).
(The “if statement” returns a 0 for false and a not 0 for true).
If = is used instead of == the result of the “if statement” is almost always not true.


scanf() forgetting the ampersand (&)

The scanf() function needs to have the address of the variable to store the input.
This means that almost always the ampersand (&) address operator is required.
(Except for pointers that point to an variable. (char * ptr_one = malloc(100)).

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.