I'm not sure what programming in C really means: - Programming in pure C with a C compiler or - programming in C with a C++ compiler.
Apart from the differences between the C's syntax of C and the C's syntax of C++, can I safely say there are absolutely (or in very few cases) no differences between two executables in terms of performance ?
I'm thinking about this issue, because in game programming, each one of the rendering part, the game object part and the game scripting part can be programmed completely different languages, to obtain the best compromise between execution speed and easy development, and this at each one of those part.
This separation between parts can be important for me, for example, I want to make a versatile 3D adventure engine, where a community would make their own gameplay without having to mess with the engine. It would only be able to make games with a single character and several ennemies, so different game type would be covered: hack & slash, infiltration, RPG, platform, etc.
I should have put this 2 paragraphs in gamedev.stackexchange, but the first part is only about languages...
C is a basic version of a programming language and supports only primitive, fixed data types. Besides built-in data types, C++ also supports user-defined data types. C++ is an enhanced version of C and supports generic data types.
In a nutshell, the main difference between C and C++ is that C is function-driven procedural language with no support for objects and classes, whereas C++ is a combination of procedural and object-oriented programming languages.
Compilers analyze and convert source code written in languages such as Java, C++, C# or Swift. They're commonly used to generate machine code or bytecode that can be executed by the target host system.
There are a lot of minor nitpicks. One that strikes me as being the most obvious is that in C++, you have to cast the return value of malloc
. Also structs are automatically typedefed in C++.
Always use a C compiler for C code, not C++. C++ isn't perfectly compatible with C.
A few others:
void func();
declares a function that hasn't specifed what its arguments are, whereas in C++, void func();
is equivalent to the C void func(void)'
, taking no arguments;'a'
) is int
in C and char
in C++;char []
in C and const char []
in C++;class
, are reserved keywords in C++.For all those who don't believe me and are downvoting, check out this C code:
#include <stdlib.h>
int main(int argc, char **argv) {
int *i = malloc(sizeof(int));
return 0;
}
Compilation under gcc
is fine, but compilation under g++
gives the following errors:
test.c: In function `int main(int, char**)':
test.c:4: error: invalid conversion from `void*' to `int*'
Note: The differences between C and C++ syntax are already described on other posts... Still, something bothered me enough to prompt the following answer:
If I understood correctly, you want to have two separate parts in a program, one in C and one in C++. One supposed should have to be really fast, and the other could be slower.
In the current case (comparing C and C++ performance), there will be no visible difference if the same C code is compiled in with a C and in C++ compiler...
Of course, never underestimate how the skills of the programmer are important for the performance of a program, no matter the language.
void *
counterparts).void *
, etc.). Fact is, the "minor details" above are considered as dangerous, which is why they generate errors or warnings on a C++ compiler.extern "c"
specifier.void *
(note that this is not supposed to happen often, or even at all, in C++, so this a negligible problem when compared with potential casting errors)By C/C++, I mean code that will be correctly understood by both C and C++ compilers. While your language of choice could vary, those compatible C/C++ header will be the same (even if you code in C++ and will provide additional C++ headers for C++ users of your code)
extern "C"
specifier, wrapped with #ifdef __cpluplus
. This will make sure a C++ compiler will know those functions are exported as C functionsnamespace
or class
or template
is a bad idea)extern "C"
specifier, wrapped with #ifdef __cpluplus
. This will make sure a C++ compiler will know those functions are to be exported as C functionsI found the following page quite interesting, as it lists the differences between C (including C99) and C++:
http://david.tribble.com/text/cdiffs.htm
As for C99 features missing from C++, you can read my answer to the question What can be done in c but not c++?: I describe C++ features easily replacing those C99 features.
Anyway, if C++ is considered fast and robust enough for the F-35, then it should be enough for you.
Much of the F-35's software is written in C and C++ because of programmer availability; Ada83 code also is reused from the F-22.
Source: Wikipedia : https://en.wikipedia.org/wiki/Lockheed_Martin_F-35_Lightning_II
So, if you need to choose, then choose your language because you like it, or because one as something the other has not. But not because of supposed performance difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With