Thread: C - type conversion

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    C - type conversion

    Hi all,

    Can someone explain this to me?

    double number = 68.6;

    printf("%c\n", number);

    when i print the number as char, it displayed char 'f'. For ASCII code, lower case 'f' should be 102.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your code gives undefined behaviour (forcing printf() to treat number as if it was a char).

    "Undefined", as that term is defined in the C standard, effectively means ANYTHING is allowed to happen. That can range from a program crash to printing output you do not expect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    thanks for reply:

    It is actually a question like this:

    int z=6;
    int y=4;
    double n=0.4;
    char a = 'C';

    printf("%c\n", z%y-n+a);

    i can manage to debug it, but am just not sure why the answer could be 'f'.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    z%y-n+a
    The result of expression is double. variadic function like printf does not have type promotion.
    So you are having undefined behavior.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    so it will be no explanations for this undefined behavior? The answer could be any char depend on the result from z%y-n+a?
    Nothing to do with ASCII?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A double is 8 bytes. The value 68.6 is encoded in some manner in those eight bytes - the "how" of that encoding depends on the floating point representation of your computer. printf() might be printing one of those bytes.

    The problem is, because the behaviour is undefined, the code is allowed to do ANYTHING. There is a good chance, for example, that results will be different if you use another compiler. With yet another compiler, your code may simply reformat your hard drive (yes, unlikely, but not infeasible).

    The bottom line is that you should not use "%c" format specifier for a value of type double.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Yes and no.

    Going back to your original post, ASCII code for 'f' is 102 or binary (0110 0110). First byte of 68.6 in binary is also (0110 0110). You can see why your getting 'f' from printf(), then.

    I wouldn't particularly count on this behavior, tho.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    thanks! got it now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expected constructor, destructor, type conversion
    By SterlingM in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2010, 01:16 PM
  2. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  3. type conversion
    By peterx in forum C Programming
    Replies: 3
    Last Post: 10-08-2005, 04:03 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Windows using Dev-C++
    By Renegade in forum C++ Programming
    Replies: 15
    Last Post: 07-07-2005, 08:29 PM