Home > Backend Development > C++ > In C language, storage of integer and character values

In C language, storage of integer and character values

WBOY
Release: 2023-08-26 21:05:06
forward
541 people have browsed it

In C language, storage of integer and character values

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. In the following code, we shall put 270 into a character type data. So the binary equivalent of 270 is 100001110, but takes only first 8-bits from right. So the result will be (00001110), that is 14. Then stores the value into variable a. It also gives warning for overflow.

In the next variable y, we are trying to store negative number say -130. The negative number will be stored as 2’s complemented method. So the binary of 130 is (10000010). The 2’s complemented value is 01111101 1 = 01111110. Here also the right most 8-bits are taken. So the result will be (01111110) = 126

Example

#include <stdio.h>
int main() {
   char x = 270;
   char y = -130;
   printf("The value of x is: %d</p><p>", x);
   printf("The value of y is: %d", y);
}
Copy after login

输出

The value of x is: 14
The value of y is: 126
Copy after login

The above is the detailed content of In C language, storage of integer and character values. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template