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