Specifying Basic Types

The C language has four basic data types, which are specified with the keywords char, int, float, and double. The char (character) type is used for text and the int type for integers. The float and double types express real (floating-point) values.

The TYPES.C program creates variables of the four basic types and prints their values:

/* TYPES.C: Illustrate basic data types. */

#include <stdio.h>

main()

{

char char_val = 'a';

int int_val = 543;

float float_val = 11.1;

double double_val = 66.123456789;

printf( "char_val = %c\n", char_val );

printf( "int_val = %d\n", int_val );

printf( "float_val = %f\n", float_val );

printf( "double_val = %2.9f\n", double_val );

}

Here is the output from TYPES.C:

char_val = a

int_val = 543

float_val = 11.100000

double_val = 66.123456789

Each basic data type requires a different amount of memory, as illustrated in Figure 4.1. In QuickC, a char contains one byte, an int has two bytes, a float has four bytes, and a double type has eight bytes.

NOTE:

The C language is designed to run on many different computers, with machine architectures that may be quite different. To accommodate these differences, some C data types are “implementation dependent,” meaning their sizes depend on which computer you're using. For instance, the int (integer) type contains two bytes on IBM PC computers and four bytes on VAXÒ minicomputers. These differences are important only if you're transporting a program from one operating system to another. Since QuickC runs only under one operating system (Windows), this book describes C data types in Windows and DOS.

Special Type Specifiers

The C language has four special type specifiers—signed, unsigned, long, and short. These act as “adjectives” to modify the range of values expressed by a basic data type.

Summary: The char and int data types are signed by default.

The signed keyword signifies that a value can be either negative or nonnegative. If you don't specify, a char or int value is signed.

You can preface a char or int with unsigned to extend the range of nonnegative values. An unsigned int can have a value in the range 0 through 65,535, and an unsigned char can have a value of 0 through 255.

The long keyword is used to increase the size of an int or double type. A long int value contains four bytes (twice as many as an int) and expresses an integer in the range –2,147,483,648 through 2,147,483,647. A long double value contains 10 bytes and can express a floating-point number with 19 digits of precision.

In QuickC, the short int type is identical to the int type. (This is not the case in some operating systems other than DOS.)

Table 4.1 lists the basic data types and the range of values each can express.

Table 4.1 Basic Data Types

Type Name Other Names Range of Values

char signed char –128 to 127
unsigned char none 0 to 255
int signed, signed int –32,768 to 32,767
unsigned unsigned int 0 to 65,535
unsigned short unsigned short int 0 to 65,535
short short int, signed short –32,768 to 32,767
signed short int ,  
long long int, signed long –2,147,483,648 to
signed long int 2,147,483,647
unsigned long unsigned long int 0 to 4,294,967,295
float none Approximately 1.2E–38 to 3.4E+38 (7-digit precision)
double none Approximately 2.2E–308 to 1.8E+308 (15-digit precision)
long double none Approximately 3.4E–4932 to 1.2E+4932 (19-digit precision)

Most programmers take advantage of type defaults. If a type qualifier appears alone, the type int is implied. By itself, short is a synonym for short int. Where long appears alone it is a synonym for long int, and where unsigned appears alone it is a synonym for unsigned int.