Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   why this output (http://www.go4expert.com/forums/why-this-output-t4926/)

cimon 27Jun2007 01:17

why this output
 
Code:

#include<stdio.h>

void main()
{
        int e;
        printf("enter number");
        scanf("%d",&e);
        printf("%d ,%d",e+5);

}

output

enter number 4
9 ,4

i am trying what happens and why
if i give an extra %d with nothing corresponding to it after comma ,

why i get this 4 as output else than e+5 which is 9

and if i use this printf statement

printf("%d %d %d %d %d",e+5);

i get
enter number 5
10 5 -18 285 1

as far as i know printf gives -ve number on error but i get only one -ve number
(compiler used turbo C)

DaWei 27Jun2007 02:08

Re: why this output
 
Quote:

if i give an extra %d with nothing corresponding to it after comma ,
Why would you do that? Printf doesn't promise to work that way. If you really want to know why, write a "printf" function.

This may be strange to you, but programming is very literal. It attempts (and sometimes fails) to do what you ask, but it never promises to read your mind or intentions. Between the two of you, you are expected to be the smart one.

shabbir 27Jun2007 08:02

Re: why this output
 
If you don't have any value for the corresponding %d its the garbage that is printed and luckily or unluckily your compiler has the 4 in the garbage location which you are printing in the code.


All times are GMT +5.5. The time now is 17:54.