1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

confusion with format string

Discussion in 'C' started by shyam_oec, Jan 23, 2008.

  1. shyam_oec

    shyam_oec New Member

    Joined:
    Nov 26, 2007
    Messages:
    89
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Developer, .NET Framework
    Location:
    Jamshedpur
    main()
    {
    float x=1.0;
    int y=3;
    printf("x=%d",x);
    printf("\ny=%f",y);
    }

    output:
    x=0
    y=285737.000011(why this unexpected output?) :confused:

    though x is floating type variable,when it is displayed in int farmat why it's output is x=0
    on the other hand value of y is something annoying
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    You used the wrong format for the printf commands.
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:14: warning: int format, double arg (arg 2)
    foo.c:15: warning: double format, different type arg (arg 2)
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    %d and %f are for integer and float respectively.
     
  4. shyam_oec

    shyam_oec New Member

    Joined:
    Nov 26, 2007
    Messages:
    89
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Developer, .NET Framework
    Location:
    Jamshedpur
    it's fine that %d and %f are for integer and float,but can u tell why we get such an output....
     
  5. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    > but can u tell why we get such an output.
    Because your code is broken.

    Broken code produces whatever it feels like, regardless of what you might expect / like / desire / hope for.

    Use a compiler which can check your printf / scanf format strings (like I posted), and don't run any code which has warnings in it.
     
  6. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    When you will go definition of printf. A sample of it..
    Code:
    int printf( . . . )
    {
         //Some code
       ...........
       ...............
      //va_arg : taking conversion symbol(%d, %f etc) and associated with values
     switch( TYPE)
     { 
    
      //Case INT_TYPE : Some code for Integer
      // Case FLOAT_TYPE : some code for Float 
      // Case CHAR_TYPE  : some code for char
        ......
       .....
      }
    return ;
    }
    
    when conversion sybol(%d) and value is double then it's value will be junk. because printf function does not check for type casting. Just %d and its associated values. If it's matching then it will go correct switch case and correct value. Else wrong switch case will execute and value will be junk.
     

Share This Page