newbie: C decls Vs. C++ decls.

P

Prashanth

Hi,

Why does C put the restriction that the variables should always be
declared at the start of the block? Is there any gain in efficiency by
doing that? How does C++ allow variables to be declared anywhere
unlike C?

Thanks,
Prashanth.
 
J

Jon Bell

Why does C put the restriction that the variables should always be
declared at the start of the block?

Perhaps you ought to ask that next door in comp.lang.c.
 
E

E. Robert Tisdale

Prashanth said:
Why does C restrict variable declaration
to the beginning of a block.

This restriction has been removed
with the latest ANSI/ISO C 99 standard.
You can put declarations where you need them
just as you can with C++.
Is there any gain in efficiency by doing that?
No.

How does C++ allow variables to be declared anywhere unlike C?

The question is moot.
 
R

Ron Natalie

Prashanth said:
Hi,

Why does C put the restriction that the variables should always be
declared at the start of the block? Is there any gain in efficiency by
doing that? How does C++ allow variables to be declared anywhere
unlike C?

C was always that way. The compilers were originally pretty simple and
it was easier to know you could emit the code for the block when you came
to the first non-declaration.

Obviously Bjarne thought it was a rather trivial extension to make it
allowed at
an arbitrary location.
 
J

Jerry Coffin

Hi,

Why does C put the restriction that the variables should always be
declared at the start of the block? Is there any gain in efficiency by
doing that? How does C++ allow variables to be declared anywhere
unlike C?

It should not normally have any effect on the generated code -- any
efficiency gain is strictly during compilation. When C was new, it was
a struggle to get the compiler to run in the memory space available at
the time. Dennis Ritchie has told about times that he had no choice but
to completely remove features, because they bloated the compiler beyond
the available memory space.

By the time the feature was added to C++, typical memory space had grown
from the 64-128K range up to over a megabyte, which made it much easier
to add quite a number of new features. As of the 1999 standard, this
feature was added to C as well.
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top