Basic Data Types

The basic data types in C are character (char), integer (int), and floating point (float and double). All other data types are derived from these basic types. For example, a string is an array of char values.

Table 0.1 lists the range of values for each data type.

Table 0.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
signed short int 32,767
long long int, signed long –2,147,483,647 to
  signed long int 2,147,483,648
unsigned long unsigned long int 0 to 4,294,967,295
__segment none 0 to 65,535
enum none –32,768 to 32,767
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)

Character Type

The character type (char) occupies one byte of storage and can express a whole number in the range of –128 to 127. Unsigned characters have a range of 0 to 255. You can represent any ASCII character as an unsigned char value.

Typical declarations of character types are shown below:

char answer; /* Declare a character variable answer */

char alpha = 'a'; /* Declare character variable alpha

and initialize it */

A character constant represents a single ASCII character. Typical character constants are shown below:

char alpha = 'a'; /* Declare and initialize */

char c2 = 0x61; /* Declare and initialize with

hexadecimal value for 'a' */

Escape Sequences

Escape sequences represent special characters, such as the carriage return. An escape sequence consists of a backslash character plus a letter or punctuation mark. Table 0.2 lists the C escape sequences; they are also listed in online help.

Table 0.2 C Escape Sequences

Character Meaning Hexadecimal Value

\a [DOS only] Alert (bell) 0x07
\n Newline (linefeed) 0x0A
\b Backspace 0x08
\r Carriage return 0x0D
\f [DOS only] Formfeed 0x0C
\t [DOS only] Tab 0x09
\v [DOS only] Vertical tab 0x0B
\\ Backslash 0x5C
\' Single quote 0x27
\" Double quote 0x22
\0 Null 0x00

Integer Type

The integer (int) type occupies two bytes of storage and can express a whole number in the range –32,768 to 32,767. Unsigned integers (unsigned or unsigned int) have a range of 0 to 65,535.

In QuickC, short integers (short or short int) are the same as integers (int). Note that the short and int types are not the same in some operating systems other than DOS.

Signed long integers (long) occupy four bytes and have a range of
–2,147,483,648 to 2,147,483,647. Unsigned long integers have a range
of 0 to 4,294,967,295.

Integer variables are declared with the keywords int, short, unsigned, or long. Typical declarations of integer types are shown below:

int z; /* Declare an int variable z */

int ten = 10; /* Declare int variable and

assign it the value 10 */

unsigned int a; /* Declare unsigned int variable */

unsigned long BigInt = 2000000001UL; /* Declare and

initialize */

Integer constants are used to represent decimal, octal, and hexadecimal numbers. There are three types of integer constants:

1.Decimal constants can only contain the digits 0–9. The first digit must not be 0.

2.Octal constants can only contain the digits 0–7. The first digit must be 0.

3.Hexadecimal constants can only contain the digits 0–9, plus the letters a–f or A–F. The constant must begin with either 0x or 0X.

You can specify that an integer constant is long by adding the suffix l or L. The suffix can be used with decimal, hexadecimal, or octal notation.

To specify that an integer constant is short, add the suffix u or U. This suffix can also be used with decimal, hexadecimal, or octal notation.

Typical integer constants are shown below:

42 /* Decimal constant */

0x34 /* Hexadecimal constant */

0x3cL /* Long hexadecimal constant */

052 /* Octal constant */

Floating-Point Types

You can declare floating-point variables using the keywords float or double. The float type occupies four bytes of storage and can express a floating-point value in the range 1.2E–38 to 3.4E+38. This type has seven-digit precision.

The double type occupies eight bytes of storage and can express a floating-point value in the range 2.2E–308 to 1.8E+308. This type has fifteen-digit precision.

The long double type occupies ten bytes of storage and can express a floating-point value in the range 3.4E–4932 to 1.2E+4932. This type has nineteen-digit precision.

Typical declarations of floating-point types are shown below:

float SmallPi = 3.14; /* Declare floating-point variable */

double AccuratePi = 3.141592653596 /* Declare

double-precision */

Floating-point constants can represent decimal numbers in either single or double precision. A floating-point constant must either contain a decimal point or end with the suffix e or E. Typical floating-point constants are shown below:

2.78 /* Floating-point constant */

3E /* Floating-point constant */