Author Topic: dont want by default #include's  (Read 9873 times)

jguarin2002

  • Guest
dont want by default #include's
« on: August 08, 2007, 06:41:38 pm »
Hello everyone

Im having this compiler directives class and I am the instructor. I want to illustrate the students what is and why is necessary the #include directive etc...

The problem is that in theory if I do not include the stdio.h header then -beside other functions- I wont have printf declared and therefore I cant make any calls to it. However -in a classical hello world example- when I cut off this line from the code (the #include <stdio.h> directive) the building does not complain and works fine, and it should because of the printf("hello world\n"); line on the code. There is no errors, even warnings or whatsoever.

I guess this is a CodeBlocks feature, but i dont know how to turn it off. How do i do that? I mean I want to make explicit the #include <stdio.h>.
« Last Edit: August 08, 2007, 06:43:30 pm by jguarin2002 »

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: dont want by default #include's
« Reply #1 on: August 08, 2007, 06:57:15 pm »
that's not CB, the compiler should complain, and I think it will.

I assume you teaching C ???

See the result of new console project from Cb (C language) :

Where I commented out the 2 includes (note the stdlib.h is not even needed !!!! NOTE to self : adjust our code)


Quote
int main()
{
    printf("Hello world!\n");
    return 0;
}

This is the compiler output CB shows -> apparently warnings  :shock: :shock: :shock:

Quote
-------------- Build: Debug in DelMe ---------------
Compiling: main.c
/home/killerbot/Projects/DelMe/main.c: In function ‘main’:
/home/killerbot/Projects/DelMe/main.c:6: warning: implicit declaration of function ‘printf’
/home/killerbot/Projects/DelMe/main.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
Linking console executable: bin/Debug/DelMe
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 2 warnings

But hey, code should also b warning free. Maybe there's a setting you can specify to  the (GCC) compiler, where such a things should be treated as an error, but I don't know if there is one.

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: dont want by default #include's
« Reply #2 on: August 08, 2007, 08:45:01 pm »
It's not a C::B feature; it's part of the C standard, which allows functions to be "implicitly" declared -- i.e. assumed to have a return value of "int" and argument types that match the ones passed.

Note: The C99 standard no longer allowed for implicit function declarations, which is why a warning is emitted. To turn the warning into an error, you can add the GCC parameter "-Werror-implicit-function-declaration".
« Last Edit: August 08, 2007, 08:50:36 pm by TDragon »
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

jguarin2002

  • Guest
Re: dont want by default #include's
« Reply #3 on: August 08, 2007, 09:22:38 pm »
u r right. Yeah. Both of you are right. But....

Im using mingw as compiler on win32.

However in Linux it will complain with a warning at least (i know mingw is a win32 gcc port, but still a port), im used more to linux than to windows (nothing against windows).

But, if, for example. You declare something like a FILE *, it will complain with an error.

Ok. So till where the C standard stands to?. printf will not complain if not included stdio.h but FILE * will cry whenever stdio.h is not declared.

What do you think?. I apreciate your comments, thank you.
julian

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: dont want by default #include's
« Reply #4 on: August 08, 2007, 09:43:31 pm »
I have done the same on windows Xp with GCC 3.4.5, also here I get a warning :

Quote
-------------- Build: Debug in DelMe ---------------
[ 50.0%] Compiling: main.c
C:\Projects\DelMe\main.c: In function `main':
C:\Projects\DelMe\main.c:5: warning: implicit declaration of function `printf'
[100.0%] Linking console executable: ..\Deliv\Debug\DelMe.exe
Process terminated with status 0 (0 minutes, 2 seconds)
0 errors, 1 warnings
Build log saved as: C:\Projects\DelMe\DelMe_build_log.html

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: dont want by default #include's
« Reply #5 on: August 08, 2007, 09:44:26 pm »
and when adding the GCC option as suggested above I get an error :

Quote
-------------- Build: Debug in DelMe ---------------
[ 50.0%] Compiling: main.c
C:\Projects\DelMe\main.c: In function `main':
C:\Projects\DelMe\main.c:5: error: implicit declaration of function `printf'
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings
Build log saved as: C:\Projects\DelMe\DelMe_build_log.html
 

So all seems to be ok to me.

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: dont want by default #include's
« Reply #6 on: August 08, 2007, 09:48:37 pm »
What, exactly, are you asking?

The C standard states (up to C99, anyway) that a function call with no prototype will be assumed to take the arguments given on the first invocation and return an int result.

This fits perfectly with your call to printf(). 

However, your declaration with FILE is not a function call, it is a variable declaration.  Since FILE has not yet been defined anywhere (stdio.h not included) the compiler issues an error.

For a more complete (if blistering) explanation, you should ask this question on comp.lang.c

Ringo

jguarin2002

  • Guest
Re: dont want by default #include's
« Reply #7 on: August 08, 2007, 10:08:23 pm »
Quote
function call with no prototype will be assumed to take the arguments given on the first invocation and return an int result

Ok. Let me see if i got it: So after first call of the unprototyped printf, subsequent printf invocations should have the same arguments number is'nt it?. If im right should not this code complain about argument mismatch?: The first call to printf is clearly misnumbering the second printf call, breaking the c99 rule? What im missing?


Code
int main()
{

printf("The return value of printf(\" Hello world\"); is %d!\n",printf("Hello world\n") );

return 0;
}

by the way... it did not crash complain or anything.
« Last Edit: August 08, 2007, 10:10:06 pm by jguarin2002 »

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: dont want by default #include's
« Reply #8 on: August 09, 2007, 03:39:18 am »
The important thing to note, here, is that you're implicitly declaring a built-in function. Certain C runtime functions are built in to GCC itself rather than being pulled from the system's C runtime -- and GCC 3.4.5 allows the use of these built-ins without even a warning (yeah, I tested it just now :P).

As you've seen, this is not an entirely optimal situation; this is fixed in the GCC 4 series, which emits a warning when you do it.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: dont want by default #include's
« Reply #9 on: August 09, 2007, 07:49:25 am »
GCC 3.4.5 also gave a warning , see a reply of mine with log above

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: dont want by default #include's
« Reply #10 on: August 09, 2007, 11:21:09 am »
GCC 3.4.5 also gave a warning , see a reply of mine with log above
Not sure which code you got a warning with, but the following doesn't even provoke a warning:
Code
int main()
{
printf("Hello, world!\n");
return 0;
}
...
Code
gcc -o test.exe test.c
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: dont want by default #include's
« Reply #11 on: August 09, 2007, 11:42:06 am »
the code I mentioned above (same as yours), I ran from within CB.

Checked full log : seems the following options were added : -Wall -fexceptions (and offcourse a -I to mingw includes)

mitsukai

  • Guest
Re: dont want by default #include's
« Reply #12 on: August 19, 2007, 08:56:24 pm »
this is probally a mingw or GCC extension (as it says in the warning). printf is a built in function, just like vc++ can compile non-standard types as built-in types.


http://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_5.html#SEC114
« Last Edit: August 19, 2007, 09:05:31 pm by mitsukai »

jguarin2002

  • Guest
Re: dont want by default #include's
« Reply #13 on: August 20, 2007, 07:30:35 pm »
Thanks mitsukai.

Quote
The following ISO C89 functions are recognized as built-in functions unless `-fno-builtin' is specified: abs, cos, fabs, fprintf, fputs, labs, memcmp, memcpy, memset, printf, sin, sqrt, strcat, strchr, strcmp, strcpy, strcspn, strlen, strncat, strncmp, strncpy, strpbrk, strrchr, strspn, and strstr. All of these functions have corresponding versions prefixed with __builtin_.

thats why.