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

Discussion in 'C Programming' started by s.subbarayan, Sep 10, 2004.

  1. s.subbarayan

    s.subbarayan Guest

    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
    s.subbarayan, Sep 10, 2004
    #1
    1. Advertising

  2. s.subbarayan

    Richard Bos Guest

    (s.subbarayan) wrote:

    > 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
    Richard Bos, Sep 10, 2004
    #2
    1. Advertising

  3. [snips]

    On Fri, 10 Sep 2004 07:40:06 +0000, Richard Bos wrote:

    > (s.subbarayan) wrote:
    >
    >> Whats the notion behind C standard preventing declaration in middle
    >> of statements?


    > 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.
    Kelsey Bjarnason, Sep 10, 2004
    #3
    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. Neil Zanella
    Replies:
    8
    Views:
    1,183
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    374
    Bart Nessux
    Feb 23, 2004
  3. tobiah
    Replies:
    22
    Views:
    572
    Ben Finney
    Sep 1, 2006
  4. Vince
    Replies:
    12
    Views:
    747
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    262
    John Crichton
    Jul 12, 2010
Loading...

Share This Page