Any C code are valid C++ code?

J

jrefactors

Since C is a subset of C++, so any C code or C libraries (printf(),
scanf(), etc...)
are valid C++ code. Is that correct? so even though the whole program
is written in
C, but with .cpp extension, we still consider as C++ program?
Please advise. Thanks
 
J

Jonathan Bartlett

Since C is a subset of C++

C is not a subset of C++. C++ has some incompatible changes from C.
However, they are compatible enough that a lot of code runs in both.

Some incompatibilities includes:

* different linking mechanisms (this is workaroundable w/ extern C)
* different interpretation of multidimensional arrays
* many C programs include typedefs and define which override C++
keywords, and therefore aren't allowed in C++ (typedef int bool; for
example)

Jon
 
G

gianguz

The answer is 'no' in general.For example one of the basic difference
(even if you create a simple hello world program and compile it under
with a C and a C++ compiler) between C and C++ relies almost in the
name mangling mechanism. C++ uses an extended decoration method to give
the linker indications about the name resolutions.
That tecnique is not compatible with C declaration naming.
This is the reason why you have to specify extern "C" {... } around C
code to ensure
compatibility.
Obviously there are other things that makes the two language very far
even so similar! ;)

Gianguglielmo
 
A

Alex Vinokur

Jonathan Bartlett said:
Since C is a subset of C++

C is not a subset of C++. C++ has some incompatible changes from C.
However, they are compatible enough that a lot of code runs in both.

Some incompatibilities includes: [snip]

* different interpretation of multidimensional arrays
What is the difference?

[snip]
 
C

Chris Barts

Since C is a subset of C++,

Wrong. A common notion that is completely wrong.
so any C code or C libraries (printf(), scanf(), etc...)
are valid C++ code.

Not true. For example, the following valid C program is not valid C++:

#include <stdlib.h>

int main(void)
{
/* new is a reserved word in C++ */
char new, *buf;
/* Implicit conversion from void* to char* not valid in C++ */
buf = malloc(1024);
free(buf);
return 0;
}

It is the case that you can use the C standard library functions in C++
code. However, it is rarely the best way to accomplish the task.
Is that correct? so even though the whole program
is written in
C, but with .cpp extension, we still consider as C++ program?

You can consider it a C++ program when it conforms to the relevant
standards. Writing in C is not a good way to conform to those standards.
 
C

Chris Torek

Since C is a subset of C++, so any C code or C libraries (printf(),
scanf(), etc...) are valid C++ code. Is that correct?

No.

Compile the following program as a C program and run it. Then,
compile it as a C++ program and run that. Observe the different
output.

#include <stdio.h>

struct A { char c[1000]; };

int main(void) {
struct B { struct A { char c[1]; } a; char c[1]; };

printf("sizeof(struct A): %lu\n", (unsigned long)sizeof(struct A));
return 0;
}

This is, of course, a carefully-constructed example -- but real C
programs really do fail to work when compiled as C++ programs,
sometimes, because of small but significant semantic changes.

(Exercise: *why* is the output different?)
 
J

Jonathan Bartlett

What is the difference?

Apparently I was incorrect. I had thought that C allocated them in a
static block while C++ allocated them as arrays of arrays, but a little
experimentation showed my ideas to be faulty.

Jon
 
E

E. Robert Tisdale

Victor said:
Since C is a subset of C++ [...]

Wrong premise. Wrong conclusion. The answer to your subj is "no".

Practically speaking, C is a subset of C++.
There are few exceptions.
Each new C++ standard attempts to subsume each new C standard.
 
J

jcoffin

C is not a subset of C++.

The C++ standard does require support for the C library, but with a few
changes in how that support must be provided (e.g. more restrictions on
which parts can be implemented as macros).

There are other differences, some of which have been pointed out
already, but I'll add yet another bit of code to demonstrate a
difference I haven't seen pointed out yet:

char x[sizeof('a')-1];

All properly-functioning C++ compilers are guaranteed to reject this.
In theory a C compiler could reject it as well, but I've never seen one
that did.
From the opposite viewpoint, most reasonably-written C can be converted
to C++ with only minimal changes, perhaps the most obvious of which is
that in well-written C, there should not be an explicit cast on the
value returned by malloc, while C++ requires one -- though well-written
C++ will rarely use malloc at all.
 
V

Victor Bazarov

Jonathan said:
Apparently I was incorrect. I had thought that C allocated them in a
static block while C++ allocated them as arrays of arrays, but a little
experimentation showed my ideas to be faulty.

No, your ideas are correct. The incorrect part is that you think that
there is a difference between "static block" and "array of arrays".

In fact, in both languages a multidimensional array is an array of arrays.
It is also true that in both languages all elements of a multidimensional
array sit next to each other, making it what you call "a static block".

V
 
R

Richard Tobin

E. Robert Tisdale said:
Practically speaking, C is a subset of C++.

Practically speaking, I've found that's not the case. On several
occasions, people have mailed me to ask why one of my C programs
doesn't compile, and the answer has turned out to be that they were
trying to compile it as C++.

I have also had to conditionalize a .h file on defined(__cplusplus) to
allow it to be used in C++ programs without breaking backward
compatibility for existing (C) users.

-- Richard
 
E

E. Robert Tisdale

Richard said:
Practically speaking, I've found that's not the case.
On several occasions, people have mailed me to ask
why one of my C programs doesn't compile,
and the answer has turned out to be that
they were trying to compile it as C++.

I have also had to conditionalize a .h file on defined(__cplusplus)
to allow it to be used in C++ programs
without breaking backward compatibility for existing (C) users.

From which I conclude that
it isn't very difficult at all for you
to ensure that your C code will compile as C++ code
and perform as expected.
 
K

Keith Thompson

E. Robert Tisdale said:
Victor said:
Since C is a subset of C++ [...]
Wrong premise. Wrong conclusion. The answer to your subj is "no".

Practically speaking, C is a subset of C++.
There are few exceptions.
Each new C++ standard attempts to subsume each new C standard.

The number of C constructs that either are invalid C++ or are valid
C++ with different semantics is small.

The number of real-world well-written C programs that use those
constructs is much larger.
 
R

Richard Tobin

From which I conclude that
it isn't very difficult at all for you
to ensure that your C code will compile as C++ code
and perform as expected.

Possibly not, but I have no incentive to do so.

-- Richard
 
E

E. Robert Tisdale

Richard said:
Possibly not, but I have no incentive to do so.

Evidently, you don't think of the people who
"have mailed me to ask why one of my C programs doesn't compile"
as customers or employers.
 
D

Dik T. Winter

Evidently, you don't think of the people who
"have mailed me to ask why one of my C programs doesn't compile"
as customers or employers.

Probably you have not thought that the C programs involved possibly
were freeware? Obviously, if the person complaining is a customer
or an employer, there is pretty good incentive to make changes.
When somebody complains about a program I put out for free, I will
simply let it go if it is some incompatibility. Only when a (good)
suggestion for change is involved am I inclined to modify. Why
should I put in my free time in changing things that work for me as
I want?
 
V

Victor Bazarov

Dik T. Winter said:
[...] Why
should I put in my free time in changing things that work for me as
I want?

Why did you feel compelled to publish it in the first place? Can
the same reason apply to compel you to fix it? If not, was it the
_real_ reason you published your code? You don't have to answer.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top