Thread: extern issue

  1. #1
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43

    extern issue

    Hi all,

    I have worked with extern variable(s) a little. But, how does the extern function(s) work. Here is a sample code which i recently saw. Can anyone tell me why the compiler is not complaining about the usage and how does this code work

    Test1.c file source code
    Code:
    //test1.c
    #include <stdio.h>
    
    void dummy(int i,int j)
    {
            printf("I am inside the dummy function with value %d and %d\n",i,j);
    }
    Test2.c file source code
    Code:
    //test2.c
    int main()
    {
            extern void dummy(int i); //Here the arguments are mismatched
    
            dummy(2);
            return 0;
    }
    Compiled it using the following gcc command

    gcc -Wall -o extern test1.c test2.c

    and received not even a warning and when i tried to run this code

    ./extern
    I am inside the dummy function with value 2 and -1075886112

    How is this working. Any clues.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I think the result is undefined. That is; it may very well crash your application...

  3. #3
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Thanks EVOEx, but some of my friends suggest that extern functions act like function pointers. Any idea on that?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "extern" on a function prototype is useless, since function prototypes are extern by default (unless they're inline). Notice that in your example, if you remove "extern" from your example it compiles exactly the same and works exactly the same. C can't save you if you lie to it with the prototypes.

  5. #5
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    tabstop thanks for your explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C#] Intercept SysListView32 item added
    By Devils Child in forum Windows Programming
    Replies: 9
    Last Post: 03-26-2010, 07:29 AM
  2. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM

Tags for this Thread