DataType in C Language

Data Types in C, C Language is rich in its data types. A data types is used to indicate the type of data value stored in a variable. The data type of a variable is specified at the time of its declaration and is attached till the end of execution of the program.
ANSI C supports three classes of data types :

  1. Primary or Fundamental Data Types
  2. Derived Data Types
  3. User Defined Data Types

1. Primary or Fundamental Data Types

All C compilers support five fundamental data types:

  • Integer (int) Data Type
  • Floating - Point (float) Data Type
  • Double Precision Floating Point(double) Data Type
  • Void Data Type
  • Character (char) Data Type

Many of them also offer extended data types such as long int and long double.

Data Types Range of values
Char -128 to 127
int -32768 to 32767
float 3.4e-38 to 3.4e+38
double 1.7e-308 ti 1.7e+308

Integer (int) Data Type

Integers are whole numbers with a range of values supported by a particular machine. Generally, integers occupy one word of storage, and since the word sizes of machines vary(typically, 16 or 32 bits) the size of an integer that can be stored depends on the computer.

If we used a 16 bit word length, the size of the integer value is limited to the range -32768 to +32767. A signed integer uses one bit for sign and 15 bits for the magnitude of the number. Similarly, a 32 bit word length can store an integer ranging from -2,147,483,648 to 2,147,483,647.

C has three classes of integer storage, namely short int, int and long int in both signed and unsigned forms. ANSI C defines these types so that they can be organized from the smallest to the largest.

For example, short int represents fairly small integer values and requires half the amount of storage as a regular int number uses.

Unlike signed integers, unsigned integers use all the bits for the magnitude of the number and are always positive.

Therefore, for a 16 bit machine, the range of unsigned integer numbers will be from 0 to 65535. We declare long and unsigned integers to increase the range of values.

The use of a qualifier signed on integers is optional because the default declaration assumes a signed number.

Floating - Point (float) Data Type

Floating point numbers are stored in 32 bits (on all 16 bit and 32 bit machines), with 6 digits of precision.

Floating point numbers are defined in C by the keyword float. When the accuracy provided by a float number is not sufficient, the type double can be used to define the number.

Double Precision Floating Point(double) Data Type

A double data type number uses 64 bits giving a precision of 14 digits. These are known as double precision numbers.

The double type represents the same data type that float represents, but with a greater precision. To extend the precision further, we may use long double which users 80 bits.

Void Data Type

The void type has no value. This is usually used to specify the type of functions.

The type of a function is said to be void when it does not return any value to the calling function.

It can also play the role of a generic type, meaning that it can represent any of the other standard types.

Character (char) Data Type

A single character can be defined as character(char) type data. Characters are usually stored in 8 bits(one byte) of internal storage.

The qualifier signed or unsigned may be explicitly applied to char.
While unsigned chars have values between 0 and 225, signed chars have values from -128 to 127.

Characters are represented by ASCII values.
Characters are stored in the memory with their ASCII values.

The following figure shows all the allowed combinations of basic types and qualifiers and their size and range on a 16 bit machine.

Type Size (bits) Range
char or single char 8 -128 to 127
unsigned char 8 0 to 255
int or signed int 16 -32768 to 32767
unsigned int 16 0 to 65535
short int, signed short int 8 -128 to 127
unsigned short int 8 0 to 255
long int or signed long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
float 32 3.4E-38 to 3.4E+38
double 64 1.7E-308 to 1.7E+308
long double 80 3.4E-4932 to 1.1E+4932

2. Derived Data Types :

  1. Arrays
  2. Pointers
  3. Functions

3. User Defined Type Declaration :

  1. Type Definition Data Type
  2. Enumerated Data Type
0 Komentar untuk "DataType in C Language"

Back To Top