question on variable definitions

R

raphfrk

When I use gcc for compiling my program, I can define variables
anywhere in the program, e.g.

int a;

<code using a>

int b;

<code using b (and/or a)>

However, when I sent the code to someone else they were unable to
compile it (They are using some IDE compiler)

I vaguely remember that originally, variables needed to be defined all
at the start of the program.

Is there a simple switch that needs to be set to allow variables to be
defined anywhere?

Also, I think if I move all the variable definitions to the start of
the program, they should be able to compile, but I would rather not do
that.
 
S

santosh

raphfrk said:
When I use gcc for compiling my program, I can define variables
anywhere in the program, e.g.

int a;

<code using a>

int b;

<code using b (and/or a)>

However, when I sent the code to someone else they were unable to
compile it (They are using some IDE compiler)

I vaguely remember that originally, variables needed to be defined all
at the start of the program.

C90 requires all declarations to be at the beginning of a block. C99
allows mixed code and declarations.
Is there a simple switch that needs to be set to allow variables to be
defined anywhere?

Your compiler manual should document such a switch, if it exists.
Also, I think if I move all the variable definitions to the start of
the program, they should be able to compile, but I would rather not do
that.

Why? I personally find intermingled code and declarations to be rather
untidy. There's also the chance that some older compiler may refuse to
compile the code, (as you've apparently discovered).
 
C

Chris Dollin

santosh said:
Why? I personally find intermingled code and declarations to be rather
untidy.

Opinions differ. I find it untidy when variables are declared
when you don't have their [initial] values to hand.
There's also the chance that some older compiler may refuse to
compile the code, (as you've apparently discovered).

Portability to standard-C90 compilers is a /good/ reason to avoid
declarations-anywhere: it trumps style every time.

(Assuming you can require C90-but-not-later conformance.)
 
R

raphfrk

santosh said:
Why? I personally find intermingled code and declarations to be rather
untidy.

Opinions differ. I find it untidy when variables are declared
when you don't have their [initial] values to hand.
There's also the chance that some older compiler may refuse to
compile the code, (as you've apparently discovered).

Portability to standard-C90 compilers is a /good/ reason to avoid
declarations-anywhere: it trumps style every time.

(Assuming you can require C90-but-not-later conformance.)


Apparently, it is Visual Studio 2007 as the compiler.

This should support C-99, so it must be a switch somewhere.
 
F

Flash Gordon

raphfrk wrote, On 02/03/07 11:46:
On Mar 2, 11:35 am, Chris Dollin <[email protected]> wrote:

<snip discussion of whether and in what standard you can declare
variables anywhere>

Please don't quote people signatures, the bit I've left in above.
Apparently, it is Visual Studio 2007 as the compiler.

This should support C-99, so it must be a switch somewhere.

Should according to whom? Microsoft don't think it should, and therefore
it does not. So you will have to stick to the old C standard which is
still the most commonly implemented. Note that you do not need to
declare variables at the beginning of the program, only at the beginning
of the block, so the following is legal and will be accepted by Visual
Studio when put in an appropriate program:

{
int i;
for (i=0; i<10; i++) {
int j = 1 * 42;
if (j < 100) {
int k = j - i;
}
}
}

Very silly code, but you will note I am declaring variables at the start
of lots of blocks inside other blocks.
 
C

CBFalconer

raphfrk said:
When I use gcc for compiling my program, I can define variables
anywhere in the program, e.g.

int a;

<code using a>

int b;

<code using b (and/or a)>

However, when I sent the code to someone else they were unable to
compile it (They are using some IDE compiler)

Gcc is not necessarily a C compiler. It depends on the switches
you set. I suggest:

gcc -W -Wall -ansi -pedantic -Wwrite-strings -Wfloat-equal

(replace -ansi with -std=C99 for incomplete C99 compliance.)
However the C90 standard (-ansi) is much more likely to port to
other systems.
 
C

CBFalconer

raphfrk said:
.... snip ...

Apparently, it is Visual Studio 2007 as the compiler.

This should support C-99, so it must be a switch somewhere.

No it doesn't. Microsoft doesn't believe in standards.
 
F

Flash Gordon

CBFalconer wrote, On 02/03/07 13:10:
raphfrk wrote:
... snip ...

No it doesn't. Microsoft doesn't believe in standards.

Not true, it believes in the C90 standard and various other standard,
just not the current C standard and various other standards.
 
D

Dave Vandervies

No it doesn't. Microsoft doesn't believe in standards.

The case under discussion is not an example of that, unless you want
to claim that no other maker of any widely-used C compiler believes in
standards either.


dave
 
B

boa

Dave said:
The case under discussion is not an example of that, unless you want
to claim that no other maker of any widely-used C compiler believes in
standards either.

Please name the makers of widely-used C compilers that totally ignores C99.

TIA
Boa
 
R

raphfrk

raphfrkwrote:

... snip ...



No it doesn't. Microsoft doesn't believe in standards.

Wow, so there isn't a switch you can set to tell it to ignore the
'warning' ?
 
K

Keith Thompson

raphfrk said:
Wow, so there isn't a switch you can set to tell it to ignore the
'warning' ?

That's a question about Visual Studio 2007, not about the C language,
so we can't answer it here.

It's likely not just a matter of ignoring the warning; the compile
would actually have to correctly process code containing mixed
declarations and statements. Since that happens to be a feature of
C++ as well as C99, there *might* be a way to do it; check the
documentation or ask in a system-specific forum.

You *might* consider compiling the code as C++ rather than as C. This
approach is fraught with peril; C++ is *nearly* a superset of C, but
there are significant differences. For example, C++ disallows some
implicit conversions between void* and other pointer types that are
allowed by C, so some valid C code is illegal C++ (there are other
differences as well).
 
B

bluejack

Why? I personally find intermingled code and declarations to be rather
untidy. There's also the chance that some older compiler may refuse to
compile the code, (as you've apparently discovered).

One reason often mentioned to declare variables at the time of first
use rather than at the beginning of blocks is to put off the work
of allocation until (if) it is actually required. I believe Scott
Meyers
discusses this in a C++ context. (Effective C++ #26)

Given that C compilers are probably designed to optimize for C
programs,
this may not be an issue in C, except in pathological cases of
extremely
poor design.

I've always been partial to top-of-block declarations because it helps
give me a sense of how much work is being done in a function. A long
stack of declarations is a warning sign of bad design, in my view.

-Bluejack
 
S

santosh

bluejack said:
One reason often mentioned to declare variables at the time of first
use rather than at the beginning of blocks is to put off the work
of allocation until (if) it is actually required. I believe Scott
Meyers
discusses this in a C++ context. (Effective C++ #26)


IMO, that's something better left to the optimiser rather than the
programmer.
Given that C compilers are probably designed to optimize for C
programs,
this may not be an issue in C, except in pathological cases of
extremely
poor design.

It shouldn't be an issue if the compiler has an half-way decent
optimiser.
I've always been partial to top-of-block declarations because it helps
give me a sense of how much work is being done in a function. A long
stack of declarations is a warning sign of bad design, in my view.

Yes, I too like to see the function's objects upfront rather than bit
by bit. But I'm not insistent on any one style.
 
M

Mark McIntyre

Please name the makers of widely-used C compilers that totally ignores C99.

That wasn't whet he said. And while you're at it, find a widely used
compiler that fully supports C99...

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
I

Ian Collins

bluejack said:
I've always been partial to top-of-block declarations because it helps
give me a sense of how much work is being done in a function. A long
stack of declarations is a warning sign of bad design, in my view.
Just use the "if the function doesn't fit on my screen, it's a bad
design" rule of thumb. Although these days with decent displays, I use
1/4 of a screen!

Ian
 
N

Nelu

Mark said:
That wasn't whet he said. And while you're at it, find a widely used
compiler that fully supports C99...

Comeau, I think. Not sure if there are others.
 
B

bluejack

Just use the "if the function doesn't fit on my screen, it's a bad
design" rule of thumb. Although these days with decent displays, I use
1/4 of a screen!

Yep, I use that one too, although as with any rule of thumb, there
will be exceptions.

I used to use this rule more or less as law, until I found myself
"factoring" out chunks of code to separate functions simply to reduce
the size of individual function blocks -- even though the code so
moved is only ever called from the one place. And once you start
creating structures to pass among the functions in order to keep the
parameter list manageable... then you know you're taking it too far!

Every now and then a big chunk of work just needs to be done in one
place.

-Bluejack
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top