Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dalik

How to code C in a C++ program?

This topic is 5572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As I am learning more about C++, and I have been seeing C code is much faster then C++ code in most things. How would I opt say the Drawing routines in C while having an OOP program. So in other words how would I code in C in a C++ source file? Or would I just write my functions in C and include the source files and call the functions from the C++ source file? Or do I call the C headers and write my code that way inthe C++ source file? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Dalik
As I am learning more about C++, and I have been seeing C code is much faster then C++ code in most things.
Could you please show us what code that might be?
quote:
How would I opt say the Drawing routines in C while having an OOP program.
Drawing routines are in most (if not all) cases identical in C and C++
quote:
So in other words how would I code in C in a C++ source file?
Take a guess
quote:
Or would I just write my functions in C and include the source files and call the functions from the C++ source file? Or do I call the C headers and write my code that way inthe C++ source file?
That does not make sense to me.

Where do these concerns of your''s come from? Some "features" of C++ may make a program less efficient than a C program, because you don''t use these features in C. When it boils down to graphics, about the only thing to stay away from is virtual functions inside tight loops.


Share this post


Link to post
Share on other sites
quote:

and I have been seeing C code is much faster then C++ code in most things


Sorry to disappoint you, but you have seen wrong. The speed is not in the language, but in how you use it. If you can''t write fast enough code in C++, then you can''t write fast enough code in C either.

Share this post


Link to post
Share on other sites
I have to make this kinda fast but if you have CODE COMPELTE (a book)there are many many examples of benchies where C code is faster then C++ code. optimized of course and even unoptimized.

This was a waste of your time, since my concern wasnt which one is faster. I was asking how do you write C code in a C++ program. Or I could have asked how do you write C code in a pascal suorce? Or Ada in a java program. I know you can write C++ struct code and thats fine but again your using C++ headers and if your using C++ headers your using C++ code. So in other words would you just include C headers and you would get C code? Am I thinking right? Of do I have to write the code ina seperate source file and include the functions in the C++ program?

Thank you.

Share this post


Link to post
Share on other sites
It''s not so much that C++ is slower than C, it''s more that object oriented is slower than procedural (and that includes using objects like file streams over the C methods). Although it''s usually almost as fast, except maybe on embedded systems (where speed really matters).

Overall if you are just learning C++ you are probably not programming anything that requires a lot of speed. Even if you used pure C/procedural, you probably wouldn''t notice an increase anyway.

Share this post


Link to post
Share on other sites
You''re asking this like they are 2 completely unrelated languages. Since C++ is a subset of C you can use them together with little or no problems. For example I prefer C style file i/o (FILE, fopen, etc) so in my classes that read/write to files use the C file i/o methods/structures.

Here''s a simple generic example:

#include <stdio.h>
#include <string>
using std::string;

class CPlayer
{
private:
FILE *fData;
int iStrength;
string sFilename;
public:
CPlayer();
~CPlayer();
void CreateGeneric(const char *filename);
};

void CPlayer::CreateGeneric(const char *filename)
{
iStrength=100;
sFilename=filename;
fData=fopen(sFilename.c_str(),"wb");
fwrite((void *)&iStrength,sizeof(int),1,fData);
fflush(fData);
fclose(fData);
}

Perfectly legal mix of C and C++ in a *.cpp file.

MSVC++ 6
DirectX 7

Share this post


Link to post
Share on other sites
quote:
Original post by Dalik
I have to make this kinda fast but if you have CODE COMPELTE (a book)there are many many examples of benchies where C code is faster then C++ code. optimized of course and even unoptimized.

That book was written over 10 years ago. C++ compiler technology has advanced a lot in that time.



"If there is a God, he is a malign thug."
-- Mark Twain

Share this post


Link to post
Share on other sites
Well if you write C-legal code and functions in your C++ project, then it is my understanding that today''s C++ compilers will usually generate code that is as efficient as C-only compilers would generate, or at least so close as to be trivial.

If you''re not convinced that is the case, you could generate libraries using a C compiler and then link them into your C++ project.

Maybe the extern "C" declaration would make a performance difference too, but I kinda doubt it.

Value of good ideas: 10 cents per dozen.
Implementation of the good ideas: Priceless.

Proxima Rebellion - A 3D action sim with a hint of strategy

Share this post


Link to post
Share on other sites
Any C statement is also a legal C++ statement, so just compile everything with a C++ compiler and it should work out fine. Writing C code in a C++ program is not at all the same as writing C code in a pascal program. I think you're a little confused as to what C++ is. It's just C with some additional functionality. Also, it won't necessarily speed up your program. The main determining factor of your program's speed is the algorithms you use. I believe, and if someone has evidence to the contrary, let me know, that if you write a program in C and compile it with a C compiler, your machine code would be almost identical to the code produced by a C++ compiler. They didn't take out optimization techniques in C++ compilers that are present in C compilers.

[edited by - kdogg on March 16, 2003 2:32:03 PM]

Share this post


Link to post
Share on other sites
quote:
Dalik
As I am learning more about C++, and I have been seeing C code is much faster then C++ code in most things.



No.

quote:
Original post by kdogg
Any C statement is also a legal C++ statement



No.

Anyway, here''s "the answer"

  
/* foo.h */
#ifdef cplusplus

extern "C"
{
#endif


int my_c_function();

#ifdef cplusplus

}
#endif



  
/* foo.c */
#include "foo.h"

int my_c_function()
{
return 0;
}


  
/* bar.cpp */
#include "foo.h"

// When you mix C and C++, main() MUST be in a C++ file

int main()
{
return my_c_function();
}


Anyway, since you''re likely to be using a combined C/C++ compiler, don''t expect the code compiled as C to outperform the code compiled as C++.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!