From: Rune Allnor on
On 10 Mai, 13:11, glen herrmannsfeldt <g...(a)ugcs.caltech.edu> wrote:
> Rune Allnor <all...(a)tele.ntnu.no> wrote:
>
> (snip)
>
> >> If you try compiling C code with a C++ compiler, it will
> >> often fail.
> > That has to do with uncompatible standards. Prior to C99
> > standard C was a subset of standard C++, to what extent such
> > standards existed and were agreed upon. Any standard-complying
> > C++ compiler would be able to compile any standard-complying
> > pre-C99 C code.
>
> One that happened to my wife once was a variable named new
> in a C program, compiled with a C compiler.  The compiler
> refused it, considering it a reserved word.

I don't remember off the top of my head if 'new' is a reserved
word in C. Assuming it's not - and also assuming that the compiler
indeed worked in C mode - that would be a bug in the compiler
rather than a C/C++ clash.

Not that it makes any difference, where and when one hits the
snag...

Rune
From: Vladimir Vassilevsky on


Rune Allnor wrote:


> I don't remember off the top of my head if 'new' is a reserved
> word in C. Assuming it's not - and also assuming that the compiler
> indeed worked in C mode - that would be a bug in the compiler
> rather than a C/C++ clash.
>
> Not that it makes any difference, where and when one hits the
> snag...

Question to C++ - ers:

for(int i = 0; i < 100; i++)
{
// do something
}

cout << i;


I've seen compilers that compile this as well as the others that won't
compile. Which ones are right?


Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
From: Andreas Huennebeck on
glen herrmannsfeldt wrote:

> Andreas Huennebeck <acmh(a)gmx.de> wrote:
>> Rune Allnor wrote:
> (snip)
>
>>> C++ is more generic, and hides some technical details from the user.
>>> C++ might be easier to use, but this brings a performance penalty.
>
>> No, it does not. In fact C++ code can be faster. Try compiling C code
>> with a C++ compiler.
>
> Object oriented code tends to do a lot of allocating and deallocaing
> of memory, which usually has a performance penalty.

This is correct, and if you do the same in C to get the same programming
comfort the penalties are the same. C++ was designed such that you
have to pay only for those features that you use. Nobody forces a C++
programmer to use libraries with dynamic memory usage if performance
is important, but a C++ programmer can use such libraries if other
features are more important than raw performance (e.g. safety, limited
time to market).

> You can write OO C code, and you can write non-OO C++ code.

And a good C programmer writes better code than a bad C++ programmer,
and vice versa, so what?

> If you try compiling C code with a C++ compiler, it will
> often fail.

This is true (e.g. when using reserved C++ keywords), but I have ported
many C source files to C++ and the work was neither difficult nor very
time consuming - anyway the time spared later while adapting the code
to new tasks was by far more.

bye
Andreas
--
Andreas H�nnebeck | email: acmh(a)gmx.de
----- privat ---- | www : http://www.huennebeck-online.de
Fax/Anrufbeantworter: 0721/151-284301
GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
PGP-Key: http://www.huennebeck-online.de/public_keys/pgp_andreas.asc

From: Rune Allnor on
On 10 Mai, 14:57, Vladimir Vassilevsky <nos...(a)nowhere.com> wrote:
> Rune Allnor wrote:
> > I don't remember off the top of my head if 'new' is a reserved
> > word in C. Assuming it's not - and also assuming that the compiler
> > indeed worked in C mode - that would be a bug in the compiler
> > rather than a C/C++ clash.
>
> > Not that it makes any difference, where and when one hits the
> > snag...
>
> Question to C++ - ers:

No i's declared above?

> for(int i = 0; i < 100; i++)
>   {
>   // do something
>   }
>
> cout << i;
>
> I've seen compilers that compile this as well as the others that won't
> compile. Which ones are right?

If a variable i has been declared before the loop,
this should compile, but the i inside the loop hides
the i outside the loop.

If there are no i's declared before the loop, the above
is at least undefined behaviour, maybe also illegal.

You should ask this question in one of the C++ groups.

Rune
From: Muzaffer Kal on
On Mon, 10 May 2010 07:57:34 -0500, Vladimir Vassilevsky
<nospam(a)nowhere.com> wrote:

>
>
>Rune Allnor wrote:
>
>
>> I don't remember off the top of my head if 'new' is a reserved
>> word in C. Assuming it's not - and also assuming that the compiler
>> indeed worked in C mode - that would be a bug in the compiler
>> rather than a C/C++ clash.
>>
>> Not that it makes any difference, where and when one hits the
>> snag...
>
>Question to C++ - ers:
>
>for(int i = 0; i < 100; i++)
> {
> // do something
> }
>
>cout << i;
>
>
>I've seen compilers that compile this as well as the others that won't
>compile. Which ones are right?

The 'i' integer in the for loop exists only in the context of the for
loop and hides an existing 'i' variable if one existed. If there is a
'i' variable before the loop was encountered this code section would
compile; if not, it should give you an undeclared variable error. If
there is no 'i' before the for loop and no error, you have a
non-complying C++ compiler.
--
Muzaffer Kal

DSPIA INC.
ASIC/FPGA Design Services

http://www.dspia.com