code that compiles in c but not in c++?

A

al.cpwn

Hi,is there a code that can compile in c but not in c++ and does not
use any c++ keywords as identifiers? I suspect using void* works in c
but not in c++. Any suggestions?
 
R

Robert Gamble

Hi,is there a code that can compile in c but not in c++ and does not
use any c++ keywords as identifiers?

Yes, there are plenty of examples, here is a simple one:

int * iptr = malloc(10); /* Cast required in C++, frowned on in C */

C and C++ are two different languages and I wouldn't recommend trying
to write code that works in both. However, if you need to know how to
write C++ compatible C code, check out "C: A Reference Manual" fifth
edition.

Robert Gamble
 
A

A. Sinan Unur

(e-mail address removed) wrote in @i40g2000cwc.googlegroups.com:
Hi,is there a code that can compile in c but not in c++ and does not
use any c++ keywords as identifiers? I suspect using void* works in c
but not in c++. Any suggestions?

Trivial:

D:\Home\asu1\UseNet\clc> cat t.c
#include <stdlib.h>

int main(void) {
char *p = malloc(100);
*p = 42;
return 0;
}

D:\Home\asu1\UseNet\clc> gcc -Wall -W -c t.c

D:\Home\asu1\UseNet\clc> g++ -Wall -W -c t.c
t.c: In function `int main()':
t.c:4: error: invalid conversion from `void*' to `char*'
 
C

CBFalconer

Robert said:
Yes, there are plenty of examples, here is a simple one:

int * iptr = malloc(10); /* Cast required in C++, frowned on in C */

C and C++ are two different languages and I wouldn't recommend
trying to write code that works in both. However, if you need to
know how to write C++ compatible C code, check out "C: A Reference
Manual" fifth edition.

And talk to P.J. Plauger, the only contributor here who is known to
have a similar need.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
H

Herbert Rosenau

(e-mail address removed) wrote in @i40g2000cwc.googlegroups.com:


Trivial:

D:\Home\asu1\UseNet\clc> cat t.c
#include <stdlib.h>

int main(void) {
char *p = malloc(100);
*p = 42;
return 0;
}

D:\Home\asu1\UseNet\clc> gcc -Wall -W -c t.c

D:\Home\asu1\UseNet\clc> g++ -Wall -W -c t.c
t.c: In function `int main()':
t.c:4: error: invalid conversion from `void*' to `char*'

This is a C program and will compile using a C compiler. You have to
change something to make a C++ from. Go out of the door and then left.
There is a group discussing ANSI C++. Ask there how to use memory
assignment in C++. C and C++ are highly different languages even as
some of theyr basics identical.


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
A

A. Sinan Unur

This is a C program and will compile using a C compiler. You have to
change something to make a C++ from. Go out of the door and then left.
There is a group discussing ANSI C++. Ask there how to use memory
assignment in C++. C and C++ are highly different languages even as
some of theyr basics identical.

The OP asked to see code that would compile as C, but not as C++. I
provided an example.

What is your problem?

Sinan
 
D

Dave Thompson

Hi,is there a code that can compile in c but not in c++ and does not
use any c++ keywords as identifiers? I suspect using void* works in c
but not in c++. Any suggestions?

void* itself works in C++, but implicit 'down' conversion from it does
not. (Although some of the things you would use cv void* for in C,
like generic lists, might be _better_ done in C++ with templates.)

For a complete (I believe) list of things in C90/95 that don't work or
are different in C++(98) see Annex C.1 of the C++ standard. If you
can't afford the USD18 to buy it from ANSI, there are several draft
versions available in the 'papers' section of the committee website
http://www.open-std.org/jtc1/sc22/wg21/ . You can't rely on these
being exactly correct or the same as the official standard everywhere,
but for answering general questions they are good enough. Be sure to
consider as you read whether there are any features of C++ you think
should be changed, or described (documented) differently, so that your
download counts as part of the standards development process. <G?>

The obvious and most widespread are oldstyle (nonprototype) function
definitions and declarations, and implicit int and implicit function
declaration. There are more obscure ones like:
char x [sizeof('x') - 1]; /* legal on most (not all) C
implementations but never on C++ */

C99 adds a number of features that are not in C++, at least not yet as
far as I have tracked. The big ones that spring to mind: long long
types, literals, operations, and formats; stdint.h, inttypes.h, and
other added formats (z, hh, etc.); complex and imaginary numbers and
operations; designated initializers and compound literals; variable
length arrays and flexible array members (of structs).

- David.Thompson1 at worldnet.att.net
 
P

P.J. Plauger

C99 adds a number of features that are not in C++, at least not yet as
far as I have tracked. The big ones that spring to mind: long long
types, literals, operations, and formats;

de facto there on many implementations, approved for the next version
of the C++ Standard
stdint.h, inttypes.h, and
other added formats (z, hh, etc.);

added with library TR1, which has been approved
complex and imaginary numbers and
operations;

complex long since there as templates, now reconciled with C-style
complex via TR1
designated initializers and compound literals;

under consideration
variable
length arrays and flexible array members (of structs).

probably will never happen, since such things are better handled
other ways in C++

You overlooked the C99 additions to the preprocessor, which have
also been picked up for the next version of C++.

Bottom line, the list of gratuitous differences *has* shrunk and
continues to shrink.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top