Copyright @ 2009 Ananda Gunawardena
Starting to think like a C programmer
We have spent quite a bit of time now talking about C
language. It is possible that so far your thinking was
based on your first “computer” language Java. You may have
been trying to think like a Java programmer and convert
that thought to C. Now it is time to think like a C
programmer. Being able to think directly in C will make you
a better C programmer. Here are 15 things to remember when
you start a C program from scratch.
1. include <stdio.h> in all your programs
2. Declare functions and variables before using them
3. increment and decrement with ++ and – operators.
4. Use x += 5 instead of x = x + 5
5. A string is an array of characters ending with a ‘\0”.
Don’t ever forget the null character.
6. Array of size n has indices from 0 to n-1. Although C
will allow you to access A[n] it is very dangerous.
7. A character can be represented by an integer (ASCII
value) and can be used as such.
8. The unary operator & produces an address
9. The unary operator * dereference a pointer
10. Arguments to functions are always passed by value. But
the argument can be an address of just a value
11. For efficiency, pointers can be passed to or return
from a function.
12. Logical false is zero and anything else is true
13. You can do things like for(;;) or while(i++) for
program efficiency and writability
14. Use /* .. */ instead of //
15. Always compile your program with –ansi –pedantic –Wall
flags
Further Readings
1.
K & R – 1.6, 1.7, 1.8, 1.9, 4.1, 5.1,5.3, 5.7
2.
http://www.cs.cmu.edu/~thoffman/S09-15123/Chapter-
2/Chapter-2.html#CHAP_2.2