Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   PRINTF() (http://www.velocityreviews.com/forums/t595729-printf.html)

jt 03-03-2008 02:12 PM

PRINTF()
 
the statement

printf("%d %d");

will print the 1st 2nd initialized variables(if present)
how is tht possible

Martin Ambuhl 03-03-2008 02:41 PM

Re: PRINTF()
 
jt wrote:
> the statement
>
> printf("%d %d");
>
> will print the 1st 2nd initialized variables(if present)


What makes you think so? Just because your implementation doesn't barf
at broken code doesn't mean whatever random thing it produces is right.

> how is tht possible


Neither that not 'tht' are in any way guaranteed. Learn to write legal
C; it will help you much more than your playing with broken code.


Vladimir Oka 03-03-2008 02:42 PM

Re: PRINTF()
 
jt wrote:
> the statement
>
> printf("%d %d");
>
> will print the 1st 2nd initialized variables(if present)
> how is tht possible


No, it won't.

It will print whatever rubbish happens to be found by printf on
the stack where it expects two ints. Your implementation may
just happen to keep two previously initialised variables there.
Mine has proper rubbish.

Some would also argue it won't necessarily print a thing, as you
don't show you printed a terminating newline anywhere. ;)

It's undefined what happens really, as you should have supplied
two ints.

--
My e-mail address is real, and I read it.

CBFalconer 03-03-2008 11:02 PM

Re: PRINTF()
 
Vladimir Oka wrote:
> jt wrote:
>
>> the statement
>>
>> printf("%d %d");
>>
>> will print the 1st 2nd initialized variables(if present)
>> how is tht possible

>
> No, it won't.
>
> It will print whatever rubbish happens to be found by printf on
> the stack where it expects two ints. Your implementation may
> just happen to keep two previously initialised variables there.
> Mine has proper rubbish.


What stack? It has undefined behaviour.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com


Vladimir Oka 03-03-2008 11:37 PM

Re: PRINTF()
 
CBFalconer wrote:
> Vladimir Oka wrote:
>> jt wrote:
>>
>>> the statement
>>>
>>> printf("%d %d");
>>>
>>> will print the 1st 2nd initialized variables(if present)
>>> how is tht possible

>> No, it won't.
>>
>> It will print whatever rubbish happens to be found by printf on
>> the stack where it expects two ints. Your implementation may
>> just happen to keep two previously initialised variables there.
>> Mine has proper rubbish.

>
> What stack? It has undefined behaviour.


Was that a necessary comment?

I even said it was undefined -- in the bit that you snipped.

And the reason OP was getting the behaviour he was had to do
with his particular implementation so that was the reply he got,
seeing as this was what he was talking about. His question was
"how is that possible". I gave one possible theory.

I know you've all been busy with Jacob and stack recently but I
wasn't expecting it to spill over all of the group...

--
My e-mail address is real, and I read it.

Richard 03-04-2008 01:17 AM

Re: PRINTF()
 
CBFalconer <cbfalconer@yahoo.com> writes:

> Vladimir Oka wrote:
>> jt wrote:
>>
>>> the statement
>>>
>>> printf("%d %d");
>>>
>>> will print the 1st 2nd initialized variables(if present)
>>> how is tht possible

>>
>> No, it won't.
>>
>> It will print whatever rubbish happens to be found by printf on
>> the stack where it expects two ints. Your implementation may
>> just happen to keep two previously initialised variables there.
>> Mine has proper rubbish.

>
> What stack? It has undefined behaviour.


Probably the stack that his environment uses I would dare say. The one
the debugger displays. Duh. Whether that stack is used in this case is
another issue, but it was fairly clear what stack he meant.

Peter Nilsson 03-04-2008 02:35 AM

Re: PRINTF()
 
Vladimir Oka <vladimir....@btopenworld.com> wrote:
> CBFalconer wrote:
> > Vladimir Oka wrote:
> > > jt wrote:
> > > > *the statement
> > > > printf("%d %d");
> > > > will print the 1st 2nd initialized variables(if present)
> > > > how is tht possible
> > >
> > > No, it won't.
> > >
> > > It will print whatever rubbish happens to be found
> > > by printf on the stack where it expects two ints.


Actually, it expects two void pointers.

> > > Your
> > > implementation may just happen to keep two previously
> > > initialised variables there. Mine has proper rubbish.

> >
> > What stack? *It has undefined behaviour.

>
> Was that a necessary comment?


Was yours?

Martin gave the answer the OP needed.

--
Peter

jaysome 03-04-2008 06:17 AM

Re: PRINTF()
 
On Mon, 03 Mar 2008 18:35:07 -0800, Peter Nilsson wrote:

> Vladimir Oka <vladimir....@btopenworld.com> wrote:
>> CBFalconer wrote:
>> > Vladimir Oka wrote:
>> > > jt wrote:
>> > > > *the statement
>> > > > printf("%d %d");
>> > > > will print the 1st 2nd initialized variables(if present) how is
>> > > > tht possible
>> > >
>> > > No, it won't.
>> > >
>> > > It will print whatever rubbish happens to be found by printf on the
>> > > stack where it expects two ints.

>
> Actually, it expects two void pointers.


I thought the "%d" conversion specifier expects type int. Isn't the
conversion specifier for a void pointer "%p"?

The way I read your comment, you are you saying that something like this
is acceptable?

printf("%d %d", (void*)98, (void*)99);

Isn't that undefined behavior?

--
jay

Micah Cowan 03-04-2008 06:53 AM

Re: PRINTF()
 
jaysome <jaysome@spamcop.net> writes:

> On Mon, 03 Mar 2008 18:35:07 -0800, Peter Nilsson wrote:
>
>> Vladimir Oka <vladimir....@btopenworld.com> wrote:
>>> CBFalconer wrote:
>>> > Vladimir Oka wrote:
>>> > > jt wrote:
>>> > > > *the statement
>>> > > > printf("%d %d");
>>> > > > will print the 1st 2nd initialized variables(if present) how is
>>> > > > tht possible


<snip>

>>> > > It will print whatever rubbish happens to be found by printf on the
>>> > > stack where it expects two ints.

>>
>> Actually, it expects two void pointers.

>
> I thought the "%d" conversion specifier expects type int. Isn't the
> conversion specifier for a void pointer "%p"?


Yes. I believe it's fair to say that Mr Nilsson misread. :)

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/

Vladimir Oka 03-04-2008 09:13 AM

Re: PRINTF()
 
Peter Nilsson wrote:
> Vladimir Oka <vladimir....@btopenworld.com> wrote:
>> CBFalconer wrote:
>>> Vladimir Oka wrote:
>>>> Your
>>>> implementation may just happen to keep two previously
>>>> initialised variables there. Mine has proper rubbish.
>>> What stack? It has undefined behaviour.

>> Was that a necessary comment?

>
> Was yours?
>
> Martin gave the answer the OP needed.


I respectfully disagree.

The OP asked along the lines of "how is it possible that I get
this silly behaviour". I disagree it enhances OP's understanding
of software in general if he gets the answer "it's undefined
behaviour, don't do it (and don't think about it further)." My
reply offered one possible scenario that could lead to undefined
behaviour having the outcome seen by the OP. I believe that is
the reply OP needed.

Not to mention I also pointed out it was undefined, but that was
snipped from CBFalconer's response, making my reply seem incomplete.

--
My e-mail address is real, and I read it.


All times are GMT. The time now is 04:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.