question on variable definitions

Discussion in 'C Programming' started by raphfrk, Mar 2, 2007.

  1. raphfrk

    raphfrk Guest

    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.
    raphfrk, Mar 2, 2007
    #1
    1. Advertising

  2. raphfrk

    santosh Guest

    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).
    santosh, Mar 2, 2007
    #2
    1. Advertising

  3. raphfrk

    Chris Dollin Guest

    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.
    Chris Dollin, Mar 2, 2007
    #3
  4. raphfrk

    raphfrk Guest

    On Mar 2, 11:35 am, Chris Dollin <> 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.
    raphfrk, Mar 2, 2007
    #4
  5. raphfrk

    Flash Gordon Guest

    raphfrk wrote, On 02/03/07 11:46:
    > On Mar 2, 11:35 am, Chris Dollin <> 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
    Flash Gordon, Mar 2, 2007
    #5
  6. raphfrk

    CBFalconer Guest

    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>
    CBFalconer, Mar 2, 2007
    #6
  7. raphfrk

    CBFalconer Guest

    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>
    CBFalconer, Mar 2, 2007
    #7
  8. raphfrk

    Flash Gordon Guest

    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
    Flash Gordon, Mar 2, 2007
    #8
  9. In article <>,
    CBFalconer <> 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
    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
    Dave Vandervies, Mar 2, 2007
    #9
  10. raphfrk

    boa Guest

    Dave Vandervies wrote:
    > In article <>,
    > CBFalconer <> 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
    boa, Mar 2, 2007
    #10
  11. raphfrk

    raphfrk Guest

    On Mar 2, 1:10 pm, CBFalconer <> wrote:
    > 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' ?
    raphfrk, Mar 2, 2007
    #11
  12. "raphfrk" <> writes:
    > On Mar 2, 1:10 pm, CBFalconer <> wrote:
    >> 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) <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"
    Keith Thompson, Mar 2, 2007
    #12
  13. raphfrk

    bluejack Guest

    On Mar 2, 3:12 am, "santosh" <> wrote:

    > 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
    bluejack, Mar 2, 2007
    #13
  14. raphfrk

    santosh Guest

    bluejack wrote:
    > On Mar 2, 3:12 am, "santosh" <> wrote:
    >
    > > 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.
    santosh, Mar 2, 2007
    #14
  15. On Fri, 02 Mar 2007 18:47:22 +0100, in comp.lang.c , boa
    <> wrote:

    >Dave Vandervies wrote:
    >> In article <>,
    >> CBFalconer <> 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.


    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
    Mark McIntyre, Mar 2, 2007
    #15
  16. raphfrk

    Ian Collins Guest

    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.
    Ian Collins, Mar 3, 2007
    #16
  17. raphfrk

    Nelu Guest

    Mark McIntyre wrote:
    > On Fri, 02 Mar 2007 18:47:22 +0100, in comp.lang.c , boa
    > <> wrote:
    >
    >> Dave Vandervies wrote:
    >>> In article <>,
    >>> CBFalconer <> 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.

    >
    > 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...)
    Nelu, Mar 3, 2007
    #17
  18. raphfrk

    bluejack Guest

    On Mar 2, 4:16 pm, Ian Collins <> wrote:

    > 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
    bluejack, Mar 3, 2007
    #18
  19. raphfrk

    santosh Guest

    Nelu wrote:
    > Mark McIntyre wrote:
    > > On Fri, 02 Mar 2007 18:47:22 +0100, in comp.lang.c , boa
    > > <> wrote:
    > >
    > >> Dave Vandervies wrote:
    > >>> In article <>,
    > >>> CBFalconer <> 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.

    > >
    > > 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.
    santosh, Mar 3, 2007
    #19
  20. raphfrk

    CBFalconer Guest

    boa wrote:
    > Dave Vandervies wrote:
    >> CBFalconer <> 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.


    Microsoft.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Mar 3, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrice
    Replies:
    1
    Views:
    1,632
    =?Utf-8?B?REZyYW5rbHlu?=
    May 13, 2004
  2. Roedy Green
    Replies:
    4
    Views:
    438
  3. Thomas Matthews
    Replies:
    3
    Views:
    396
    Jason
    Aug 21, 2003
  4. Charles L
    Replies:
    5
    Views:
    1,188
    Ron Natalie
    Feb 8, 2005
  5. Replies:
    1
    Views:
    151
    Florian Groß
    Oct 26, 2005
Loading...

Share This Page