Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Any C code are valid C++ code?

Reply
Thread Tools

Any C code are valid C++ code?

 
 
jrefactors@hotmail.com
Guest
Posts: n/a
 
      12-10-2004
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-10-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Since C is a subset of C++ [...]


Wrong premise. Wrong conclusion. The answer to your subj is "no".
 
Reply With Quote
 
 
 
 
Jonathan Bartlett
Guest
Posts: n/a
 
      12-10-2004
(E-Mail Removed) wrote:
> 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
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
 
Reply With Quote
 
gianguz
Guest
Posts: n/a
 
      12-10-2004
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

 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      12-10-2004

"Jonathan Bartlett" <(E-Mail Removed)> wrote in message news:41b9ebca$(E-Mail Removed)...
> (E-Mail Removed) wrote:
> > 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]

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
Chris Barts
Guest
Posts: n/a
 
      12-10-2004
(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      12-10-2004
In article <(E-Mail Removed) .com>
<(E-Mail Removed)> wrote:
>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?)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Jonathan Bartlett
Guest
Posts: n/a
 
      12-10-2004

>> * different interpretation of multidimensional arrays

>
> 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
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      12-10-2004
Victor Bazarov wrote:

> (E-Mail Removed) wrote:
>
>> 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.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-10-2004
On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
<(E-Mail Removed)> wrote:

>"Jonathan Bartlett" <(E-Mail Removed)> wrote
>> * different interpretation of multidimensional arrays


>What is the difference?


C lets you blur the distinction between ** and *[ ] and [ ][ ] rather
more, especially in function calls.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
XSD: Any valid XML in one node - different parser results mkremser@gmail.com XML 5 01-20-2005 11:39 PM
Deprecated functionality with generics (previously valid code now invalid) mortoray@ecircle-ag.com Java 2 01-18-2005 08:18 PM
Any C code are valid C++ code? jrefactors@hotmail.com C Programming 67 12-20-2004 06:29 AM
The promotion code "MSUU4C8E3475" is not a valid promotion code Sam-Hong Kong MCDST 2 03-04-2004 06:47 PM



Advertisments