Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Storage of integer and character values in C
We have used the integer and character variables many times in our program. Here we will see how they are stored in the memory.
In C the character values are also stored as integers. When we assign a value larger than the character range to a char variable, overflow occurs and only the lower 8 bits are stored.
Syntax
char variable_name = value; int variable_name = value;
Example: Character Overflow Behavior
In the following code, we shall put 270 into a character type data. The binary equivalent of 270 is 100001110, but char takes only the rightmost 8-bits. So the result will be (00001110), that is 14 −
#include <stdio.h>
int main() {
char x = 270;
char y = -130;
printf("The value of x is: %d
", x);
printf("The value of y is: %d
", y);
return 0;
}
The value of x is: 14 The value of y is: 126
How It Works
For variable x = 270:
- 270 in binary: 100001110 (9 bits)
- char stores only 8 bits: 00001110
- Result: 14 in decimal
For variable y = -130:
- -130 is stored using 2's complement method
- 130 in binary: 10000010
- 2's complement: flip bits (01111101) + 1 = 01111110
- Only rightmost 8 bits stored: 01111110 = 126
Key Points
- char variables store values in the range -128 to 127 (signed)
- Values outside this range cause overflow and wrap around
- Negative numbers use 2's complement representation
Conclusion
Character variables in C store integer values using 8 bits. When values exceed the char range, only the lower 8 bits are preserved, causing overflow behavior that follows predictable binary arithmetic rules.
