Data Types in C – A Comprehensive Guide
According to a study, C is the only language that has existed for the most prolonged period in computer programming history.
In C, one of the most potent and enduring programming languages, data types are the building blocks of any program. Defining the correct data types for your needs allows your program to run efficiently and handle data effectively. Without data types, C would be a jumble of commands with no ability to store or manipulate information. This blog will explore the various data types in C language. By the end, you’ll have a firm grasp of C’s data types and how to use them.
What is Data Type in C?
Every programming language revolves around data, and C is no exception. To work with data effectively, it’s essential to understand data types. Data type in C language defines the data’s size, range, and format that can be stored in a variable. They are fundamental building blocks in C and are crucial in memory allocation, variable manipulation, and overall program execution.
A data type in C is a classification that specifies the type of value a variable can hold. It dictates the size of the memory allocated for the variable and the operations that can be performed on it. For instance, an integer data type allows you to store whole numbers, while a floating-point data type lets you store numbers with decimal points.
Built-in Data Types
C language has a variety of built-in data types, which can be broadly classified into the following categories:
- Basic data types
- Derived data types
Let’s look at each category in detail.
Basic Data Types
C offers some basic data types, further sub-classified based on their size and range. The primary data types are:
1. int
The int data type is used to store integers. It occupies 2 bytes of memory and stores whole numbers between -32,768 to 32,767. For example-
int age = 30;
2. short int
The short int data type also stores integers but occupies 2 bytes of memory. It can keep numbers from -32,768 to 32,767. For example-
short int age = 30;
3. long int
The long int data type is used to store larger integers. It occupies 4 bytes of memory and can keep numbers from -2,147,483,648 to 2,147,483,647. For example-
long int age = 50000;
4. char
The char data type is used to store characters. It occupies 1 byte of memory and can store letters, numbers, and symbols. For example-
char ch = 'A';
5. float
The float data type is used to store real numbers with single precision. It occupies 4 bytes of memory and can store numbers from -3.4e38 to 3.4e38. For example-
float salary = 10000.99;
6. double
The double data type is used to store real numbers with double precision. It occupies 8 bytes of memory and can store numbers from -1.7e308 to 1.7e308. For example-
double sales = 50000.995;
7. void
The void data type specifies that a function or pointer does not have a return value. It is used in functions and pointers. For example:
void hello()
{
printf("Hello");
}
Derived Data Types
Derived data types are based on the basic data types and are used for more complex operations. The derived data types in C include:
1. Arrays
Arrays are a key concept in C and are used extensively in programs. An array is a collection of elements of the same data type. Arrays can be fixed size or dynamic size.
A fixed-size array can be defined as:
data_type array_name[size];
For example, an integer array of size five can be defined as int nums[5];
The elements of the array are accessed using indices starting from 0. For example:
nums[0] = 10;
nums[1] = 20;
Dynamic arrays can be created using pointers. For example:
int *ptr = (int *) malloc(size * sizeof(int));
Here, malloc allocates size * sizeof(int) bytes of memory and assigns the address to ptr.
2. Pointers
A pointer is a variable that stores the address of another variable. Pointers allocate memory dynamically, referring to arrays, structures, functions, etc.
The general syntax of a pointer is:
data_type *pointer_name;
For example, an integer pointer can be defined as int *ptr;
This defines ptr as a pointer to an integer.
The & operator is used to get the address of a variable. For example:
int num = 10;
ptr = #
Now, ptr holds the address of the integer num.
The * operator is used to dereference a pointer and access the value at the address pointed to by it. For example:
*ptr = 20; // Changes the value of num to 20
So, pointers are a fundamental concept in C, enabling us to handle memory efficiently.
3. Structures
A structure creates a new data type that can hold heterogeneous elements. It defines a template that can be used to create variables with diverse data types.
A structure can be defined using the “struct” keyword,
struct structure_name {
data_type member1;
data_type member2;
...
};
Example:
struct student {
char st_name[50];
int st_age;
float st_percentage;
};
We can then create variables of this structure type as:
struct student s1;
s1.st_age = 20;
s1.st_percentage = 80.5;
strcpy(s1.st_name, "John");
We can also have structure types with a tag as:
struct student {
// definition
} s2;
4. Unions
A union is a particular type of data structure in C. It is defined using the union keyword. It allows for storing different kinds of values at the exact memory location. It represents a new data type that can hold diverse elements. A union can have only one value at a time, which is overwritten when another value is stored.
union union_name {
data_type member_m1;
data_type member_m2;
...
};
Example:
union student {
char st_name[50];
int st_age;
float st_percentage;
};
We can then have union variables as:
union student u1;
u1.st_age = 20; // Stores the integer 20
u1.st_percentage = 80.5; // Overwrites the age value
User-Defined Data Types in C
Besides built-in data types, the C language allows you to create your own data types based on your requirements. The main advantage of user-defined data types is that they make the code more readable, maintainable, and reusable. C has much more to offer, and the right C course can help you figure it out.
There are three types of user-defined data types in C:
- Structure
- Union
- Enumerated data types
Enumerated Data Types
Enumerated types are defined using the enum keyword. An enumerated type declares a set of zero or more identifiers that may be used as that type’s values and an optional type name.
Example:
enum color {red, green, blue};
Here, red, green, and blue are the possible values for the enumerated type color. We can also assign integer values to enum identifiers if we want. For example:
enum day {Mon=1, Tue, Wed, Thu, Fri, Sat, Sun};
Here, mon is assigned 1, Tue is 2, and so on. If no values are given, mon will be 0, Tue will be 1, and so on.
We can use the enum type to declare variables as:
enum color c;
c = red;
And we can have enum types with a tag as:
enum colors {red, green, blue} c;
c = blue;
Defining Enumerated Data Types
To define an enumerated data type in C, use the enum keyword, followed by the name of the enum and a list of constant names enclosed in curly braces. For example:
enum Weekdays {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
Type Qualifiers
Type qualifiers in C language modify the properties of a data type. There are three types of qualifiers in C:
- const- It indicates that the value of the variable cannot be changed.
- volatile- It informs the compiler that the value of the variable may change unexpectedly.
- restrict: It indicates that the pointer is the only means to access the object it points to.
Type Casting
Type casting is a technique that allows you to convert a variable from one data type to another. It’s often used to perform operations between different data types or to avoid potential data loss during arithmetic operations.
In C, you can perform typecasting using the following syntax:
(data_type)expression
For example, to convert a floating-point number to an integer, you can use type casting like this:
float number = 5.75;
int result;
result = (int)number;
Conclusion
Data types in C language are crucial for efficiently handling data and memory allocation. We’ve covered the basics of built-in data types, user-defined data types, enumerated data types, type qualifiers, and type casting. Understanding and mastering the above-discussed concepts will help you write more efficient and readable code in C.