views:

432

answers:

3

I am currently learning and experimenting with C and am using Bloodshed's DEV-C++ as an IDE.

Now, I just realized that the following piece of code (as it is...no includes or nothing) compiles and runs :

main ()
{
    printf("%d", strlen("hello"));  
}

Now, if I'm not mistaken, shouldn't two header files be included in this source for it to work ? stdio.h and string.h...but as you can see, I did not add them and the code still compiled and ran successfully.

My complaint is that I want the compiler to be "strict" because since I'm still learning C, I don't want the code to run if normally it shouldn't.

So, is there any way to prevent Dev-C++ from 'correcting my mistakes' when it comes to includes, ie making it more kinda "strict" ?

+4  A: 

I don't know if this actually is a DevC++ issue, but in any case you should consider ditching it. It is no longer being developed and is very buggy. I recommend changing to Code::Blocks, which is better in every way and alows you to use the very latest GCC compiler.

anon
Downloaded and currently trying out Code::Blocks; Thanks for the advice
Andreas Grech
It isn't a Dev-C++ issue, it is a C90 feature absent of C99 and C++.
AProgrammer
Actually, it is a DevC++ issue, in that the version of the compiler that comes with it is ancient. Current gcc versions warn about lack of prototypes.
anon
-Wimplicit-function-declaration is in -Wall since at least gcc 2.95. And gcc 4.3 in c90 or gcc90 mode still doesn't warn without a request of at least -Wimplicit-function-declaration (I haven't 4.4 readily available here to check, but looking at the online documentation on gcc.gnu.org, I don't think it has changed). Note that this warning isn't about the presence of prototypes but the implicit declaration. All the options controlling the check of prototypes seem to be available in gcc 2.95 as well.
AProgrammer
With gcc 4.4.0, using the command line "gcc f.c" I get a warning about printf.
anon
I see. It is warning about the conflict between the intrinsic printf and the implicit declaration. You'll get it only when gcc has an intrinsic of the same name as the function implicitly declared You still need -Wimplicit-function-declaration (which is included in -Wall) to get a warning in other cases.
AProgrammer
+1 Thanks for the suggestion of Code::Blocks; dumped Dev-C++ and started using it
Andreas Grech
+1  A: 

One of the possibilities for 'undefined behaviour' - which you get if you call a variadic function without a visible prototype - is that your code compiles and runs successfully.

If you're using gcc as the underlying compiler then you should be able to pass flags such as -std=c89 -pedantic -Wall -Wextra and get warnings about code such as the snippet that you've posted.

Charles Bailey
+7  A: 

C90 had a feature (absent of C99 and C++) called implicit function declaration: when you used a name not declared yet in a function call, the compiler behaved as if

extern int identifier();

had been seen. That feature has been dropped from C99 and most compilers had option to warn about this even before C99 was promulgated.

Even when staying in C90, it is not recommended style to use this. If you have to maintain code making use of this and can't add prototypes, check that:

  • the functions returns an int (it is the case for printf but the validity is implementation dependent for strlen which returns a size_t which can be int or something else)

  • the function isn't variadic (it is the case for strlen but not printf)

  • the type of the arguments is not modified by default argument promotions (char, short, float are) and you must pay attention to cast pointers to void* when needed when the expected type is void*, you have to pay attention to cast NULL to the correct pointer type. (These are the same things you have to pay attention for variadic arguments BTW).

If those conditions aren't met -- and they aren't for any calls in your code -- you enter in the realm of undefined behavior.

AProgrammer
+1 Thanks for this very interesting information; I didn't know about this
Andreas Grech