-6

I was trying to get into c programming, but in a question a get stuck, please explain this.

 int main()
{
 char c = 255;
 c=c+10;
 printf("%d",c);
 return 0;
 }

the output it gave is

> 9

kindly explain this to me.

1
  • @Biffen my bad wrong research i suppose i will remove the comment Commented Feb 20, 2019 at 10:02

1 Answer 1

4

The maximum value of a char is 255.

By adding 10 to that number you get 265.

Because that value is not a suitable value for a char it will do 265 % 256 resulting 9

That's why your result is 9

Sign up to request clarification or add additional context in comments.

3 Comments

The modulo reasoning is not quite correct. You don't use module 255 but module 256.
Oh yeah because there are 256 values possible (0 to 255). You're right
If the char is signed then it is undefined behaviour.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.