473,789 Members | 2,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 ?
Nov 13 '05 #1
12 3812
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.
Nov 13 '05 #2
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> wrote in message news:<bn******* *****@ID-114079.news.uni-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
Nov 13 '05 #3
Minti wrote:
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> wrote in message news:<bn******* *****@ID-114079.news.uni-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.
Nov 13 '05 #4
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> 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
mi***@cowan.nam e
Nov 13 '05 #5
Micah Cowan wrote:
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> 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.
Nov 13 '05 #6
In article <m3************ @localhost.loca ldomain>,
Micah Cowan <mi***@cowan.na me> wrote:
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> 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 dj******@csclub .uwaterloo.ca
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
Nov 13 '05 #7

On Wed, 22 Oct 2003, Dave Vandervies wrote:

Micah Cowan <mi***@cowan.na me> wrote:
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> 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

Nov 13 '05 #8
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> wrote in message news:<bn******* *****@ID-114079.news.uni-berlin.de>...
Minti wrote:
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> wrote in message news:<bn******* *****@ID-114079.news.uni-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
Nov 13 '05 #9
Minti wrote:
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> wrote in message news:<bn******* *****@ID-114079.news.uni-berlin.de>...
Minti wrote:
> Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> wrote in message news:<bn******* *****@ID-114079.news.uni-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.
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.