Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question on variable definitions

Reply
Thread Tools

question on variable definitions

 
 
raphfrk
Guest
Posts: n/a
 
      03-02-2007
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.

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-02-2007
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).

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      03-02-2007
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.

 
Reply With Quote
 
raphfrk
Guest
Posts: n/a
 
      03-02-2007
On Mar 2, 11:35 am, Chris Dollin <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-02-2007
raphfrk wrote, On 02/03/07 11:46:
> On Mar 2, 11:35 am, Chris Dollin <(E-Mail Removed)> wrote:


<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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-02-2007
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>


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-02-2007
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>


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-02-2007
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
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      03-02-2007
In article <(E-Mail Removed)>,
CBFalconer <(E-Mail Removed)> wrote:
>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 http://www.velocityreviews.com/forums/(E-Mail Removed)
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
 
Reply With Quote
 
boa
Guest
Posts: n/a
 
      03-02-2007
Dave Vandervies wrote:
> In article <(E-Mail Removed)>,
> CBFalconer <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
irb question - variable definitions when calling irb from a script problem Nuralanur@aol.com Ruby 1 10-26-2005 09:13 PM
preventing "duplicate" variable definitions Roedy Green Java 4 07-15-2005 09:03 AM
Multiple definitions of a variable in C++ not permitted Charles L C++ 5 02-08-2005 12:13 PM
Re: Variable definitions within a Case Statement Patrice ASP .Net 1 05-13-2004 06:21 PM
Effiiency of Variable definitions in loops Thomas Matthews C++ 3 08-21-2003 10:05 PM



Advertisments