tags:

views:

627

answers:

2
#include<stdio.h>

int main()
{

    unsigned char c;
    c = 300;
    printf("%d",c);
    return 0;
}

Is the output in any way predictable or its undefined??

+5  A: 

Sorry for the first answer, here is an explanation from the C++ standards :)

Is the output in any way predictable or its undefined??

It is predictable. There are two points to look after in this code: First, the assignment of value that the type unsigned char can't hold:

unsigned char c;
c = 300;

3.9.1 Fundamental types (Page 54)

Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.41)
...
41) This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

Basically:

c = 300 % (std::numeric_limits<unsigned char>::max() + 1);

Second, passing %d in the format string of printf to print unsigned char variable.
This one ysth got it right ;) There is no undefined behavior, because a promotional conversion from unsigned char to int happens in the case of variadic arguments!

Note: that the second part of the answer is a rephrasing of what have been said in the comments of this answer but it is not my answer originally.

AraK
"numeric_limit" is actually "numeric_limits"
shadyabhi
Where can I read all these C++ standards?
shadyabhi
You have to buy the official docs if you like, or just go to http://www.open-std.org/jtc1/sc22/wg21/ and download the latest draft for free: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3000.pdf
AraK
I am new to this site. I just wanna know, how do u people actually get to know this fast that this question is asked..:) How come you are alerted about the question so fast?
shadyabhi
@Shadyabhi You can add whatever "tag" you like to your _Interesting Tags_ on the right of the main page of StackOverflow. Those questions will be highlighted :)
AraK
@AraK.. So, i mean you actually keep refreshing the page if you wish want to be the first to answer the question..
shadyabhi
If `UCHAR_MAX` is greater than `INT_MAX`, then the promotion would be to `unsigned int`. And printing an `unsigned int` with `"%d"` is implementation-defined (or an implementation-defined signal is raised).
Alok
But for the number `300%(UCHAR_MAX+1)`, the result is well-defined of course, since that is guaranteed to be representable by an `int`. I added the above for completeness.
Alok
+2  A: 

The result of the assignment should be predictable:

3.9.1

4 Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.17)

17) This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

In addition, sizeof(char) is defined as 1, and sizeof(unsigned char) = sizeof(char), so you should see the same result regardless of implementation (assuming you don't have bytes with funny sizes other than 8).

However, the warning is telling you that the result is probably not what you intended (for example, perhaps you overestimated the size of the unsigned type?). If that's what you intended, why not write 300 % (1 << CHAR_BIT) (assuming that 300 is somehow significant to you)?

UncleBens