Do you know your standards?

I’ve found that frequently, each person has a different idea of what C standards are. Most of this is due to people in academia mixing and matching C/C++. Students then are inclined to simply assume one standard exists in both, when it might not exist in either. Worse yet, is compilers often let you mix code standards together, making C and C++ seem like the same language. I’ve decided to make a short list of the things I see people do, and whether they’re fact or myth.

Myth: C and C++ both have the same comment style.
Until C99, this was not true. C only had

/* */
style comments, and
was an addition only in C++. The C99 standard added the C++ style comments to C.

Myth: This is not valid:

size_t n = strlen("Hello");
char message[n];

This is valid code as of C99. Traditionally, this sort of code was used by newcomers to C. It seems logical at first — until a simple explanation of the stack and constant sizes clears the issue up. However, C99 added this rather messy ability. Between the two big compilers (GCC and MSVC), only GCC attempts to support it.

Myth: You can overload functions in C.
You cannot overload functions in C. Furthermore, as a sidenote, this is illegal:

void MyFunction(int a = 5);
void MyFunction(int a)

Myth: A function without a return type is void.
In the very old days of K&R C, a function with no return type returned an int by default. For some reason, people sometimes still use this today, often thinking it returns void. Why? Because main() automatically returns 0, and thus, this generates no warnings:

   printf("Hello world!\n");

This rather silly example shows how confusing C can be to maintain backwards compatibility. The C99 standard removes “implicit int” functions.

Myth: C has references, like C++.
Fact: C does not have references, they were an addition to C++. In fact, because C does not have objects, references would be almost entirely useless, as pointers suffice.

Myth: This is valid C code:

for (int i=0; i<10; i++);

For loop variable declarations in C were not added until C99. Before that, they were invalid.

Myth: This is valid C code:

const char *str;
str = "Hello, World!";
size_t len = strlen(str);

Variables in C must always be declared at the top of their code block:

const char *str = "Hello, World!";
size_t len = strlen(str);

This restriction was removed by C++.

Note – Most compilers these days will have an ANSI strict mode to force compliance to the strictest supported level. I don’t recommend this, but simply to know the history and behaviour of your language.

Next time on SourceMod DevLog, the SourceMod DevLog: A tutorial on finding addresses in DLLs (similar to what you’ve seen in NemoD and CS:S DM)!

Leave a Reply

You must be logged in to post a comment.