Difference between %f and %d

This is a discussion on Difference between %f and %d within the C Programming forums, part of the General Programming Boards category; what is the difference between those two if you, say use the printf and print out a number like 103.21 ...

  1. #1
    Johnny1
    Guest

    Difference between %f and %d

    what is the difference between those two if you, say use the printf and print out a number like 103.21 or something?

    d stands for decimal
    f stands for float

    % is called the "format specifier" I think.
    thanks anyone, I'll check my book too, John

  2. #2
    1234
    Guest
    can't find it in my book

  3. #3
    Johnny1
    Guest
    oh, it says in my book that %d, has the "d" meaning that it will read in only integers but will display them in decimal format, so 3 will show as 3.0. So I guess float %f, the f will take in or dislay real decimal numbers. Ok, I'll read more

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    %d prints an integer in base 10, %f prints a floating point value. So if I used

    printf( "%d, %f\n", 5, 5.5 );

    The output would be 5, 5.500000. If you give printf 5.5 but use %d I think anything could happen, the same goes with giving it an integer but using %f.

    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  5. #5
    Johnny1
    Guest
    thank you. Ok, now I read that the format specifiers for scanf() are similar to those used for printf() for the corresponding data types except that %lf is used to read a "double"

    But to print a double you can still use "%f" and you don't have to used "%lf"? That seems to be what it is saying.

    and thanks again, John

  6. #6
    Johnny1
    Guest
    now, check this code out, when I put in the "double" with about 6 decimal places, like 34.23423423, the return I get when I run it is 0.000000. Why?

    See, I followed the rule where you use "%ld" to scan the double, and not %f like you do for float. but to printf() a double you use a %f for both float and doubles. But I get 0.000000.
    I don't know why.

  7. #7
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Yea, printf will automatically convert a float to a double, this is normal for C function arguments, floating point values are converted to double and short integer values are converted to int. The reason scanf doesn't do it is because you pass a pointer to scanf, not a value. If scanf used %f for both double and float then there will be problems with pointer access. So scanf uses %f for float and %lf for double while print just uses %f because of the conversion to double.

    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  8. #8
    Johnny1
    Guest
    oh, here is the code to my last post, sorry

    Code:
    #include <stdio.h>
    
    int main()
    {
      int num;
      double f;
      
      printf( "Enter an integer: " );
      scanf( "%d", &num );
      
      printf( "Enter a floating point number: " );
      scanf( "%ld", &f );
      
      printf( "%d ", num );
      printf( "%f ", f );
      
      return 0;
      }

  9. #9
    Johnny1
    Guest
    I'm not that far, haven't studied pointers yet. hopefully I'll see what you mean, thanks. I still don't know why I got 0.000000

  10. #10
    Johnny1
    Guest
    and I hope my most previous code isn't confusing, it should say "Enter a double number", but still that doesn't affect the data types I used, it's just words.

  11. #11
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Look real close, you're using %ld, not %lf. %ld is a long integer, not a double, so no conversion is made and the value that was originally in f is printed. On my computer it prints random garbage because local variables aren't usually initialized to 0. Your compiler may do this, that's why you get 0.000000.
    Code:
    scanf( "%ld", &f );
    Don't worry about pointers with scanf, just remember to use the address of operator & on variables you are scanning. How scanf works really doesn't matter as long as you understand how to use it right.

    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  12. #12
    Johnny1
    Guest

    Smile

    yes! It works, thanks bebop, so many of my errors are not conceptual but technical. that makes me feel kinda smarter

  13. #13
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Dr. Bebop
    this is normal for C function arguments, floating point values are converted to double and short integer values are converted to int.
    not quite: this conversion (and btw chars are also converted to ints) only occurs in functions that take variable numbers of variables and in functions declared with the old style declaration, such as:

    Code:
    int old_style_function (a, b, c)
    int a;
    double b;
    int c;
    {
      /*code*/
      return 1;
    }
    hello, internet!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21