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

S

sugaray

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 ?
 
J

Jeremy Yallop

sugaray said:
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.
 
M

Minti

Jeremy Yallop said:
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"?
 
J

Jeremy Yallop

Minti said:
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.
 
J

Jeremy Yallop

Micah said:
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.
 
D

Dave Vandervies

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
 
M

Micah Cowan

Jeremy Yallop said:
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).
 
A

Arthur J. O'Dwyer

Micah Cowan said:
Jeremy Yallop said:
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
 
M

Minti

Jeremy Yallop said:
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.
 
J

Jeremy Yallop

Minti said:
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.
 
D

Dan Pop

In said:
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
 
J

Jeremy Yallop

Dan said:
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
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top