int a=printf("%d\n",a);

Discussion in 'C Programming' started by sugaray, Oct 21, 2003.

  1. sugaray

    sugaray Guest

    does the expression int a=printf("%d\n",a); implementation dependent ?
    I supposed it would produced the result 2, but i got 4206596 (result
    may vary on your machine), i wonder if the value produced was a memory
    address and anything special with this expression ?
    sugaray, Oct 21, 2003
    #1
    1. Advertising

  2. sugaray wrote:
    > does the expression int a=printf("%d\n",a); implementation dependent ?


    No, it's undefined, since `a' hasn't been initialized at the time you
    pass its value to printf(). Don't do that.

    > I supposed it would produced the result 2


    Why?

    Jeremy.
    Jeremy Yallop, Oct 21, 2003
    #2
    1. Advertising

  3. sugaray

    Minti Guest

    Jeremy Yallop <> wrote in message news:<bn3dlp$sjdeg$-berlin.de>...
    > sugaray wrote:
    > > does the expression int a=printf("%d\n",a); implementation dependent ?

    >
    > No, it's undefined, since `a' hasn't been initialized at the time you
    > pass its value to printf(). Don't do that.


    By undefined do you mean undefined "value" or undefined "behaviour"?

    >
    > > I supposed it would produced the result 2

    >
    > Why?
    >
    > Jeremy.


    --
    Imanpreet Singh Arora
    Minti, Oct 21, 2003
    #3
  4. Minti wrote:
    > Jeremy Yallop <> wrote in message news:<bn3dlp$sjdeg$-berlin.de>...
    >> sugaray wrote:
    >> > does the expression int a=printf("%d\n",a); implementation dependent ?

    >>
    >> No, it's undefined, since `a' hasn't been initialized at the time you
    >> pass its value to printf(). Don't do that.

    >
    > By undefined do you mean undefined "value" or undefined "behaviour"?


    In this case there is no difference. The value of `a' is
    indeterminate, so the behaviour when it is used is undefined.

    Jeremy.
    Jeremy Yallop, Oct 21, 2003
    #4
  5. Micah Cowan wrote:
    > Jeremy Yallop <> writes:
    >
    >> sugaray wrote:
    >> > does the expression int a=printf("%d\n",a); implementation dependent ?

    >>
    >> No, it's undefined, since `a' hasn't been initialized at the time you
    >> pass its value to printf(). Don't do that.

    >
    > How do you know that? He gave us an isolated expression: there is
    > no way to know whether or not it was initialized.


    Read it again. It's a declaration, not an expression (despite the
    words "the expression").

    Jeremy.
    Jeremy Yallop, Oct 22, 2003
    #5
  6. In article <>,
    Micah Cowan <> wrote:
    >Jeremy Yallop <> writes:
    >
    >> sugaray wrote:
    >> > does the expression int a=printf("%d\n",a); implementation dependent ?

    >>
    >> No, it's undefined, since `a' hasn't been initialized at the time you
    >> pass its value to printf(). Don't do that.

    >
    >How do you know that? He gave us an isolated expression: there is
    >no way to know whether or not it was initialized.
    >
    >Assuming it was, it is uncertain to me whether the expression
    >above meets the requirement in 6.5#2 that "the prior value shall
    >be read only to determine the value to be stored." (the exact
    >meaning and intent of that phrase is somewhat unclear to me).


    I'm not sure that requrement is even relevant here; doesn't that
    only apply between sequence points? There's a sequence point between
    evaluating a in the argument list and the invocation of printf, and
    another one immediately after printf returns before its return value is
    assigned to a, so there's no problem between the access and modification
    of a.


    dave

    --
    Dave Vandervies
    If he'd got it wrong (and it happens to us all), several people would
    have jumped on him and started stuffing socks down his throat.
    --Richard Heathfield in comp.lang.c
    Dave Vandervies, Oct 22, 2003
    #6
  7. sugaray

    Micah Cowan Guest

    Jeremy Yallop <> writes:

    > sugaray wrote:
    > > does the expression int a=printf("%d\n",a); implementation dependent ?

    >
    > No, it's undefined, since `a' hasn't been initialized at the time you
    > pass its value to printf(). Don't do that.


    How do you know that? He gave us an isolated expression: there is
    no way to know whether or not it was initialized.

    Assuming it was, it is uncertain to me whether the expression
    above meets the requirement in 6.5#2 that "the prior value shall
    be read only to determine the value to be stored." (the exact
    meaning and intent of that phrase is somewhat unclear to me).

    --
    Micah J. Cowan
    Micah Cowan, Oct 22, 2003
    #7
  8. On Wed, 22 Oct 2003, Dave Vandervies wrote:
    >
    > Micah Cowan <> wrote:
    > >Jeremy Yallop <> writes:
    > >
    > >> sugaray wrote:
    > >> > int a=printf("%d\n",a);
    > >>
    > >> No, it's undefined, since `a' hasn't been initialized at the time you
    > >> pass its value to printf(). Don't do that.

    > >

    [Assuming that pesky 'int' weren't there...]
    > >it is uncertain to me whether the expression
    > >above meets the requirement in 6.5#2 that "the prior value shall
    > >be read only to determine the value to be stored." (the exact
    > >meaning and intent of that phrase is somewhat unclear to me).

    >
    > I'm not sure that requrement is even relevant here; doesn't that
    > only apply between sequence points?


    Yep. The expression-statement

    a = printf("%d\n", a);

    is equivalent in the essentials to

    a = a+1;

    You're reading the value, computing something, and storing the
    value. No undefined behavior there at all.

    The undefined behavior appears here:

    int a = printf("%d\n", a);

    which is essentially equivalent to

    int a;
    int rc;
    rc = printf("%d\n", a); /* UB since a's value is indeterminate */
    a = rc;

    -Arthur
    Arthur J. O'Dwyer, Oct 23, 2003
    #8
  9. sugaray

    Minti Guest

    Jeremy Yallop <> wrote in message news:<bn3uf0$s6007$-berlin.de>...
    > Minti wrote:
    > > Jeremy Yallop <> wrote in message news:<bn3dlp$sjdeg$-berlin.de>...
    > >> sugaray wrote:
    > >> > does the expression int a=printf("%d\n",a); implementation dependent ?
    > >>
    > >> No, it's undefined, since `a' hasn't been initialized at the time you
    > >> pass its value to printf(). Don't do that.

    > >
    > > By undefined do you mean undefined "value" or undefined "behaviour"?

    >
    > In this case there is no difference. The value of `a' is
    > indeterminate, so the behaviour when it is used is undefined.
    >


    But given the 'declaration'

    int a = printf("%d", a);

    Does the standard gaurantee that "a" would have been 'defined' before
    its value is used by the printf.

    --
    Imanpreet Singh Arora
    Minti, Oct 23, 2003
    #9
  10. Minti wrote:
    > Jeremy Yallop <> wrote in message news:<bn3uf0$s6007$-berlin.de>...
    >> Minti wrote:
    >> > Jeremy Yallop <> wrote in message news:<bn3dlp$sjdeg$-berlin.de>...
    >> >> sugaray wrote:
    >> >> > does the expression int a=printf("%d\n",a); implementation dependent ?
    >> >>
    >> >> No, it's undefined, since `a' hasn't been initialized at the time you
    >> >> pass its value to printf(). Don't do that.
    >> >
    >> > By undefined do you mean undefined "value" or undefined "behaviour"?

    >>
    >> In this case there is no difference. The value of `a' is
    >> indeterminate, so the behaviour when it is used is undefined.
    >>

    > But given the 'declaration'
    >
    > int a = printf("%d", a);
    >
    > Does the standard gaurantee that "a" would have been 'defined' before
    > its value is used by the printf.


    Yes. The scope of `a' begins at the end of its declarator.

    Jeremy.
    Jeremy Yallop, Oct 23, 2003
    #10
  11. sugaray

    Dan Pop Guest

    In <> Jeremy Yallop <> writes:

    >Minti wrote:
    >> Jeremy Yallop <> wrote in message news:<bn3uf0$s6007$-berlin.de>...
    >>> Minti wrote:
    >>> > Jeremy Yallop <> wrote in message news:<bn3dlp$sjdeg$-berlin.de>...
    >>> >> sugaray wrote:
    >>> >> > does the expression int a=printf("%d\n",a); implementation dependent ?
    >>> >>
    >>> >> No, it's undefined, since `a' hasn't been initialized at the time you
    >>> >> pass its value to printf(). Don't do that.
    >>> >
    >>> > By undefined do you mean undefined "value" or undefined "behaviour"?
    >>>
    >>> In this case there is no difference. The value of `a' is
    >>> indeterminate, so the behaviour when it is used is undefined.
    >>>

    >> But given the 'declaration'
    >>
    >> int a = printf("%d", a);
    >>
    >> Does the standard gaurantee that "a" would have been 'defined' before
    >> its value is used by the printf.

    >
    >Yes. The scope of `a' begins at the end of its declarator.


    So, the answer is actually "no": if `a' was already defined prior to our
    definition, the `a' in the printf call refers to the "new" `a', currently
    uninitialised and not to the "old" `a'.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 23, 2003
    #11
  12. Dan Pop wrote:
    > In <> Jeremy Yallop <> writes:
    >
    >>Minti wrote:
    >>> Jeremy Yallop <> wrote in message news:<bn3uf0$s6007$-berlin.de>...
    >>>> Minti wrote:
    >>>> > Jeremy Yallop <> wrote in message news:<bn3dlp$sjdeg$-berlin.de>...
    >>>> >> sugaray wrote:
    >>>> >> > does the expression int a=printf("%d\n",a); implementation dependent ?
    >>>> >>
    >>>> >> No, it's undefined, since `a' hasn't been initialized at the time you
    >>>> >> pass its value to printf(). Don't do that.
    >>>> >
    >>>> > By undefined do you mean undefined "value" or undefined "behaviour"?
    >>>>
    >>>> In this case there is no difference. The value of `a' is
    >>>> indeterminate, so the behaviour when it is used is undefined.
    >>>>
    >>> But given the 'declaration'
    >>>
    >>> int a = printf("%d", a);
    >>>
    >>> Does the standard gaurantee that "a" would have been 'defined' before
    >>> its value is used by the printf.

    >>
    >>Yes. The scope of `a' begins at the end of its declarator.

    >
    > So, the answer is actually "no":


    Well, that depends on what the question is :). I understood it as
    "Does the standard guarantee that "a" is in scope at the time its
    value is used by printf?". I think you read it as "Does the standard
    guarantee that if "a" is already defined its value is used by printf?"
    (where "its" refers to "the already-defined a's"). We don't disagree
    on the behaviour, anyway:

    > if `a' was already defined prior to our definition, the `a' in the
    > printf call refers to the "new" `a', currently uninitialised and not
    > to the "old" `a'.


    Right.

    Jeremy.
    Jeremy Yallop, Oct 23, 2003
    #12
  13. sugaray

    Micah Cowan Guest

    Jeremy Yallop <> writes:

    > Micah Cowan wrote:
    > > Jeremy Yallop <> writes:
    > >
    > >> sugaray wrote:
    > >> > does the expression int a=printf("%d\n",a); implementation dependent ?
    > >>
    > >> No, it's undefined, since `a' hasn't been initialized at the time you
    > >> pass its value to printf(). Don't do that.

    > >
    > > How do you know that? He gave us an isolated expression: there is
    > > no way to know whether or not it was initialized.

    >
    > Read it again. It's a declaration, not an expression (despite the
    > words "the expression").


    Doh! :)

    --
    Micah J. Cowan
    Micah Cowan, Oct 23, 2003
    #13
    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. Schnoffos
    Replies:
    2
    Views:
    1,199
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    434
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    934
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    421
    James Kanze
    Apr 17, 2007
Loading...

Share This Page