Thread: Beginners question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Beginners question

    Just learning a bit of c. I've read about import statements so I'm wondering why this is working even though I haven't imported stdio.h into the program -

    Code:
    main()
    {
           printf("this is a test");
    }

    It prints out fine. The compiler is gcc - is gcc automatically importing the standard library ?
    Cheers,
    James

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I don't know, but you should really be using

    Code:
    int main()
    {
           printf("this is a test");
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so I'm wondering why this is working
    Luck. Your compiler just happened to pick the right function to call.

    >is gcc automatically importing the standard library ?
    Headers don't contain executable code, only declarations. We include stdio.h because it's a pain in the ass to memorize and write inline all of the prototypes for standard functions. This is perfectly legal and is guaranteed to work in C89:
    Code:
    int printf ( const char *format, ... );
    
    int main ( void )
    {
      printf ( "Hello, world!\n" );
      return 0;
    }
    What I did was exactly what including stdio.h would do, which is declare printf so that this code can compile. The "import" you're thinking of is a part of the link step, where the object code for the functions declared in a header is located and function calls in your code are resolved into calls to the appropriate object code. When you build your program, the compiler will generally link with the common libraries automatically, which is why you didn't have to do anything special.

    If you turn up your warnings, you'll be told that printf is being used without being declared, that main doesn't have a return value (and also probably that implicit int for main is a bad practice). In other words, always compile with the highest warning level.
    My best code is written with the delete key.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You don't HAVE to have prototypes, and as explained in previous replies, the standard C library is included by the linker by default [and even if you have a main that is COMPLETELY EMPTY, you get some functions dragged in from the C-library, such as the C-startup code that comes BEFORE main].

    The problem with not having prototypes is things like this:

    Code:
    int main() {
       float f;
       f = foo(4);
       printf("f 0 %f\n", f);
       return 0;
    }
    
    float foo(float f)
    {
        return f * f;
    }
    Since the compiler thinks that 4 is a integer, it will assume that foo takes a int argument. Later on it implements the function foo with a float argument. The consequence is that a int is interpreted as a float, which will make it zero - and you won't get 16.0 as a result, but 0.0.

    --
    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.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You don't HAVE to have prototypes
    If we're talking about printf, lack of a prototype is always undefined. Functions with variable arguments must have a prototype.
    My best code is written with the delete key.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Prelude View Post
    >You don't HAVE to have prototypes
    If we're talking about printf, lack of a prototype is always undefined. Functions with variable arguments must have a prototype.
    Yes, you are right. And in some instances, it's even quite likely that printf _WILL_ fail under the circumstance of no prototype, such as x86_64 on Linux - it uses the register AL to indicate how many floating point arguments are in registers, if I remember correctly.

    --
    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.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it uses the register AL to indicate how many floating point arguments are in registers
    That depends on the implementation, but you've certainly made the point that not prototyping functions is a Bad Thing(TM).
    My best code is written with the delete key.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Prelude View Post
    That depends on the implementation, but you've certainly made the point that not prototyping functions is a Bad Thing(TM).
    As far as I'm aware, there is only one ABI implemented for Linux on x86_64, and I'm 99% sure it uses the register AL as "number of floating point registers (SSE) that holds values for printf" [and of course any other varargs function that takes floating point values as arguments].

    --
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >As far as I'm aware, there is only one ABI implemented for ...
    I wasn't attacking your example, no worries.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    Thanks guys. It's making a bit more sense. I've only really programmed in c# and java so i'm trying to work out what happens at system level by learning c. I guess reading a book on how compilers and linkers work will be useful. Well back to it, page 38 of K & R.
    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 05:53 PM
  2. beginner's question :D
    By kingliaho in forum C Programming
    Replies: 5
    Last Post: 10-17-2008, 05:20 PM
  3. beginner's question about C
    By Roberto Llovera in forum C Programming
    Replies: 5
    Last Post: 02-26-2004, 05:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM