Assigning integers to char type in C/C++
In c/c++ the char type can be used to store a single character or small integers.
Have a look at the program below and determine what the value of “ch” would be without running the program.
#include <stdio.h>
int main(){
char ch = 127;
ch++;
return 0;
}
What would the value of ch be when the program is executed? Most people would guess it would be 128, but the right answer is -128.
The reason behind it is because a signed char type in c/c++ only accepts values from -128 to 127 and when ch++ is executed 127 goes to the next avaialble number which is -128.
Categories: OOP344