How Memory Computer Storing C Data Type

wahyu eko hadi saputro
4 min readDec 27, 2022

--

C programming has some data type, some sort of data type in C are int, char. I am actually curious about how C stores some data types particularly int, char and floating point.

Char datatype

A char data type, even though we call it char, computer will store char data in binary (0 or 1). For simplicity there is ANSI format (8 bit encoding) to handle char encoding,

The American National Standards Institute (ANSI) is a private nonprofit organization that oversees the development of voluntary consensus standards for products, services, processes, systems, and personnel in the United States.

some ANCI encoding

int main(){
char ch = 'A';
printf("ch : %d\n", ch );
printf("ch : %c\n", ch );
return 0;
}

PS D:\wahyu\c\array> .\str.exe
ch : 65
ch : A

Based on the above code, the char ‘A’ will be translated into 65 then translated to binary : 1000001. Based computer perspective, char ‘A’ and number 65 are treated as the same binary that is 1000001. On a computer level, the computer only understands 1 and 0, it is dependent on software level to interpret the binary. If we want to interpret 1000001 as char ‘A’, we can use format printf(“ch : %c\n”, ch );. And if we want to interpret 1000001 as an integer we can use format printf(“ch : %d\n”, ch );.

Integer data type

In C, integer data type is divided into 2 groups, signed and unsigned int. By default int is signed integer if we don’t put any signed or unsigned keyword in front of the int keyword.


int main(){

int a = 2147483647; //01111111111111111111111111111111
printf("a : %d\n", a ); //print signed int
printf("a : %u\n", a ); //print unsigned int

int b = -2147483648; // 10000000000000000000000000000000
printf("b : %d\n", b ); //print signed int
printf("b : %u\n", b ); //print unsigned int

int c = -1; // 11111111111111111111111111111111
printf("c : %d\n", c ); //print signed int
printf("c : %u\n", c ); //print unsigned int
return 0;
}

PS D:\wahyu\c\array> .\str.exe
a : 2147483647
a : 2147483647
b : -2147483648
b : 2147483648
c : -1
c : 4294967295

Based on the code above, we can conclude C uses the sign-magnitude form, in this form, a binary number has a bit for a sign symbol. If this bit is set to 1, the number will be negative else the number will be positive if it is set to 0.

-2147483648 = 10000000000000000000000000000000
-2147483647 = 10000000000000000000000000000001


-2 = 11111111111111111111111111111110
-1 = 11111111111111111111111111111111
1 = 00000000000000000000000000000001

2147483647 = 01111111111111111111111111111111

Floating point

For floating point there is standard IEEE format for storing floating point, but I am not sure C is following IEEE 754 format or not. The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE).

To store a floating-point number, 32 bit memory will be allocated in computer:

1 bit for sign

8 bit for exponent

23 bit for significant

An example of a layout for 32-bit floating point is:

IEEE format block

A. Proses converting floating point 0.15625 to binary according IEEE 754 format:

1. Convert decimal to binary

0.15625 = 0.00101 in binary

2. Add bias (https://en.wikipedia.org/wiki/Exponent_bias)

For bias

N = exponent (8 bits) = 127

3. The converting process

0.00101, shift right and make the value in front of dot (.) is 1, at the end the result = 1.01 * 2 pow -3, where -3 is a real exponent.

Exponent (8 bits) = bias + a real exponent

127 + (-3) = 124 = 01111100

Fraction (23 bit) = value after dot (.), so the value after dot from 1.01 = 0100000000000000000000

Sign = 0 due to positive.

final result : 0 01111100 0100000000000000000000

B. Proses converting floating point 10.50 to binary according IEEE IEEE 754 format:

10.50 = 1010.1 = 1.0101 * 2 pow 3 where 3 is the real exponent
Exponent (8 bits) = bias + a real exponent
Exponent (8 bits) = 127 + 3 = 130 = 10000010

Sign bit = 0 due to positive
Exponent (8 bits) = 10000010
Fraction = 01010000000000000000000

final result 10.50 ==> 0 10000010 01010000000000000000000

Source:

https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

https://www.javatpoint.com/signed-and-unsigned-binary-numbers-in-digital-electronics

https://en.wikipedia.org/wiki/IEEE_754

--

--

No responses yet