-5
#include <stdio.h>
int main()
{
    signed char ch;
    ch = 128;
    printf("%f", ch);
    return 0;
}

Can anyone explain why it is printing 0.000 every time??

I tried %f as a format specifier for signed char value.

Dominique
  • 16,450
  • 15
  • 56
  • 112
premdanav
  • 9
  • 2
  • 4
    I think you need to read more carefully the documentation on the format parameter of the "printf()" function. "%f" is to be used with type "float", not with signed characters. – virolino Dec 06 '22 at 12:32
  • dear @virolino can you explain to me why it is not giving any warning and printing 0 every time?? – premdanav Dec 06 '22 at 12:37
  • 1
    Assigning a `signed char` with 128 will overflow. And as noted above %f if for floats, not integral values. What did you actually expect to get ? – wohlstad Dec 06 '22 at 12:41
  • Please learn to read documentation ASAP. It is [described there](https://en.cppreference.com/w/c/io/fprintf). Use `%f` with `float` type or `%d` with integer types. Also enable compiler warnings: https://godbolt.org/z/We3jxjP34 to detect this problems. – Marek R Dec 06 '22 at 12:58
  • You did not get a warning because the code you wrote is actually correct. The problem is that you "told" the computer to something else, not what you wanted. You just have to tell correctly to the computer exactly what you want. And why always "0.000" ? There are already answers for that, read below. – virolino Dec 07 '22 at 06:10

2 Answers2

1

If you want to print a value, that you have in an integer variable, as if it were floating point, you have to write an explicit cast.

#include <stdio.h>
int main()
{
    signed char ch;
    ch = 128;
    printf("%f\n", (double)ch);
    return 0;
}

In a call to most functions that take a double or float argument, the compiler would insert this cast for you, but it doesn't do it for printf because printf's argument list isn't declared. See the older question I just linked this one to, for more detail.

Since you know the value is an integer, you can also write out the fractional part yourself:

#include <stdio.h>
int main()
{
    signed char ch;
    ch = 128;
    printf("%d.000000\n", ch);
    return 0;
}

will print the same thing as the first example.

zwol
  • 135,547
  • 38
  • 252
  • 361
0

To print the value of signed char you need to use %d format. %f id to use float and double types.

#include <stdio.h>
int main()
{
    signed char ch;
    ch = 128;
    printf("%d\n", ch);
    return 0;
}

https://godbolt.org/z/5816GvPWd

It will output -128 as you set the last bit of 8-bit integer (assuming CHAR_BIT == 8) and the system uses two's complement integers


Assignment of 128 to 8-bit signed char is implementation-defined as 128 cannot be accommodated in 8 bit integer. Most implementations will behave as I wrote above - ie print -128

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Even if 8-bit 2's complement. `ch` may have other values than -128. "Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised." – chux - Reinstate Monica Dec 06 '22 at 13:01
  • @chux-ReinstateMonica indeed - but I think that it is too complex for OP – 0___________ Dec 06 '22 at 13:06
  • I am confident this answer can be improved without undue complexity and without incorrect assertions. – chux - Reinstate Monica Dec 06 '22 at 13:14
  • @chux I don't think it is worth the trouble, considering (1) POSIX requires `CHAR_BIT == 8`, (2) C2x changes the rule you quote and I believe it now requires a result of −128, (3) in any case the odds of OP encountering a machine that _doesn't_ produce −128 are somewhere between slim and nonexistent. – zwol Dec 06 '22 at 13:18
  • @zwol POSIX requires` CHAR_BIT == 8` is not relevant to this question. C2x will certainly drop support of non-2's encoding. My read of C2x does not change the "the new type is signed and the value cannot be represented in it; either the result is implementation-defined ...." requirement. Perhaps I have an old proposal. Do you have access to the latest? Of course, once it comes out, we will know for sure. – chux - Reinstate Monica Dec 06 '22 at 13:24