473,801 Members | 2,472 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
12 3817
In <sl************ *******@ekoi.cl .cam.ac.uk> Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> writes:
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.


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: Da*****@ifh.de
Nov 13 '05 #11
Dan Pop wrote:
In <sl************ *******@ekoi.cl .cam.ac.uk> Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> writes:
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.


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.
Nov 13 '05 #12
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> writes:
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").


Doh! :-)

--
Micah J. Cowan
mi***@cowan.nam e
Nov 13 '05 #13

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.