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


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



Under Subject


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation



Related Subjects


Professional English II

HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Statistics and Numerical Methods

MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation


Engineering Graphics

GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Physics for Electrical Engineering

PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Basic Civil and Mechanical Engineering

BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Electric Circuit Analysis

EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Physics for Electronics Engineering

PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Electrical and Instrumentation Engineering

BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Circuit Analysis

EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Materials Science

PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Physics for Civil Engineering

PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Basic Electrical, Electronics and Instrumentation Engineering

BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Physics for Information Science

PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation