For example, there is a program completely written in ANSI C, if we compile it with the C compiler as the first version and in the second version we compile it with the C ++ compiler. Will there be differences in the collected file? Those. Is it possible to write code for systems that require programs written in C and then compile them using C ++ without side effects?
3 answers
Here the previous speakers described some differences of C and C ++ languages, but I will try to answer the question :)
Will there be differences in the collected file?
Of course, they will. And you yourself can easily verify this with the help of the diff utility, for example (if you set it on two corresponding binary files received from the C and C ++ compilers). Yes, and simply compare the size - they will be a little different.
Another way to see the differences is the gcc compiler -S key. If you compile the source with this key:
g++ -S main.c # компилятор С++ gсс -S main.c # компилятор С then in the same directory a file will appear with the extension .s - assembler listing of your code. Comparing this listing for the gcc and g ++ cases leads to some answer to the second question:
Is it possible to write code for systems that require programs written in C and then compile them using C ++ without side effects?
In many cases, the answer will be - yes, you can. If the code is written in ANSI C compatible with a subset of the C ++ language, then the generated assembler code will be (almost) the same for the cases of gcc and g ++. It will differ only in some binding (g ++ will add initialization of its streams, etc.). The names of the functions will also be different, but this does not matter, since the binary will contain addresses, not names.
- oneand the difference in
sizeof('a')you consider irrelevant? - VladD - @VladD, there is no difference C from C ++, which I would consider to be irrelevant :). It just seemed to me that the author of the question did not quite ask about it. - o2gy
- Simply you write “If the code is written in ANSI C, compatible with a subset of the C ++ language ...” - VladD
C ++ and C are two different languages in which even outwardly identical constructs have different semantics.
For example, in C ++ you can write
int x; int y = 10; ( x = y ) += 20; In C, you will not compile such code.
Even the usual familiar “classical” for offer and that has a difference in the definition.
And it’s quite a trivial difference: in C, string literals have types of non-constant character arrays, and in C ++ they have constant character arrays.
In addition, in C, unlike C ++, declarations are not language sentences.
I wrote about this particular difference in my personal forum .
In the C language, there are composite literals and arrays of variable length, which are absent in C ++.
On the other hand, for example, in C ++ there are links that are missing in C.
If you write in C, for example,
int *p = malloc( sizeof( int ) ); , as some programmers often advise, including on Stackoverflow and similar tips, give outrageous positive points, then you may not have enough of your whole life, so that in your project, which you want to compile, like the C ++ project, change all such sentences to
int *p = ( int * )malloc( sizeof( int ) ); On the other hand, it's a good idea to try compiling your C program like a C ++ program, because the C ++ compiler can draw your attention to the things that the C compiler swallows without noticing, but nevertheless the code is not satisfactory .
For a start, C and C ++ are different languages, the difference between them is not even in code generation, but in semantics.
The simplest example - sizeof('a') gives different results in different languages.
In addition, name mangling in C ++ usually gives other function names at the binary API level.
- The C ++ standard ensures that the size of the char is one byte. You want to say that the standard C does not guarantee this? :)) - Arkady
- 2@Arkady is a C ++ standard,
'a'is a char character literal. By standard C,'a'is a character literal of typeint. Therefore,sizeof('a') == sizeof(char)is an excellent test in C ++ vs C. - Lapshin Dmitry - @LapshinDmitry thanks, did not know) - Arkady