Thread: output bit strange

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    output bit strange

    hello all,
    why the output is always zero when i try to convert it from int to float in the printf function. till i explicitly cast it to a float it keeps on outping zeros. here is the code

    Code:
    #include<stdio.h>
    
    int main()
    {
            int a=10;
            //int *p=&a;
            
            printf("%f",a);
            getchar();
    }
    and my output
    Code:
    0.000000
    can anyone tell me why this stange output is

    thank u
    -onthewaytoC

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Because you're not converting it -- you're just lying to printf and seeing some form of undefined behavior. Either tell the truth and use %d, or perform an actual conversion.
    Code:
    #include <stdio.h>
    
    int main()
    {
       int a = 10;
       printf("%d\n", a);
       printf("%f\n", (float)a);
       getchar();
       return 0;
    }
    
    /* my output
    10
    10.000000
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    FOX
    Join Date
    May 2005
    Posts
    188
    Try this instead:
    Code:
    printf("%f", (float) a);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or if you prefer:
    Code:
    printf("%f", 0.0f + a );
    Takes less keystrokes.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    thax very for your help, i have one more question to be clarified. some text books still follows conversion of type in this method (especially) converting from int to char using printf

    Code:
    #include<stdio.h>
    
    int main()
    {
        int a=65;
        printf("%c",a);
        getchar();
        return 0;
    }
    is this a proper way of converting. the question remains the same, cose it converts int to char using printf but not int to float

    -onthewaytoC

  6. #6
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Well certainly in ASCII, 65 is equal to "A", therefore the int already "is" the character. All the printf statement does is treat the int like a character instead of an integer, so it prints a character instead of a number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  2. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 08:22 PM
  3. Array output strange
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2006, 06:58 AM
  4. Strange output for class
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 11-05-2006, 04:22 PM
  5. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM