Thread: Why do we need this

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    Why do we need this

    Found this code in one of the header file of some project. I dont really understand why do we use this.

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    Does any one know where and why do need to include this in the header file. How does it help. Just curoius to know

    Thank you

    ssharish2005

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because the syntax and meaning of some things are slightly different between C and C++, extern "C" tells the compiler that the following definition(s) are "C" rather than "C++", so that it can use the "old" syntax and semantics. One example is:
    int foo(); in C++ means that foo takes no arguments.
    int foo(); in C means that foo is undefined how many arguments it takes.

    --
    Mats

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    int foo(); in C means that foo is undefined how many arguments it takes.
    What happens when this is the case

    Code:
    int foo(); 
    
    int main()
    {
        foo();
        
        return 0;
    }
    
    int foo()
    {
        printf("I am foo");
    }
    In this case the foo is defines. Would that still interprert foo undefined? Or where u just speaking about the prototype.

    ssharish2005

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ssharish2005 View Post
    What happens when this is the case

    Code:
    int foo(); 
    
    int main()
    {
        foo();
        
        return 0;
    }
    
    int foo()
    {
        printf("I am foo");
    }
    In this case the foo is defines. Would that still interprert foo undefined? Or where u just speaking about the prototype.

    ssharish2005
    In C with the above code would be OK to call foo(3) or foo("blah", 47, 4.5), just as well as foo(). In C++, only foo() is accepted by the compiler.

    Of course, in C, the compiler may moan about missing prototypes and such if you do this - and rightly so, I would say - but it doesn't stop old/clever code from abusing this rule - a particular example would be using "home-made variable number of arguments", something like this:
    Code:
    int sumit();
    
    int main() {
       printf("sum = ", sumit(2, 19, 23));
       printf("sum = ", sumit(3, 19, 23, 42));
    }
    
    int sumit(int n, int a, int b, int c, int d, int e) 
    {
        int sum = 0;
        switch(n) {
            case 5:
               sum += e;
            case 4:
               sum += d;
            case 3:
               sum += c;
            case 2:
               sum += b;
            case 1:
               sum += a;
               break;
            default: 
               .... do some error handling ... 
        }
        return sum;
    }
    [I haven't tested that code, but I believe it works.

    --
    Mats

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Thanks Map, i got it. And code works fine except this

    Code:
     printf("sum = %d ", sumit(2, 19, 23));
     printf("sum = %d ", sumit(3, 19, 23, 42));
    Thanks a lot

    ssharish2005

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ssharish2005 View Post
    Found this code in one of the header file of some project. I dont really understand why do we use this.
    http://www.parashift.com/c++-faq-lit....html#faq-32.4
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ssharish2005 View Post
    Thanks Map, i got it. And code works fine except this

    Code:
     printf("sum = %d ", sumit(2, 19, 23));
     printf("sum = %d ", sumit(3, 19, 23, 42));
    Thanks a lot

    ssharish2005
    "It's always the simple things that trip you up" :-)

    --
    Mats

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would like to stress that due to the undefined nature of a function such as bar(), that it's not really something that you should use extensively as a design. The normal rules about getting a compile without errors and warnings should always apply.

    bar isn't type strict, and doesn't really have a place in C.
    Code:
    void bar();
    
    ... 
    
    bar(42);
    
    void bar(const char *s)
    {
      puts(s);
    }
    Variable argument lists are useful, but there is a better way to do it: use the stuff in <varargs.h>
    Last edited by whiteflags; 09-01-2007 at 06:24 PM.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by matsp View Post
    "It's always the simple things that trip you up" :-)

    --
    Mats

    Hey, i dint ment to pick you up there. heyy i am sorry

    ssharish2005

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And I also think this wouldn't be really necessary when we save the file with .c extension. But perhaps in the header file which dosn't differentiate may be it would be necessary.

    And is it true to include this to call a C++ function in C

    Code:
    #ifdef __cplusplus
    extern "C++" {
    #endif
    
    function prototypes
    
    #ifdef __cplusplus
    }
    #endif
    ssharish2005
    Last edited by ssharish2005; 09-01-2007 at 07:51 PM.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ssharish2005 View Post
    Found this code in one of the header file of some project. I dont really understand why do we use this.
    C++ names are type-mangled. C names are not. extern "C" tells the compiler that the following name should not be type-mangled for linkage purposes. It allows C++ code to call C code.

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    ITS TO RUN C++ style fuctions to c compiler.

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by tanmay_solanki View Post
    ITS TO RUN C++ style fuctions to c compiler.
    You can't run C++ functions in a C compiler. C++ is a superset of C. You can compile C with C++ compiler, but not the other way round.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed