printf function help
Eric "Shubes"
plug-devel@lists.PLUG.phoenix.az.us
Tue Mar 2 09:21:02 2004
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 <stdio.h>
>>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