Will wonders never cease? Kenny M has a real C question...

Discussion in 'C Programming' started by Kenny McCormack, Sep 5, 2010.

  1. Working with some old code - that has compiled (and run) successfully on
    dozens of Unix systems over the years - I came across the following line
    of code.

    At file scope level:

    FILE *outfile = stdout;

    (After including stdio, of course). Today, gcc running on a Unix system
    says that the initializer is not a constant. And, digging through
    /usr/include/stdio.h shows that that does seem to be the case.

    So, the question is: Does the standard have anything to say about this?
    Now, obviously, whenever anyone ever asks that question, the answer is
    "No", since the standard guarantees virtually nothing useful. But,
    still I'm curious to see what flames, er, I mean, discussion I can
    unleash on this subject.

    Why should it be? Why shouldn't it be?

    --
    Is God willing to prevent evil, but not able? Then he is not omnipotent.
    Is he able, but not willing? Then he is malevolent.
    Is he both able and willing? Then whence cometh evil?
    Is he neither able nor willing? Then why call him God?
    ~ Epicurus
     
    Kenny McCormack, Sep 5, 2010
    #1
    1. Advertising

  2. On 05/09/2010 04:26, Kenny McCormack wrote:
    > Working with some old code - that has compiled (and run) successfully on
    > dozens of Unix systems over the years - I came across the following line
    > of code.
    >
    > At file scope level:
    >
    > FILE *outfile = stdout;
    >
    > (After including stdio, of course). Today, gcc running on a Unix system
    > says that the initializer is not a constant. And, digging through
    > /usr/include/stdio.h shows that that does seem to be the case.
    >
    > So, the question is: Does the standard have anything to say about this?


    Yes. The above code is not required to compile because 7.19.1#3
    defines stdout as an expression, when 6.7.8#4 says that "All the
    expressions in an initializer for an object that has static storage
    duration shall be *constant* expressions or string literals."

    Francois Grieu
     
    Francois Grieu, Sep 5, 2010
    #2
    1. Advertising

  3. Kenny McCormack

    Shao Miller Guest

    Kenny McCormack wrote:
    > Working with some old code - that has compiled (and run) successfully on
    > dozens of Unix systems over the years - I came across the following line
    > of code.
    >
    > At file scope level:
    >
    > FILE *outfile = stdout;
    >
    > (After including stdio, of course). Today, gcc running on a Unix system
    > says that the initializer is not a constant. And, digging through
    > /usr/include/stdio.h shows that that does seem to be the case.
    >
    > So, the question is: Does the standard have anything to say about this?
    > Now, obviously, whenever anyone ever asks that question, the answer is
    > "No", since the standard guarantees virtually nothing useful. But,
    > still I'm curious to see what flames, er, I mean, discussion I can
    > unleash on this subject.
    >
    > Why should it be? Why shouldn't it be?
    >


    You might be interested in "address constant" in 'n1256.pdf', section
    6.6, point 9.

    I think that a way to perceive the constraint is that static object
    initialization occurs at program startup (I'm pretty sure that in most
    cases, the initialization values are simply loaded from the program
    image or during such loading, just the same as the object code that
    constitutes the program). I think that the constraint for requiring
    constant values helps to alleviate data dependencies such as "we need to
    know this before we can know that". So basically constants at
    translation-time are a fairly simple constraint to make to facilitate this.

    Does that make any sense to you?
     
    Shao Miller, Sep 8, 2010
    #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. Bredal Jensen
    Replies:
    3
    Views:
    399
    Christopher Reed
    Dec 25, 2005
  2. Chris Reedy
    Replies:
    16
    Views:
    550
    Donald 'Paddy' McCarthy
    Aug 25, 2003
  3. Kenny Tilton

    Lisp with Kenny! <g>

    Kenny Tilton, Sep 3, 2003, in forum: Python
    Replies:
    0
    Views:
    285
    Kenny Tilton
    Sep 3, 2003
  4. John Salerno
    Replies:
    3
    Views:
    278
    Steven D'Aprano
    Mar 3, 2006
  5. kenny
    Replies:
    1
    Views:
    195
    Thomas 'PointedEars' Lahn
    Jan 3, 2011
Loading...

Share This Page