why this output

cimon's Avatar, Join Date: Feb 2007
Go4Expert Member
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)
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.