Thread: though no external prototype, no header included, function "seen"

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    though no external prototype, no header included, function "seen"

    So firstly I want to ask why this:

    add.c :
    Code:
    int add(int a,int b){
            return a+b;
    }
    main.c :
    Code:
    #include<stdio.h>
    
    int main(){
            int a=3,b=7;
            printf("a+b=%d\n",add(a,b));
    }
    is getting compiled as it is, without including a header with the prototype of add by

    $gcc add.c main.c

    And secondly why when including custom headers, mean like "header.h" not in <>, when compiling have to give as arguments all the .c files and not just the one containing the main, like we do when using existing headers? are the .c files "of the" existing headers already compiled somewhere and just get linked of some kind?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but if you add -Wall to your compiler line, you'll get at least one warning for the lack of prototype.

    Also, the code wouldn't actually work if you change the return type to float (and format to %f) - it would print garbage. [And give you more warnings if you did enable warnings].

    And secondly why when including custom headers, mean like "header.h" not in <>, when compiling have to give as arguments all the .c files and not just the one containing the main, like we do when using existing headers? are the .c files "of the" existing headers already compiled somewhere and just get linked of some kind?
    Ehm, not quite directly related here: <something.h> and "someother.h" is just an indication (or a hint) from the programmer where the header file is found - in the end, you can use "stdio.h" and <myheader.h> - although if you ues <myheader.h> you may need to add "-I." to the compiler line, as <> includes are normally not taken from the current directory.

    The linking of the standard library is done by gcc as a default - unless you specify -nostdlib [or something similar]. So printf, scanf, fopen, rand, strcpy, etc are automatically linked to your application [although the linker knows which you use and which aren't used, so only those that you use are included in your application].

    When it comes to other functions, you have to tell the compiler that those functions exist, and where they are. Either in the form of a .c file, or by compiling each module separately [using -c to just compile that unit, and not link it into an application], and then specify for example
    Code:
    gcc -o main main.o add.o
    to produce the final executable.

    This is more useful when your application starts to be a bit larger, and compiling ALL of the code takes a significant time.

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

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by alcaseltz View Post
    is getting compiled as it is, without including a header with the prototype of add
    Because prototypes are not required in the C language. The compiler should have given a warning, though.

    And secondly why when including custom headers, mean like "header.h" not in <>
    The <header.h> syntax means to scan for the header file in all include paths, including the system include path. The "header.h" syntax means to do all that, PLUS, also look in the current directory.

    when compiling have to give as arguments all the .c files and not just the one containing the main, like we do when using existing headers?
    Because a .c file is not a header file. All .c files must be compiled.

    are the .c files "of the" existing headers already compiled somewhere and just get linked of some kind?
    The standard headers are part of the standard library. The standard library already exists, you don't have to compile anything. But obviously, you must compile your OWN code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM