Thread: Why does this compile whithout #includes?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Why does this compile whithout #includes?

    Hi I'm using the minGW compiler I compile with the following command: gcc <filename>

    why does this work without writing the proper includes

    Code:
    int main ()
    {
     printf("isdigit('c') = %d, isdigit('1') = %d\n", isdigit('c'),isdigit('1'));
    
      return 0;
    }
    how can I make it give me an error for this?

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should be getting at least one warning. If not, compile with the -Wall option, and -pedantic for good measure. For more information, read the GCC manual on Options to Request or Suppress Warnings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Obviously depends on how you define "work". Include files, in this case, are only needed for the prototype of printf(). Since printf() is a function that takes variable arguments, and x86 supports variable arguments in a sane way, it happens to work for this.

    Likewise for the missing ctype.h include, isdigit() returns an integer (which is the default), and takes the parameter as an integer. So it's working. Now, if you printf("sin(x) = %f\n", sin(0.5));, I expect you will NOT get the right result - you MAY get the right value, but it's unlikely, I'd say, since sin returns a double value, and the compiler will default to int - so it will print some utter random rubbish.

    However, if you enable warnings (-Wall in gcc), then you will get warnings to tell you that the compiler hasn't seen the prototype for printf().

    --
    Mats
    Last edited by matsp; 02-12-2009 at 05:25 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    ok so now I'm getting warnings and sin returns a correct result but I don't understand what is happening. how does it know how to link the functions printf , isdigit and sin?

    this is the output I got:


    Code:
    >>gcc -Wall -pedantic testIsDigit.c
    testIsDigit.c: In function `main':
    testIsDigit.c:5: warning: implicit declaration of function `printf'
    testIsDigit.c:5: warning: implicit declaration of function `isdigit'
    testIsDigit.c:6: warning: implicit declaration of function `sin'
    
    >>a
    isdigit('c') = 0, isdigit('1') = 4
    sin(x) = 0.479426
    with this code:
    Code:
    int main ()
    {
     printf("isdigit('c') = %d, isdigit('1') = %d\n", isdigit('c'),isdigit('1'));
     printf("sin(x) = %f\n", sin(0.5));
    
      return 0;
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Linking has nothing to do with include files [except in MSVC, where #pragma lib(...) can be used to avoid the IDE/Makefile having to know about the libraries].

    In particular regarding linking, printf, isXXXX and sin [in the gcc-mingw case - math routines are in libm in Linux/Unix environments] are in the C library.

    Of course, if your header file does something like:
    Code:
    #define printf our_local_printf
    then you wouldn't get it linking. But most header-files do not do that. [Windows.h does - it looks if you are using ANSI or UNICODE mode, and defines Func as FuncA or FuncU respectively - so if you call CreateFile(...) without including windows.h, it will complain that it is not able to find CreateFile, even if you link with the correct library].

    It is also likely that sin is working correctly simply because the compiler has a builtin knowledge of that function and the fact that it returns a double rather than integer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The linker typically scourges all .lib files it knows of to find the functions and put the correct address in and so. However, the linker does not care about types. So whether you pass int or double, or whatever, the linker says nothing.
    The compiler is responsible for that part. And if it does not have a prototype, it will guess, which usually results in bad things™.
    Further, implicit declarations are deprecated in C90 and errors in C99 and C++, so using them is NOT a good idea™. So consider treating those warnings as errors and you'll be fine
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Awesome thanks

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    The linker typically scourges all .lib files it knows of...
    pedantically, I would like to point out that the linker "scourges" the library files that is has been given.

    If you use "gcc -v xx.c", it shows you what gcc does "behind the curtain":
    Code:
    d:/gcc_mingw/bin/../libexec/gcc/mingw32/3.4.5/collect2.exe -Bdynamic 
    d:/gcc_mingw/bin/../lib/gcc/mingw32/3.4.5/../../../crt2.o 
    d:/gcc_mingw/bin/../lib/gcc/mingw32/3.4.5/crtbegin.o 
    -Ld:/gcc_mingw/bin/../lib/gcc/mingw32/3.4.5 
    -Ld:/gcc_mingw/bin/../lib/gcc 
    -Ld:/gcc_mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../mingw32/lib 
    -Ld:/gcc_mingw/bin/../lib/gcc/mingw32/3.4.5/../../.. d:/Temp/ccGkbaaa.o -lmingw32 
    -lgcc -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 
    -lgcc -lmoldname -lmingwex -lmsvcrt d:/gcc_mingw/bin/../lib/gcc/mingw32/3.4.5/crtend.o
    And yes, those libraries are repeated on purpose - that is because some libraries include other libraries object code, and the linker only looks for "now unknown references" - so to resolve those, the library has to be included twice.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    cool I didn't know u can see all that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile and includes
    By Lang in forum C Programming
    Replies: 4
    Last Post: 03-18-2008, 03:29 AM
  2. dvv-cpp not able to compile?
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 10-23-2004, 06:36 PM
  3. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 06:00 AM
  4. #includes
    By RedLenses in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2002, 03:59 PM
  5. Replies: 4
    Last Post: 11-14-2001, 10:28 AM