Coding statements before declaration-a doubt on standard!!!

S

s.subbarayan

Dear all,
Whats the notion behind C standard preventing declaration in middle
of statements?While C++ allows this will it not be good if this
supported in C also?
Also theres some point if we refer to some variable which is not
declared so preventing of in between declarations can be supported.But
what if i simply want to do printf "hello world" in between
declarations?
expecting wishful thoughts from beautiful minds,
Regards,
s.subbarayan
 
R

Richard Bos

Whats the notion behind C standard preventing declaration in middle
of statements?

It can often be easier to maintain. You don't have to go looking through
all your code to find a declaration; you know it will be in one of a
limited number of places, usually at the top of the current function or
in a header.
While C++ allows this will it not be good if this supported in C also?

As of C99, it _is_ supported. I'm not completely sure this was a good
idea, either.
Also theres some point if we refer to some variable which is not
declared so preventing of in between declarations can be supported.

I'm sorry, but I couldn't parse that sentence.
But what if i simply want to do printf "hello world" in between
declarations?

Why would you want to do that?

Richard
 
K

Kelsey Bjarnason

[snips]

As of C99, it _is_ supported. I'm not completely sure this was a good
idea, either.

Actually, I really like this feature. While it does mean variable
definitions are scattered about, it also means that they tend to be
(or at least, _should_ tend to be) more closely linked to the code that
uses them:

void func(void)
{
int i;
/* 20 lines of code */
i = init();
use(i);
}

vs

void func(void)
{
/* 20 lines of code */
int i = init();
use(i);
}

Matter of preference, perhaps, and you can "fake it" using block scoping,
but I find this approach, on the whole, cleaner than the traditional C
approach.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top