Understanding Data Types and Modifiers in C
In this lesson, we will learn the basics of data types and modifiers in C programming and how they are used during variable declaration to restrict the type and length of data stored in computer memory.
What is Data Type
In the C programming language, data types specify the type of data that a variable can hold. Each data type requires a different amount of memory and has a specific range of values.
Understanding data types is crucial because it helps you choose the appropriate type for storing and manipulating data. C supports the following data types given below.
- The short name for Integer data type is int.
- The short name for Float data type is float.
- The short name for Double data type is double.
- The short name for Character data type is char.
Basic Data Types in C
Let's go through the basic data types in C with simple explanations and examples.
int (Integer)
- Description: Store whole numbers without decimals.
- Memory Size: 4 bytes.
- Range: -2,147,483,648 to 2,147,483,647 (on most systems).
- Format Specifier: %d.
int guest_count = 20;
float (Float)
- Description: Stores numbers with decimals (upto 6 decimal places).
- Memory Size: 4 bytes.
- Range: −3.4E+38 to 3.4E+38 (meaning -3.4 × 1038 to 3.4 × 1038).
- Format Specifier: %f.
float budget = 5000.00;
double (Double)
- Description: Stores numbers with decimals (upto 15 decimal places).
- Memory Size: 8 bytes.
- Range: -1.7E+308 to 1.7E+308 (meaning -1.7 × 10308 to 1.7 × 10308).
- Format Specifier: %lf.
double distance = 1263.2514376;
char (Character)
- Description: Stores a single character within single quotes.
- Memory Size: 1 byte.
- Range: -128 to 127 or 0 to 255 (depends on the system).
- Format Specifier: %c.
char grade = 'A';
Understanding Data Type Modifiers in C
In C programming, data type modifiers are keywords that you can use to change the properties of basic data types. They help you manage the range and size of data more efficiently. The common data type modifiers in C are:
- signed - It is default modifier of int and char data type if no modifier is specified. It says that user can store negative or positive values.
- unsigned - It is used on int and char data type. It says that user can store only positive values.
- short - It limits user to store small integer values and occupies 2 bytes of memory space in every operating system. It can be used only with integer data type.
- long - This can be used to increased size of the int or double data types to 2 more bytes.
Storage Size and Range of Built-in Data Types in C (on 64-bit System)
| Data Type | Storage Size | Range | Format Specifier |
| short int | 2 bytes | -32,768 to 32,767 | %hd |
| unsigned short int | 2 bytes | 0 to 65,535 | %hu |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 | %d |
| unsigned int | 4 bytes | 0 to 4,294,967,295 | %u |
| long int | 8 bytes (Linux) / 4 bytes (Windows) | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (on 64-bit Linux) |
%ld |
| unsigned long int | 8 bytes (Linux) / 4 bytes (Windows) | 0 to 18,446,744,073,709,551,615 (on 64-bit Linux) | %lu |
| long long int | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
%lld |
| unsigned long long int | 8 bytes | 0 to 18,446,744,073,709,551,615 | %llu |
| char | 1 byte | -128 to 127 | %c |
| unsigned char | 1 byte | 0 to 255 | %c |
| float | 4 bytes | −3.4E+38 to 3.4E+38 (upto 6 decimal places) | %f |
| double | 8 bytes | -1.7E+308 to 1.7E+308 (upto 15 decimal places) | %lf |
| long double | 8 bytes (Windows) / 12-16 bytes (Linux, some GCC compilers) | −1.1E+4932 to 1.1E+4932 (19 decimal places, compiler-dependent) | %Lf |
long int and unsigned long int may be 4 bytes on Windows (even on 64-bit systems).
long double is typically 8 bytes on Windows but can be 12 or 16 bytes on Linux (GCC, Clang).
The actual range and precision of long double may vary depending on the compiler and system.
long long int is always 8 bytes, so it's better for guaranteed large values.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.