What is Data Types? Built-in and Derived Types with Examples
- What is a Data Type?
- Classification of Data Types
- List of Data Types with Storage Size and Value Range
- Built-in Data Types in C
- 1. int
- Size and Range of int
- 2. float
- 3. double
- 4. char
- Derived Data Types in C
- 1. Array
- 2. Pointer
- 3. Structure
- 4. Union
- Modifiers in C
- Common Modifiers in C
- a. Short and Long
- b. Signed and Unsigned
- Enumeration Data Type (enum)
- Key Points of enum Data Type:
In C programming, a data type specifies the type of data a variable can store and the amount of memory allocated to it.
There are two types of data types
- Built-in (Primitive) Data Type
- Derived Data Type
What is a Data Type?
A data type defines the type of value a variable can store and the operations that can be performed on that value. It also decides the amount of memory allocated to the variable and the nature of data it can hold.
For example, to store an integer value like 10, we must declare a variable of type int. A variable declared as int can store only whole numbers, not characters or decimal values.
Some Examples of Fundamental Data Types:
float– for decimal numbersint– for integer valueschar– for single characters
Example:
int data;
data = 20;
In the example above, the variable data is of type int. This means it can only be assigned integer values, not characters or decimal numbers.
Classification of Data Types
| Type | Data Types |
|---|---|
| Basic data types | int, char, float, double |
| Derived data type | pointer, array, structure, union |
| Enumeration data type | enum |
| Void data type | void |
List of Data Types with Storage Size and Value Range
| Type | Storage Size | Value Range |
| char | 1 byte | -128 to 127 or 0 to 255 |
| signed char | 1 byte | -128 to 127 |
| int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
| unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
| short | 2 bytes | -32,768 to 32,767 |
| unsigned short | 2 bytes | 0 to 65,535 |
| long | 4 or 8 bytes | -2,147,483,648 to 2,147,483,647 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| unsigned long | 4 or 8 bytes | 0 to 4,294,967,295 or 0 to 18,446,744,073,709,551,615 |
| float | 4 bytes | ~1.2E-38 to ~3.4E+38 |
| double | 8 bytes | ~2.3E-308 to ~1.7E+308 |
| long double | 10 bytes (system-dependent) | Typically ~3.4E-4932 to ~1.1E+4932 |
| unsigned char | 1 byte | 0 to 255 |
Built-in Data Types in C
1. int
The int data type is used to store whole numbers (integers). These numbers can be positive, negative, or zero. It cannot store decimal values.
To declare an integer variable, we use the keyword int.
int number;
Size and Range of int
The size of an int depends on the system and compiler, but:
- On most modern systems (including 32-bit and 64-bit systems), an
intusually takes 4 bytes - The C standard guarantees that
intis at least 2 bytes
Printing an int
To print an integer value in C, use the %d format specifier.
printf("%d", number);
Key Points:
- The
inttype cannot store decimal values. - If you assign a decimal value to an
int, the fractional part is truncated, and only the whole number is stored. - To store decimal values, use the
floatordoubledata type.
2. float
The float data type is used to store decimal (fractional) numbers. It stores single-precision floating-point values and is suitable when moderate precision is sufficient.
Storage Size:
- Typically 4 bytes (32 bits), as defined by the IEEE-754 standard. This size is generally fixed across compilers.
Precision:
- Offers about 6–7 significant decimal digits of precision.
Format Specifier:
- Use %f to print a
floatvalue.
Key Points:
- By default, floating-point constants like
12.5are treated asdoublein C. - To explicitly define a
floatconstant, append an f or F (e.g.,12.5f).
Example:
float price = 12.5f;
printf("%f", price);
3. double
The double data type stores double-precision floating-point numbers. It provides higher precision and a wider range (about 15–16 significant digits) than float, making it suitable for calculations that require high accuracy.
Storage Size:
- Typically 8 bytes (64 bits)
- Follows the IEEE-754 double-precision standard on most systems
Range & Precision:
- Range: Approximately 1.7E-308 to 1.7E+308.
- Precision: About 15–16 significant decimal digits.
Format Specifiers:
- %f: Fixed-point notation.
- %e: Scientific (exponential) notation.
- %g: Uses the shorter of %f or %e.
Example:
double value = 12345.6789;
printf("%f\n", value);
printf("%e\n", value);
printf("%g\n", value);
4. char
The char data type is used to store a single character, such as a letter, digit, or symbol. Character values are written inside single quotes, for example: 'A', '5', '$'.
Storage Size:
- 1 byte (8 bits).
Range:
- signed char: -128 to +127
- unsigned char: 0 to 255
Format Specifier:
- Use %c to print a character value.
Key Points:
- A
charvariable can store only one character. - It cannot store strings (multiple characters).
- Internally, characters are stored as ASCII values
Example:
char grade = 'A';
printf("%c", grade);
Derived Data Types in C
In C, the four main derived data types are: Array, Structure, Pointer, and Union.
1. Array
An array is a collection of elements of the same data type, stored in contiguous memory locations. The size of an array is fixed at the time of declaration.
Example:
char a[10]; // Character array
int b[10]; // Integer array
2. Pointer
A pointer is a variable that stores the memory address of another variable of a specific type. Pointers are essential for dynamic memory allocation and efficient array/function handling.
Syntax:
data_type *var_name;
While a normal variable stores a value, a pointer stores an address. The address-of operator (&) retrieves the address of a variable. A pointer that is not pointing to a valid address is often initialized to NULL (value 0). The size of a pointer depends on the system architecture (e.g., 4 bytes on a 32-bit system).
Example:
int *a; // Pointer to an integer
char *b; // Pointer to a character
3. Structure
A structure (struct) is a user-defined type that groups variables of different data types under a single name. Each member occupies its own memory space.
Syntax:
struct structure_name {
data_type member1;
data_type member2;
// ... more members
};
Why Use Structures? Structures organize related data. For example, instead of managing separate variables (name1, rollNo1, class1) for each student, you can define a single Student structure.
Example:
struct Student {
char name[50];
int rollNo;
int class;
};
4. Union
A union is similar to a structure in syntax, but all its members share the same memory location. Consequently, only one member can hold a value at any given time. Unions are memory-efficient when you need to store one of several possible data types.
Syntax:
union union_name {
data_type member1;
data_type member2;
// ... more members
};
Example:
union Student {
char name[50];
int rollNo;
int class;
};
Modifiers in C
Modifiers in C are keywords that change the size of memory allocated to a variable and therefore modify the range of values it can store. They are always used as a prefix to basic data types.
Modifiers allow programmers to control memory usage and value range according to program requirements.
Common Modifiers in C
shortlongsignedunsigned
a. Short and Long
The short and long modifiers change the size and range of integer types. short int typically uses 2 bytes, int uses 4 bytes, and long int uses 4 or 8 bytes, depending on the system.
Example:
short int a; // Typically 2 bytes
int b; // Typically 4 bytes
long int c; // Typically 4 or 8 bytes
You can verify sizes using the sizeof() operator.
#include <stdio.h>
int main() {
short int a;
int b;
long int c;
printf("size of short int = %lu bytes\n", sizeof(a));
printf("size of int = %lu bytes\n", sizeof(b));
printf("size of long int = %lu bytes\n", sizeof(c));
return 0;
}
Output (example):
size of short int = 2 bytes
size of int = 4 bytes
size of long int = 8 bytes
b. Signed and Unsigned
The signed and unsigned modifiers change whether an integer type can represent negative numbers.
- signed: Can store negative, zero, and positive values. This is the default for
charandint. - unsigned: Can store only zero and positive values, effectively doubling the positive range.
Example:
unsigned int a; // Can store 0 to 4,294,967,295 (assuming 4 bytes)
int b; // Can store -2,147,483,648 to 2,147,483,647
Enumeration Data Type (enum)
The enum (enumeration) type is a user-defined data type that consists of a set of named integer constants. It makes code more readable by using meaningful names instead of numbers.
Key Points of enum Data Type:
- By default, the first enumerator has the value 0, and each subsequent one increases by 1.
- You can assign specific integer values to enumerators.
Example:
enum Status { OK, WARNING, ERROR };
// OK = 0, WARNING = 1, ERROR = 2