By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

about extern c

P: 28
can any one tell me whats the use of extern c with an example
Jun 12 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
extern "C" defines a function in a C++ file with C function naming instead of the name mangling that C++ applies (in order to enable overloading). That means that the function can be called from a C module as well as a C++ module

Expand|Select|Wrap|Line Numbers
  1. // Header.h
  2.  
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6.  
  7. void printHelloWorld(void); 
  8.  
  9. #ifdef __cplusplus
  10. }
  11. #endif
  12.  
  13. // cplusplus.cpp
  14.  
  15. #include <iostream>
  16. #include "header.h"
  17.  
  18. using namespace std;
  19.  
  20. void printHelloWorld(void)
  21. {
  22.     cout << "Hello World !" << endl;
  23. }
  24.  
  25. // c.c
  26.  
  27. #include "header.h"
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31.     printHelloWorld();
  32. }
  33.  
Jun 13 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.