why this output

Discussion in 'C' started by cimon, Jun 26, 2007.

  1. cimon

    cimon New Member

    Joined:
    Feb 9, 2007
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    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)
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice