2007-10-29

To C or Not to C

Today I was a bit puzzled by this code (test.c):

#include <stdio.h>

int main()
{
    int i = ceil(2.9f);
    printf("i is: %d\n", i);
}

I had compiled the code using the command cl test.c, clean compile no warnings, no messages, after running the program the result was: i is: 1024. WTF?!

The proper compile command cl /W4 test.c returned this:

test.c(5) : warning C4013: 'ceil' undefined; assuming extern returning int

C assumes a bit too much.

Next time I want to try a code snippet I won't use the .c extension, I'll use .cc, one character makes a big difference:

test.cc(5) : error C3861: 'ceil': identifier not found


D'oh, I forgot to #include <math.h>!

2 comments:

Dorin said...

let us not forget the lack of parameters after printf ;) - other (gc)compilers know to warn about that too ;)

Cristian said...

You're right! The code editor from blogger doesn't have a live C compiler ;) My memory dump was not accurate, I'll update the post. Rembmer, be vigilant!