Programming in C: Unit I (b): Introduction to C

Basic Data Types in C

C Program

C language provides very few basic data types.lists the basic data types, their size, range, and usage for a C programmer on a 16-bit computer.

BASIC DATA TYPES IN C

C language provides very few basic data types. Table 2.3 lists the basic data types, their size, range, and usage for a C programmer on a 16-bit computer. In addition to this, we also have variants of int and float data types.

The char data type is of one byte and is used to store single characters. Note that C does not provide any data type for storing text. This is because text is made up of xt is individual characters.

You will be surprised to see that the range of char is given as -128 to 127. char is supposed to store characters not numbers, so why this range? The answer is that, in memory characters are stored in their ASCII codes. For example, the character A has the ASCII code 65. In memory we will not store 'A' but 65 (in binary number format).

In addition, C also supports four modifiers-two sign specifiers (signed and unsigned) and two size specifiers (short and long).

Table 2.4 shows the variants of basic data types.

In Table 2.4, we have unsigned char and signed char. Do we have negative characters? No, then why do we have such data types? The answer is that we use signed and unsigned char to ensure portability of programs that store non-character data as char.

While the smaller data types take less memory, the larger types incur a performance penalty. Although the data type we use for our variables does not have a big impact on the speed or memory usage of the application, we should always try to use int unless there is a special need to use any other data type.

Last but not the least the void type holds no value. It is primarily used in three cases:

• To specify the return type of a function (when the function returns no value)

• To specify the parameters of the function (when the function accepts no arguments from the caller).

• To create generic pointers. We will read about generic pointers in the chapter on Pointers.

We will discuss the void data type in detail in the coming chapters.

Note

Unsigned int/char keeps the sign bit free and makes the entire word available for storage of the non-negative numbers.

Sign bit is the leftmost bit of a memory word which is used to determine the sign of the content stored in that word. When it is 0, the value is positive and when it is 1, the value is negative.

How are Float and Double Stored?

In computer memory, float and double values are stored in mantissa and exponent forms where the exponent represents power of 2 (not 10). The number of bytes used to represent a floating point number generally depends on the precision of the value. While float is used to declare single-precision values, double is used to represent double- precision values.

Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format to represent mantissa and exponents. According to the IEEE format, a floating point value in its binary form is known as a normalized form. In the normalized form, the exponent is adjusted in such a way that the binary point in the mantissa always lies to the right of the most significant non-zero digit.

Example 2.1

Convert the floating point number 5.32 into an IEEE normalized form.

Moreover, the IEEE format for storing floating point numbers uses a sign bit, mantissa, and the exponent (Figure 2.9). The sign bit denotes the sign of the value. If the value is positive, the sign bit contains 0 and in case the value is negative it stores 1.

Generally, exponent is an integer value stored in unsigned binary format after adding a positive bias. In other words, because exponents are stored in an unsigned form, the exponent is biased by half its possible value. For type float, the bias is 127; for type double, it is 1023. You can compute the actual exponent value by subtracting the bias value from the exponent value. Finally, the normalized binary equivalent is stored in such a way that lower byte is stored at higher memory address. For example, ABCD is actually stored as DCBA.

Programming in C: Unit I (b): Introduction to C : Tag: : C Program - Basic Data Types in C