Thread: problems with indirect width specification

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    problems with indirect width specification

    Hi,
    I have some doubts related to indirect width specification.

    Here is the sample code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
      float f=20, width_f=3;
      clrscr();
      printf("%*.2f",width_f,f);
      getch();
    }
    The output of the above code is 0.00; while if I change the data type of width_f from float to int, then the output comes out to be 20.00. I know that the default data type for width specification needs to be 'int'. But I am still not clear about the output when the data type is of float type.

    Can anyone help me out with this ?? Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Width specifications are always integer, regardless of the type you're trying to print.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by udit1992 View Post
    I know that the default data type for width specification needs to be 'int'. But I am still not clear about the output when the data type is of float type.
    Based on your result and the use of conio.h I assume you are on a intel/windows machine where int is probably 4 bytes, float too, double is 8 bytes and numbers are stored little-endian. Additionally floating point numbers are represented in IEEE754 format. (correct me if I'm wrong).

    First thing you have to know is that if you call a variadic function like printf (in other words a function which accepts a variable number of arguments), all float values will be promoted to double values.

    Now I think the following is happening:
    Your first value (width_f) is promoted to a double thus 3 is stored as 00 00 00 00 00 00 08 40 in memory. The second value (f = 20) is stored as 00 00 00 00 00 00 34 40. (You can use this online conversion tool)
    The whole memory block looks like:
    00 00 00 00 00 00 08 40 00 00 00 00 00 00 34 40

    printf expects an int for the width format specifier which means it just reads the first 4 bytes which in your case are 00 00 00 00. Then it reads the next 8 bytes (%f means print a double) which are 00 00 08 40 00 00 00 00. Putting this hex number into the conversion tool (don't forget that you are on a little endian machine!) gives 5.308E-315. Printing this very small value with just 2 digits after the decimal point is 0.00.

    HTH, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion specification of %c
    By adnilsah017 in forum C Programming
    Replies: 1
    Last Post: 06-29-2011, 06:07 AM
  2. NTFS Specification
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-18-2009, 01:36 PM
  3. about exception specification
    By George2 in forum C++ Programming
    Replies: 1
    Last Post: 01-27-2008, 04:25 AM
  4. Incomplete type specification
    By New++ in forum C++ Programming
    Replies: 14
    Last Post: 12-20-2004, 08:51 AM
  5. Indirect Addressing uses how many bits?
    By franziss in forum C Programming
    Replies: 8
    Last Post: 12-16-2004, 04:33 PM