Thread: Problem about float

  1. #1
    Registered User user.beshi's Avatar
    Join Date
    Oct 2014
    Posts
    9

    Problem about float

    I just checking but confused with float. in that code same size int,and same type double are working but float showing nothing in printf..why?? i'm using GCC compiler int 32bit win7 os

    Code:
    #‎include <stdio.h>
    int main()
    {
    
      char arr[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
      printf("Size of char=%c\n", ((char *) (&arr[0]))[1]);
    
      printf("Size of short=%c\n", ((short *) (&arr[0]))[1]);
    
      printf("Size of int=%c\n", ((int *) (&arr[0]))[1]);
    
      printf("Size of long=%c\n", ((long *) (&arr[0]))[1]);
    
      printf("Size of float=%c\n", ((float *) (&arr[0]))[1]);
    
      printf("Size of double=%c\n", ((double *) (&arr[0]))[1]);
    
      return 0;
    }
    Last edited by Salem; 10-06-2014 at 04:12 PM. Reason: Fakebook links

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I'm surprised you get anything, since it's so obviously broken.
    Code:
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:13:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat]
    foo.c:15:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:17:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]
    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 user.beshi's Avatar
    Join Date
    Oct 2014
    Posts
    9
    Quote Originally Posted by Salem View Post
    I'm surprised you get anything, since it's so obviously broken.
    Code:
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:13:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat]
    foo.c:15:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:17:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]
    I know about those warning,i said that i was checking only..checking a different way to get size of data types..but why float is not working!!can you tell me?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The result of doing undefined stuff is NOT defined!!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User user.beshi's Avatar
    Join Date
    Oct 2014
    Posts
    9
    i got output from those 6 printf like
    1
    2
    4
    4

    8
    as i expected but in 5th printf where float showing nothing..if float not showing then why double is working?? i checked that int* reached to 4bytes ahead as well as float went same address..but from that address %c should show 4.. why not showing by float*???? all are showing even same type double also but why not float*?
    Last edited by user.beshi; 10-06-2014 at 02:41 PM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    if float not showing then why double is working?
    Refer to post #4 above (and post #2 also, for good measure).

    What exactly are you trying to accomplish? Why not just use "sizeof"?

  7. #7
    Registered User user.beshi's Avatar
    Join Date
    Oct 2014
    Posts
    9

    Exclamation

    Quote Originally Posted by Matticus View Post
    Refer to post #4 above (and post #2 also, for good measure).

    What exactly are you trying to accomplish? Why not just use "sizeof"?
    may be i failed to make you understand what i'm asking... i know about sizeof... i said in my post that i was just testing.....i know about those 3 warning,still i surprised..long and double are the holder of rest 2 warning but they are working as i told,but why not float??what wrong with that float* bit petterns...
    Last edited by user.beshi; 10-06-2014 at 03:00 PM.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First you should be considering those warning as errors! Second it is not surprising that any one of those printf() statements fail, it is surprising that any of those printf() functions work at all. When you use a specifier that doesn't match the variable type anything can happen. This means that any or all of those statements may work as you seem to expect, or they may totally fail. This is the meaning of Undefined behavior.

    Jim

  9. #9
    Registered User user.beshi's Avatar
    Join Date
    Oct 2014
    Posts
    9
    Quote Originally Posted by jimblumberg View Post
    First you should be considering those warning as errors! Second it is not surprising that any one of those printf() statements fail, it is surprising that any of those printf() functions work at all. When you use a specifier that doesn't match the variable type anything can happen. This means that any or all of those statements may work as you seem to expect, or they may totally fail. This is the meaning of Undefined behavior.

    Jim
    yeah yeah i understand that... i just told it to go just 32bits(size of float) forward to that address that holds '4' as a character.so from that address next 8 bits are 0011 0100.i just told to print those 8 bits by %c..it should printf 4 after converting those bits as character.though float* reached to my desired address but not showing next 8bits properly..i wanna know the reason only..long also goes to same address and that %c at that printf showing those next 8bits as well as double* goes 64bits ahead and then %c printf next 8bits properly..why its not working when float* goes??forget about warning for a while,come to the point..its surprising to me coz i cant agree with your comment
    When you use a specifier that doesn't match the variable type anything can happen
    coz there are always a reason, nothing cause like
    anything can happen
    ..i just wanna know that reason. if you know then tell that..i dont wanna get reply like
    anything can happen

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    There's no way to know because it depends on each compiler and how they react to 'undefined behavior'.
    Maybe another compiler will show float and not the double. verstehen?

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If you really want to find the answer of what it is doing; then, find out how to have the compiler generate assembly output.
    Then, read the assembly code.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    All of them, except for char, would likely fail to produce the right answer on a big endian machine.

    For example, the 'short' value would consist of a word containing the bytes '2' and '3', and it's only the quirk of the machine endianess that means it prints 2.

    As for float, there is something else to consider.
    Whenever a float is passed to a variadic function like printf, it is automatically promoted to double. You might think you were going to get lucky and just extract the LSB of the passed float, but what you actually get is the LSB of the promoted double. This of course has nothing to do with the original value you thought you had.
    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.

  13. #13
    Registered User user.beshi's Avatar
    Join Date
    Oct 2014
    Posts
    9
    Quote Originally Posted by stahta01 View Post
    If you really want to find the answer of what it is doing; then, find out how to have the compiler generate assembly output.
    Then, read the assembly code.

    Tim S.
    i wanted to find answer by not doing this

  14. #14
    Registered User user.beshi's Avatar
    Join Date
    Oct 2014
    Posts
    9
    Quote Originally Posted by Salem View Post
    All of them, except for char, would likely fail to produce the right answer on a big endian machine.

    For example, the 'short' value would consist of a word containing the bytes '2' and '3', and it's only the quirk of the machine endianess that means it prints 2.

    As for float, there is something else to consider.
    Whenever a float is passed to a variadic function like printf, it is automatically promoted to double. You might think you were going to get lucky and just extract the LSB of the passed float, but what you actually get is the LSB of the promoted double. This of course has nothing to do with the original value you thought you had.
    thanks.. i thought if it is automatically promoted to double then it may goes to at first of that address holds '8'. so if it is not gonna show 4 then it will show 8. now i got it, problem(to me) caused when it is converting to double .i checked it more and that made me more satisfied.. thanks.
    Last edited by user.beshi; 10-07-2014 at 01:35 AM.

  15. #15
    Registered User user.beshi's Avatar
    Join Date
    Oct 2014
    Posts
    9
    i done it in other way.i saw that float* goes to my desired address but not printing next 8bits properly..so from that address i just do forward 8bits and again backed 8bits.then i tell to print next 8bits by %c ..it is working by printing 4
    Code:
    printf("Size of float=%c\n",((char*)(((float *) (arr))+1))[1]-1)
    .. .i think i got it where the problem is

    now without any warning my prog will work fine
    Code:
    #include<stdio.h>
    int main()
    {
    
      unsigned char arr[15] = { '0', '1', '2', '3', '4','5', '6', '7', '8', '9','a','b','c','d','e' };
    
      printf("Size of char=%c\n", ((char *) (&arr[0]))[1]);
    
      printf("Size of short=%c\n", ((short *) (&arr[0]))[1]);
    
      printf("Size of int=%c\n", ((int *) (&arr[0]))[1]);
    
      printf("Size of long=%c\n", ((char*)(((long *) (arr))+1))[1]-1);
    
      printf("Size of float=%c\n",((char*)(((float *) (arr))+1))[1]-1);
    
      printf("Size of double=%c\n", ((char*)(((double *) (arr))+1))[1]-1);
    
      return 0;
    }
    Last edited by user.beshi; 10-07-2014 at 02:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Float
    By GokhanK in forum C Programming
    Replies: 10
    Last Post: 08-28-2013, 08:11 PM
  2. Float problem
    By edmund085 in forum C Programming
    Replies: 14
    Last Post: 08-01-2011, 11:27 PM
  3. Hex to float problem
    By supi in forum C Programming
    Replies: 1
    Last Post: 07-01-2008, 01:24 AM
  4. Float problem
    By +Azazel+ in forum C Programming
    Replies: 23
    Last Post: 02-29-2008, 02:57 AM
  5. problem with float
    By aqua in forum C Programming
    Replies: 10
    Last Post: 10-04-2002, 10:24 AM

Tags for this Thread