473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question on variable definitions

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.

Mar 2 '07 #1
23 1924
raphfrk wrote:
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).

Mar 2 '07 #2
santosh wrote:
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.)

--
Chris "electric hedgehog" Dollin
A rock is not a fact. A rock is a rock.

Mar 2 '07 #3
On Mar 2, 11:35 am, Chris Dollin <chris.dol...@hp.comwrote:
santosh wrote:
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.)

--
Chris "electric hedgehog" Dollin
A rock is not a fact. A rock is a rock.

Apparently, it is Visual Studio 2007 as the compiler.

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

Mar 2 '07 #4
raphfrk wrote:
>
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 2 '07 #5
raphfrk wrote:
>
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 2 '07 #6
raphfrk wrote, On 02/03/07 11:46:
On Mar 2, 11:35 am, Chris Dollin <chris.dol...@hp.comwrote:
<snip discussion of whether and in what standard you can declare
variables anywhere>
>--
Chris "electric hedgehog" Dollin
A rock is not a fact. A rock is a rock.
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.
--
Flash Gordon
Mar 2 '07 #7
CBFalconer wrote, On 02/03/07 13:10:
raphfrk wrote:
... 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.
Not true, it believes in the C90 standard and various other standard,
just not the current C standard and various other standards.
--
Flash Gordon
Mar 2 '07 #8
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>raphfrk wrote:
>>
... 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.
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

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Personally, I'd prefer to persuade the customers not to be so damn silly, and
then give them a few swift slaps on the ear; but perhaps you are one of those
strange people who prefers to /keep/ your customers. --Richard Heathfield, CLC
Mar 2 '07 #9
boa
Dave Vandervies wrote:
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>raphfrk wrote:
... 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.

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
Mar 2 '07 #10
On Mar 2, 1:10 pm, CBFalconer <cbfalco...@yahoo.comwrote:
raphfrkwrote:

... 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.
Wow, so there isn't a switch you can set to tell it to ignore the
'warning' ?
Mar 2 '07 #11
"raphfrk" <ra*****@netscape.netwrites:
On Mar 2, 1:10 pm, CBFalconer <cbfalco...@yahoo.comwrote:
>raphfrkwrote:

... 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.

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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 2 '07 #12
On Mar 2, 3:12 am, "santosh" <santosh....@gmail.comwrote:
raphfrk wrote:
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).
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

Mar 2 '07 #13

bluejack wrote:
On Mar 2, 3:12 am, "santosh" <santosh....@gmail.comwrote:
raphfrk wrote:
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).

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.

Mar 2 '07 #14
On Fri, 02 Mar 2007 18:47:22 +0100, in comp.lang.c , boa
<bo*****@gmail.comwrote:
>Dave Vandervies wrote:
>In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>>raphfrk wrote:
... 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.

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.
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
Mar 3 '07 #15
bluejack wrote:
>
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

--
Ian Collins.
Mar 3 '07 #16
Mark McIntyre wrote:
On Fri, 02 Mar 2007 18:47:22 +0100, in comp.lang.c , boa
<bo*****@gmail.comwrote:
>Dave Vandervies wrote:
>>In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
raphfrk wrote:
... 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.
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.

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.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Mar 3 '07 #17
On Mar 2, 4:16 pm, Ian Collins <ian-n...@hotmail.comwrote:
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

Mar 3 '07 #18
Nelu wrote:
Mark McIntyre wrote:
On Fri, 02 Mar 2007 18:47:22 +0100, in comp.lang.c , boa
<bo*****@gmail.comwrote:
Dave Vandervies wrote:
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
raphfrk wrote:
... 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.
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.
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.
>From the Standard library side, dinkumware C and C++ library claims
complete implementation of and conformance to C99.

Mar 3 '07 #19
boa wrote:
Dave Vandervies wrote:
>CBFalconer <cb********@maineline.netwrote:
>>raphfrk wrote:

... 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.

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.
Microsoft.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 3 '07 #20
boa
CBFalconer wrote:
boa wrote:
>Dave Vandervies wrote:
>>CBFalconer <cb********@maineline.netwrote:
raphfrk wrote:

... 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.
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.

Microsoft.
LOL.

Not quite the answer I was looking for, but still excellent. ;-)

Boa
Mar 3 '07 #21
boa
Mark McIntyre wrote:
On Fri, 02 Mar 2007 18:47:22 +0100, in comp.lang.c , boa
<bo*****@gmail.comwrote:
>Dave Vandervies wrote:
>>In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
raphfrk wrote:
... 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.
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.

That wasn't whet he said. And while you're at it, find a widely used
compiler that fully supports C99...
It's not a question about support levels, but about policies. Microsofts
policy is to ignore C99, other compiler vendors have implemented C99 in
various degrees.

It would be sad if clc regulars ignore C99 just to be able to support
Microsoft... (Yes, I have asbestos clothing, bring it on :-))

Boa
Mar 3 '07 #22
boa wrote, On 03/03/07 07:25:
Mark McIntyre wrote:
>On Fri, 02 Mar 2007 18:47:22 +0100, in comp.lang.c , boa
<bo*****@gmail.comwrote:
>>Dave Vandervies wrote:
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
raphfrk wrote:
... 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.
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.
That wasn't whet he said. And while you're at it, find a widely used
compiler that fully supports C99...

It's not a question about support levels, but about policies. Microsofts
policy is to ignore C99, other compiler vendors have implemented C99 in
various degrees.
As far as I can see TenDRA only mentions C90
http://www.ten15.org/docs/tdfc/ and I believe the same applues to
PacificC http://www.htsoft.com/products/PACIFICc.php

I've no idea how widely used they are.
It would be sad if clc regulars ignore C99 just to be able to support
Microsoft... (Yes, I have asbestos clothing, bring it on :-))
I think that even the regulars that deliberately avoid using C99
specific features are not ignoring C99. If nothing else they try to
avoid doing things that are no longer legal in C99 ;-)
--
Flash Gordon
Mar 3 '07 #23
On Mar 2, 11:07 am, "raphfrk" <raph...@netscape.netwrote:
>
I vaguely remember that originally, variables needed to be defined all
at the start of the program.
No, no, no, no, and no. Originally, variables needed to be
**declared**
at the beginning of **a block**. In other words it has always been
legal
to do:

int main(int argc, char **argv)
{
int i;

... code that uses i
{
int j;
}
}

Mar 3 '07 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
19
by: Xandau | last post by:
hello all, i wotk with java every day but last time i have interested in C#. everything goes great except one thing... in java everything is a reference (except plain types) so i thought that...
8
by: Steve Klett | last post by:
This is probably a commonly asked question, but I can't find a good way to search for it, so I will ask here. I assumed that there had to be an easy way to access a page level variable from a...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
10
by: subramanian100in | last post by:
Suppose I declare a global variable int g; in two different files say a.c which has main() and b.c When I compile them to build an executable under gcc in Redhat Linux with the command ...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.