Pages

C PROGRAMMING UNIT 2 Data Types (PART 1)


THE BUILT-IN DATA TYPES IN C
Introduction
Data types are provided to store various types of data that is processed in real life. A student's record might contain the following data types: name, roll number, and grade percentage. For example, a student named Anil might be assigned roll number 5 and have a grade percentage of 78.67. The roll number is an integer without a decimal point, the name consists of all alpha characters, and the grade percentage is numerical with a decimal point. C supports representation of this data and gives instructions or statements for processing such data. In general, data is stored in the program in variables, and the kind of data the variable can have is specified by the data type. Using this example, grade percentage has a float data type, and roll number has an integer data type. The data type is attached to the variable at the time of declaration, and it remains attached to the variable for the lifetime of the program. Data type indicates what information is stored in the variable, the amount of memory that can be allocated for storing the data in the variable, and the available operations that can be performed on the variable. For example, the operation S1 * S2, where S1 and S2 are character strings, is not valid for character strings because character strings cannot be multipled.
Program
// the program gives maximum and minimum values of data type
#include <stdio.h>
main()
{
int i,j ;// A
i = 1;
while (i > 0)
{
j = i;
i++;
}
printf ("the maximum value of integer is %d\n",j);
printf ("the value of integer after overflow is %d\n",i);
}
Explanation
  1. In this program there are two variables, i and j, of the type integer, which is declared in statement A.
  2. The variables should be declared in the declaration section at the beginning of the block.
  3. If you use variables without declaring them, the compiler returns an error.
Points to Remember
  1. C supports various data types such as float, int, char, etc., for storing data.
  2. The variables should be declared by specifying the data type.
  3. The data type determines the number of bytes to be allocated to the variable and the valid operations that can be performed on the variable.
VARIOUS DATA TYPES IN C
Introduction
C supports various data types for processing information. There is a family of integer data types and floating-point data types. Characters are stored internally as integers, and they are interpreted according to the character set. The most commonly used character set is ASCII. In the ASCII character set, A is represented by the number 65.
Program/Examples
The data type families are as follows:
Integer family
    char data type
    int data type
    short int data type
    long int data type
These data types differ in the amount of storage space allocated to their respective variables. Additionally, each type has two variants, signed and unsigned, which will be discussed later.
Float family (real numbers with decimal points)
    Float data type
    Double data type
(ANSI has also specified long double, which occupies the same storage space as double)
Explanation
  1. Data type determines how much storage space is allocated to variables.
  2. Data type determines the permissible operations on variables.
Points to Remember
  1. C has two main data type families: integer for representing whole numbers and characters of text data, and float for representing the real-life numbers.
  2. Each family has sub-data types that differ in the amount of storage space allocated to them.
  3. In general, the data types that are allocated more storage space can store larger values.

THE INTEGER DATA TYPE FAMILY
Introduction
Integer data types are used for storing whole numbers and characters. The integers are internally stored in binary form.
Program/Example
Here is an example that shows how integers are stored in the binary form.
Number =13
  • Decimal representation = 1*101 + 3*100
  • Binary representation = 1101 = 1*23 + 1*22 + 0*21 + 1*1
Each 1 or 0 is called a bit, thus the number 13 requires 4 bits.
In the same way, the number 130 is 1000 0010 in binary.
If the general data type is char, 8 bits are allocated. Using 8 bits, you can normally represent decimal numbers from 0 to 255 (0000 0000 to 1111 1111). This is the case when the data type is unsigned char. However, with signed char, the leftmost bit is used to represent the sign of the number. If the sign bit is 0, the number is positive, but if it is 1, the number is negative.
Binary representation of the following numbers in signed char is as follows:
Number = 127 Binary representation = 0111 1111 (leftmost bit is 0, indicating positive.)
Number = −128 Binary representation = 1000 0000 (leftmost bit is 1, indicating negative.)
The negative numbers are stored in a special form called "2's complement". It can be explained as follows:
Suppose you want to represent −127:
  1. Convert 127 to binary form, i.e. 0111 1111.
  2. Complement each bit: put a 0 wherever there is 1 and for 0 put 1. So you will get 1000 0000.
  3. Add 1 to the above number
4.    1000 0000
5.   +       1
6.  -------------
7.   1000 0001 (−127)
Thus in the signed char you can have the range −128 to +127, i.e. (−28 to 28−1).
The binary representation also indicates the values in the case of overflow. Suppose you start with value 1 in char and keep adding 1. You will get the following values in binary representation:
0000 0001 (1)
0111 1111 (127)
1000 0000 (-128)
1000 0001 (-127)
In the case of unsigned char you will get
0000 0001 (1)
0111 1111 (127)
1000 0000 (128)
1000 0001 (129)
1111 1111 (255)
0000 0000 (0)
This concept is useful in finding out the behavior of the integer family data types.
The bytes allocated to the integer family data types are (1 byte = 8 bits) shown in Table 2.1.
Table 2.1: Integer data type storage allocations
Data Type
Allocation
Range
signed char
1 byte
−27 to 27−1 (−128 to 127)
Unsigned char
1 byte
0 to 28−1 (0 to 255)
short
2 bytes
−215 to 215 −1 (−32768 to 32767)
Unsigned short
2 bytes
0 to 216 −1 (0 to 65535)
long int
4 bytes
231 to 231−1 (2,147,483,648 to 2,147,483,647)
int
2 or 4 bytes depending on implementation
Range for 2 or 4 bytes as given above
Explanation
  1. In C, the range of the number depends on the number of bytes allocated and whether the number is signed.
  2. If the data type is unsigned the lower value is 0 and the upper depends on the number of bytes allocated.
  3. If the data type is signed then the leftmost bit is used as a sign bit.
  4. The negative number is stored in 2's complement form.
  5. The overflow behavior is determined by the binary presentation and its interpretation, that is, whether or not the number is signed.
Points to Remember
  1. The behavior of a data type can be analyzed according to its binary representation.
  2. In the case of binary representation, you have to determine whether the number is positive or negative.
OVERFLOW IN char AND UNSIGNED char DATA TYPES
Introduction
Overflow means you are carrying out an operation such that the value either exceeds the maximum value or is less than the minimum value of the data type.
Program
// the program gives maximum and minimum values of data type
#include <stdio.h>
main()
{
char i,j ;
i = 1;
while (i > 0) // A
{
j = i; // B
i++; // C
}
printf ("the maximum value of char is %d\n",j);
printf ("the value of char after overflow is %d\n",i);
}
Explanation
  1. This program is used to calculate the maximum positive value of char data type and the result of an operation that tries to exceed the maximum positive value.
  2. The while loop is terminated when the value of i is negative, as given in statement A. This is because if you try to add 1 to the maximum value you get a negative value, as explained previously (127 + 1 gives −128).
  3. The variable j stores the previous value of i as given in statement B.
  4. The program determines the maximum value as 127. The value after overflow is -128.
  5. The initial value of i is 1 and it is incremented by 1 in the while loop. After i reaches 127, the next value is -128 and the loop is terminated.
Points to Remember
  1. In the case of signed char, if you continue adding 1 then you will get the maximum value, and if you add 1 to the maximum value then you will get the most negative value.
  2. You can try this program for short and int, but be careful when you are using int. If the implementation is 4 bytes it will take too much time to terminate the while loop.
  3. You can try this program for unsigned char. Here you will get the maximum value, 255. The value after overflow is 0.

THE char TYPE
Introduction
Alpha characters are stored internally as integers. Since each character can have 8 bits, you can have 256 different character values (0–255). Each integer is associated with a character using a character set. The most commonly used character set is ASCII. In ASCII, "A" is represented as decimal value 65, octal value 101, or hexadecimal value 41.
Explanation
If you declared C as a character as
char c;
then you can assign A as follows:
c = 'A';
c = 65;
c = '\x41';   // Hexadecimal representation
c = '\101';   // Octal representation
You cannot write c = ‘A’ because ‘A’ is interpreted as a string.
Escape Sequence
Certain characters are not printable but can be used to give directive to functions such as printf. For example, to move printing to the next line you can use the character "\n". These characters are called escape sequences. Though the escape sequences look like two characters, each represents only a single character.
The complete selection of escape sequences is shown here.
\a
alert (bell) character
\\
backslash
\b
backspace
\?
question mark
\f
form feed
\’
single quote
\n
new line
\"
double quote
\r
carriage return
\ooo
octal number
\t
horizontal tab
\xhh
hexadecimal number
\v
vertical tab


Points to Remember
  1. Characters are stored as a set of 255 integers and the integer value is interpreted according to the character set.
  2. The most common character set is ASCII.
  3. You can give directive to functions such as printf by using escape sequence characters.