By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
Jeremy Yallop <je****@jdyallop.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

P: n/a
Minti wrote:
Jeremy Yallop <je****@jdyallop.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

P: n/a
Jeremy Yallop <je****@jdyallop.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.name
Nov 13 '05 #5

P: n/a
Micah Cowan wrote:
Jeremy Yallop <je****@jdyallop.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

P: n/a
In article <m3************@localhost.localdomain>,
Micah Cowan <mi***@cowan.name> wrote:
Jeremy Yallop <je****@jdyallop.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

P: n/a

On Wed, 22 Oct 2003, Dave Vandervies wrote:

Micah Cowan <mi***@cowan.name> wrote:
Jeremy Yallop <je****@jdyallop.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

P: n/a
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote in message news:<bn************@ID-114079.news.uni-berlin.de>...
Minti wrote:
Jeremy Yallop <je****@jdyallop.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

P: n/a
Minti wrote:
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote in message news:<bn************@ID-114079.news.uni-berlin.de>...
Minti wrote:
> Jeremy Yallop <je****@jdyallop.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

P: n/a
In <sl*******************@ekoi.cl.cam.ac.uk> Jeremy Yallop <je****@jdyallop.freeserve.co.uk> writes:
Minti wrote:
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote in message news:<bn************@ID-114079.news.uni-berlin.de>...
Minti wrote:
> Jeremy Yallop <je****@jdyallop.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

P: n/a
Dan Pop wrote:
In <sl*******************@ekoi.cl.cam.ac.uk> Jeremy Yallop <je****@jdyallop.freeserve.co.uk> writes:
Minti wrote:
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote in message news:<bn************@ID-114079.news.uni-berlin.de>...
Minti wrote:
> Jeremy Yallop <je****@jdyallop.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

P: n/a
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> writes:
Micah Cowan wrote:
Jeremy Yallop <je****@jdyallop.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.name
Nov 13 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.