KB01 - C Programming Styles

  1. Include the followings and all other related header files in all your program.
    • #include <stdio.h>
    • #include <stdlib.h>
    • #include <stdint.h>
    • #include <stdbool.h>
  2. Declare functions and variables before using them.
  3. Better to increment and decrement with ++ and -- operators.
  4. Better to 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. An 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 returned 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 understanding
  14. Use /* .. */ instead of //, it makes the code look better and readable
  15. The last and most important one is always compiling your program before submitting or showing it to someone. Don’t assume that your code is compliable and contains no errors. Try using –std=c99,
    which is c99 standard. It is better. (Although, c11 is also on its way but is not a standard at the moment for all machines)