printf function help

Tarun Karra plug-devel@lists.PLUG.phoenix.az.us
Sat Mar 6 15:03:02 2004


--0-1310543108-1078610346=:96639
Content-Type: text/plain; charset=us-ascii

hi ,
 
thanx for the help on printf..
sorry for delay in getting back to you..
even i agree that its a ***stupid question****, but one of my friends had to answer this in an interview and i wanted to find out the answer..
 
SO It prints out a garbage value off the stack..
I agree that its a garbage value, but i dont think its out of stack..
correct me if any of my assumptions are wrong...
unless all the empty uninitalized values (above stack pointer)contain SAME GARBAGE VALUES.
 
because when i add some local variables to the program it still prints out the same garbage value.Below 2 programs print same garbage value out..
 
#include <stdio.h>
void main()
{
printf("%d");
}
 
#include <stdio.h>
void main()
{
int x = 10;
int y=99;
printf("%d");
}
 
 
Any way even i dont want to waste time in finding out the answer. It may be as some one suggested that its printing garbage value from va_list, va_ptr etc.. or "results are unpredictable from IBM manuals.."
 
thanx a lot for the help and time
tarun
 

"Eric \"Shubes\"" <plug@shubes.net> wrote:
As IBM manuals are famous for saying, "Results are unpredictable".

Vaughn Treude wrote:
> On Monday 01 March 2004 14:18, you wrote:
> 
>>hi,
>>
>>Iam sorry i think i didnt make the question right first time
>>
>>Can you please run the program below and give me a reason for the output
>>#include 
>>int main()
>>{
>>printf("%d") ;
>>return 0;
>>}
>>
>>watchout
>>
>>
>>>printf("%d",x) ;
>>
>>there is no x my program. Its only **********printf("%d")
>>;****************** my program still compiles and prints out value 34603777
>>
>>Can any one give me a reason for this output.
>>thx
>>tarun
> 
> 
> Tarun
> 
> The reason you get this output is because when you call a function in C, the 
> underlying assembly code pushes all the parameter values on the stack, and 
> then calls the function. Within that function, the stack is where it gets 
> the parameters. The printf function sees the "%d" specifier in the string, 
> so it's expecting a valid numeric value to be there on the stack, and not 
> knowing any better, it's pulling out a garbage value and printing it. This 
> is relatively harmless with the "%d" specifier, but is nasty with the "%s" 
> (string) specifier, because the latter will pull a value off the stack and 
> treat it as a pointer, often causing a seg fault.
> 
> Vaughn
> 
>>
>>
>>
>>coberr@scottsdaleins.com wrote:
>>
>>%d prints the value of argument in signed decimal notation. For example,
>>this will print the value of x.
>>
>>#include
>>int main()
>>{
>>float x=10;
>>printf("%d",x) ;
>>return 0;
>>}
>>
>>
>>Try 'man 3 printf' for details....
>>
>>
>>
>>
>>
>>Tarun Karra
>>T
>>To: plug-devel@lists.PLUG.phoenix.az.us
>>Sent by: cc:
>>plug-devel-admin@lists.PLU
>>G.phoenix.az.us bcc:
>>Subject: printf
>>function help
>>
>>03/01/2004 11:45 AM
>>Please respond to
>>plug-devel
>>
>>
>>
>>
>>
>>
>>hi ,,
>>
>>what does this program print and why
>>I know printf can take variable arguments..
>>how does compiler treat %d in this program..
>>#include
>>int main()
>>{
>>printf("%d") ;
>>return 0;
>>}
>>
>>thx
>>tarun
>>
>>
>>Do you Yahoo!?
>>Get better spam protection with Yahoo! Mail
>>
>>
>>
>>
>>
>>
>>
>>
>>_______________________________________________
>>PLUG-devel mailing list - PLUG-devel@lists.PLUG.phoenix.az.us
>>http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel
>>
>>---------------------------------
>>Do you Yahoo!?
>>Get better spam protection with Yahoo! Mail
> 
> 
> ----------------------------------------
> Content-Type: text/html; charset="us-ascii"; name="Attachment: 1"
> Content-Transfer-Encoding: 7bit
> Content-Description: 
> ----------------------------------------
> _______________________________________________
> PLUG-devel mailing list - PLUG-devel@lists.PLUG.phoenix.az.us
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel
> 

-- 
-Eric 'shubes'
"There is no such thing as the People;
it is a collectivist myth.
There are only individual citizens
with individual wills
and individual purposes."
-William E. Simon (1927-2000),
Secretary of the Treasury (1974-1977)
"A Time For Truth" (1978), pg. 237

_______________________________________________
PLUG-devel mailing list - PLUG-devel@lists.PLUG.phoenix.az.us
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel

---------------------------------
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster.
--0-1310543108-1078610346=:96639
Content-Type: text/html; charset=us-ascii

<DIV>hi ,</DIV>
<DIV>&nbsp;</DIV>
<DIV>thanx for the help on printf..</DIV>
<DIV>sorry for delay in getting back to you..</DIV>
<DIV>even i agree that its a ***stupid question****, but one of my friends had to answer this in an interview and i wanted to find out the answer..</DIV>
<DIV>&nbsp;</DIV>
<DIV>SO It prints out a garbage value off the stack..</DIV>
<DIV>I agree that its a garbage value, but i dont think its out of stack..</DIV>
<DIV>correct me if any of my assumptions are wrong...</DIV>
<DIV>unless all the empty uninitalized values (above stack pointer)contain SAME GARBAGE VALUES.</DIV>
<DIV>&nbsp;</DIV>
<DIV>because when i add some local variables to the program it still prints out the same garbage value.Below 2 programs print same garbage value out..</DIV>
<DIV>&nbsp;</DIV>
<DIV>#include &lt;stdio.h&gt;</DIV>
<DIV>void main()<BR>{<BR>printf("%d");<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>#include &lt;stdio.h&gt;</DIV>
<DIV>void main()<BR>{<BR>int x = 10;<BR>int y=99;<BR>printf("%d");<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>Any way even i dont want to waste time in finding out the answer. It may be as some one suggested that its printing garbage value from va_list, va_ptr etc.. or "results are unpredictable from IBM manuals.."</DIV>
<DIV>&nbsp;</DIV>
<DIV>thanx a lot for the help and time</DIV>
<DIV>tarun</DIV>
<DIV>&nbsp;</DIV>
<DIV><BR><B><I>"Eric \"Shubes\"" &lt;plug@shubes.net&gt;</I></B> wrote:</DIV>
<BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #1010ff 2px solid">As IBM manuals are famous for saying, "Results are unpredictable".<BR><BR>Vaughn Treude wrote:<BR>&gt; On Monday 01 March 2004 14:18, you wrote:<BR>&gt; <BR>&gt;&gt;hi,<BR>&gt;&gt;<BR>&gt;&gt;Iam sorry i think i didnt make the question right first time<BR>&gt;&gt;<BR>&gt;&gt;Can you please run the program below and give me a reason for the output<BR>&gt;&gt;#include <STDIO.H><BR>&gt;&gt;int main()<BR>&gt;&gt;{<BR>&gt;&gt;printf("%d") ;<BR>&gt;&gt;return 0;<BR>&gt;&gt;}<BR>&gt;&gt;<BR>&gt;&gt;watchout<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;&gt;printf("%d",x) ;<BR>&gt;&gt;<BR>&gt;&gt;there is no x my program. Its only **********printf("%d")<BR>&gt;&gt;;****************** my program still compiles and prints out value 34603777<BR>&gt;&gt;<BR>&gt;&gt;Can any one give me a reason for this output.<BR>&gt;&gt;thx<BR>&gt;&gt;tarun<BR>&gt; <BR>&gt; <BR>&gt; Tarun<BR>&gt; <BR>&gt; The reason you
 get this output is because when you call a function in C, the <BR>&gt; underlying assembly code pushes all the parameter values on the stack, and <BR>&gt; then calls the function. Within that function, the stack is where it gets <BR>&gt; the parameters. The printf function sees the "%d" specifier in the string, <BR>&gt; so it's expecting a valid numeric value to be there on the stack, and not <BR>&gt; knowing any better, it's pulling out a garbage value and printing it. This <BR>&gt; is relatively harmless with the "%d" specifier, but is nasty with the "%s" <BR>&gt; (string) specifier, because the latter will pull a value off the stack and <BR>&gt; treat it as a pointer, often causing a seg fault.<BR>&gt; <BR>&gt; Vaughn<BR>&gt; <BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;coberr@scottsdaleins.com wrote:<BR>&gt;&gt;<BR>&gt;&gt;%d prints the value of argument in signed decimal notation. For example,<BR>&gt;&gt;this will print the value of
 x.<BR>&gt;&gt;<BR>&gt;&gt;#include<BR>&gt;&gt;int main()<BR>&gt;&gt;{<BR>&gt;&gt;float x=10;<BR>&gt;&gt;printf("%d",x) ;<BR>&gt;&gt;return 0;<BR>&gt;&gt;}<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;Try 'man 3 printf' for details....<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;Tarun Karra<BR>&gt;&gt;T<BR>&gt;&gt;To: plug-devel@lists.PLUG.phoenix.az.us<BR>&gt;&gt;Sent by: cc:<BR>&gt;&gt;plug-devel-admin@lists.PLU<BR>&gt;&gt;G.phoenix.az.us bcc:<BR>&gt;&gt;Subject: printf<BR>&gt;&gt;function help<BR>&gt;&gt;<BR>&gt;&gt;03/01/2004 11:45 AM<BR>&gt;&gt;Please respond to<BR>&gt;&gt;plug-devel<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;hi ,,<BR>&gt;&gt;<BR>&gt;&gt;what does this program print and why<BR>&gt;&gt;I know printf can take variable arguments..<BR>&gt;&gt;how does compiler treat %d in this program..<BR>&gt;&gt;#include<BR>&gt;&gt;int main()<BR>&gt;&gt;{<BR>&gt;&gt;printf("%d") ;<BR>&gt;&gt;return
 0;<BR>&gt;&gt;}<BR>&gt;&gt;<BR>&gt;&gt;thx<BR>&gt;&gt;tarun<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;Do you Yahoo!?<BR>&gt;&gt;Get better spam protection with Yahoo! Mail<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;_______________________________________________<BR>&gt;&gt;PLUG-devel mailing list - PLUG-devel@lists.PLUG.phoenix.az.us<BR>&gt;&gt;http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel<BR>&gt;&gt;<BR>&gt;&gt;---------------------------------<BR>&gt;&gt;Do you Yahoo!?<BR>&gt;&gt;Get better spam protection with Yahoo! Mail<BR>&gt; <BR>&gt; <BR>&gt; ----------------------------------------<BR>&gt; Content-Type: text/html; charset="us-ascii"; name="Attachment: 1"<BR>&gt; Content-Transfer-Encoding: 7bit<BR>&gt; Content-Description: <BR>&gt; ----------------------------------------<BR>&gt; _______________________________________________<BR>&gt; PLUG-devel mailing list - PLUG-devel@lists.PLUG.phoenix.az.us<BR>&gt;
 http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel<BR>&gt; <BR><BR>-- <BR>-Eric 'shubes'<BR>"There is no such thing as the People;<BR>it is a collectivist myth.<BR>There are only individual citizens<BR>with individual wills<BR>and individual purposes."<BR>-William E. Simon (1927-2000),<BR>Secretary of the Treasury (1974-1977)<BR>"A Time For Truth" (1978), pg. 237<BR><BR>_______________________________________________<BR>PLUG-devel mailing list - PLUG-devel@lists.PLUG.phoenix.az.us<BR>http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel</BLOCKQUOTE><p><hr SIZE=1>
Do you Yahoo!?<br>
Yahoo! Search - <a href="http://search.yahoo.com/?fr=ad-mailsig-home">Find what you’re looking for faster.</a>
--0-1310543108-1078610346=:96639--