Home > OOP344 > Assigning integers to char type in C/C++

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
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: