#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??
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sorry for the first answer, here is an explanation from the C++ standards 🙂
It is predictable. There are two points to look after in this code:
First, the assignment of value that the type
unsigned charcan’t hold:Basically:
Second, passing
%din the format string ofprintfto printunsigned charvariable.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.