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

will there be any issues in using C functions in CPP

arunmib
100+
P: 104
Hi all,
I have a two doubt....Will there be any issues in using standard C functions in CPP file and compiling it with CPP compiler. To make things clear I'll add an example,
Expand|Select|Wrap|Line Numbers
  1. // File name : Test.cpp
  2.  
  3. #include <iostream>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.      cout<<"\nTesting....\n";
  11.      printf("\nTesting....\n");
  12.      return 0;
  13. }
g++ -o Test.o Test.cpp

I know it compiles, what I mean is won't there any issues, some C99 standards, some blah blah stuff.....

Another doubt won't there be any issue (in the same sense as above question) In compiling a C file with a CPP compiler. For this, consider the above example file without the CPP specific features....

I think I've made myself clear....
Aug 9 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Yes, there will be issues.

C++ is not C.

For example:

void fx();l

in C this means fx is a function with arguiments specified when the funciton is first called.. In C++ is means this is a function with no arguments.

The rules for const are much tighter in C++ than C. Code than compiled in C won't compile in C++.

You can't use implied int in C++.

Some compilers will produce errors if a variable is used on a function call without having been initialized, even though it is the function call that will initialize the variable.

Old style argument declaration can also cause a C++ compiler error:
Expand|Select|Wrap|Line Numbers
  1. int MyFunction(a,b)
  2. int a,
  3. int b,
  4. {
  5.  
  6. }
  7.  
etc...

Personally, if you got it to compile in C then I wouild make it into a library and add the library to the C++ build. Just put and extern "C" around the contents of the library header.
Aug 9 '07 #2

RRick
Expert 100+
P: 463
If you compile the code under C++, you won't be able to call it from a C program (either with the object code or in a library). C++ performs name mangling on the routine's name that C++ can understand but C can't.

The way around this problem is described at the end of post #2. Putting the extern "C" declaration around the code being compiled disables the name mangling.
Aug 9 '07 #3

arunmib
100+
P: 104
Thanks weaknessforcats and RRick for the reply.....some light has been shed.....
Aug 12 '07 #4

Post your reply

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