ζeta

by Luka Napotnik

C programming language rant

C is a language from 80’s and while it is good in certain areas (like low-level intraction with the OS), it lags significantly in others. If your’e not pedant enough and lack discipline, you will find out that most of your development time will go to debugging trivial pointer errors and/or synchronization errors if writing multi-threaded programs.

While the language minimalism and simplicity is very attractive, I think it’s not suitable anymore for modern programming. So, avoid using C, if possible :-)

There is a systems language, fairly close to C. It has good C support so inlining C code is nearly trivial. You can find more about the language by visiting the Go programming language home page. Even more rant

Common programming errors

1. Implicit declaration of function

This happens when the header information of a called function is unknown to the current source file.

In such case, the C language will default the function return value of an int type. Which can be a serious problem if the function is supposed to return a pointer. For example, on 64-bit Intel system, instead of returning a pointer of size sizeof(void *)==8, it will truncate the pointer to a 4-byte integer value, thus returning a pointer to an invalid memory location.

2. assigning signed values to unsigned data types

Don’t put the unsigned just because you can. Use that only if you’re absolutely certain you can’t possibly have negative values. For example, it’s like a convention that functions usually return negative values on errors, and positive on success. Using unsigned integers to assign a return value of a function that can return negative values will hide possible unhandled errors.

3. argument count in string literal and given arguments don’t match

Mostly happens with the printf and scanf family of functions. For example, the code

printf("%s: hello\n");

is an easy way for a possible segmentation fault to occur. The best prevention is to use additional compiler flags that check the arguments of such functions.

4. ignoring warning messages

All of the above mentioned can be prevented with appropriate compiler flags and taking care of the warning messages. I propose the following additional GCC flags:

-Wall -Werror -Wformat-nonliteral -Wextra -pedantic