Home

 › 

Articles

 › 

Data Types in C: Fully Explained

Dynamic memory allocation

Data Types in C: Fully Explained

The C language is a very popular language that has been around for about five decades. You’ll find it being used as the preferred choice for systems programming or embedded systems. It is also a favorite for those who want to develop applications that need low-level hardware access. Data types are some of the main concepts in this programming language. They are for specifying the type of data to be used in a program. We need to understand the data types in C in order to be better programmers who are more effective. This would ensure that a program functions as it is intended to without any errors.  We will discuss the different data types in C and go through their features and best use cases. This will help you to understand the different data types in C so that you can build better programs. 

Basic Data Types in C

Some of the basic data types in C are the integer data type, the character data type, and the floating point data type.

Integer Data Type

The integer data type in C stores whole numbers. It includes short, long, and unsigned integer types that have different sizes and ranges. The “int” data type is the default integer type in C and has a size of 2 or 4 bytes depending on the system. The “short int” data type has a smaller size than the “int” data type. The “long int” data type has a larger size.

Unsigned integer types can only store non-negative values. They also have a larger positive range than the signed integer types. The range and size of integer types depend on the system architecture. It is vital that you choose the correct integer type based on the size and range of values to be stored.

Character Data Type

The character data type in C stores single characters such as letters, digits, and symbols. We use the ASCII code system to represent characters in the C language. The “char” data type in C stores a single character the size of one byte. The ASCII system assigns a unique code to each character. C provides various functions to manipulate character data types.

yottabyte
In the “char” data type, the variable can store just one character in the size of one byte.

Floating-Point Data Type

The floating-point data type in C stores decimal numbers. This includes the “float” and “double” types that do not have the same range or size. The “float” type can store decimal places that are up to 6. The “double” type can store up to the fifteenth decimal place. You should note that the “double” type has a larger range and size than the “float” type.

You must select the right floating-point type based on the required precision and range of values. Sometimes though, floating-point operations are not always precise and we can end up getting errors. This is why we need to be careful when using floating-point data types in C.

Data Types in C(Derived)

We’ve covered the basic types of data types in C but these are not all that there is. Data types that are derived are created from data types we discussed earlier which are basic. Some of these derived data types are structures, arrays, and unions.

Arrays

Think of an array as a group of values sharing a common data type. In the  C programming language, we declare and initialize arrays using brackets [ ]. There are different types of arrays with two of them being one-dimensional and multidimensional arrays. We use one-dimensional arrays store a list of elements. We use multidimensional arrays on the other hand store data as tables or matrices.

Structures

A structure is a collection of elements of different data types. Structures in the C programming language are declared using the keyword “struct” followed by the structure name and its elements. Structures can be initialized by giving values to their elements. We use structures to store data that can be logically grouped together. Some examples are employee records or student information. Structures can be accessed using the dot (.) operator and can be passed as arguments to functions.

Unions

Unions are similar to structures in that they are also a collection of elements of different data types. The main difference is that unions can only hold one value at a time. This means that changing the value of one element in a union will affect the value of all other elements. Unions are declared using the keyword “union” followed by the union name and its elements. We use unions to save memory space when only one of the elements in the collection needs to be accessed at a time.

Enumerated Data Types

We use the enumerated data type in C to establish a collection of designated integer constants. These are also known as enumeration constants. These constants are represented by identifiers and have an associated integer value. Enumerated data types are ideal for a limited set of related values. This can be days of the week or the colors of the rainbow. If they have some kind of relationship they can be enumerated data values.

To declare an enumerated data type in C, the “enum” keyword is used followed by the name of the enumeration and a list where named constants are enclosed in braces. Here is an example:

enum days_of_week {

   Monday,

   Tuesday,

   Wednesday,

   Thursday,

   Friday,

   Saturday,

   Sunday

};

In the example above, we’ve defined an enumeration called “days_of_week” with seven named constants representing the days of the week.

Enumerated data types in C can be used in many ways. We can use them in declaring variables. They can even be passed as arguments to functions and even in switch statements. They can also be combined with other data types, such as integers and characters.

We should always remember that the values of the named constants in an enumeration start at zero by default. They can however be assigned specific integer values if we want. It’s also possible to assign values to some named constants and not others. In such cases, the subsequent constants will be assigned values one greater than the previous constant.

Type Modifiers

In C programming, type modifiers are used to modify the properties of data types. The five commonly used type modifiers in C are “signed,” “unsigned,” “short,” “long,” and “const.” 

Signed and Unsigned

The “signed” and “unsigned” type modifiers are used with integer data types to specify whether the value can be negative or not. Integer data types in C are “signed” by default. This means they can store both positive and negative values. If you use the “unsigned” modifier with an integer data type then it will only store non-negative values. This is because the highest bit in the data type stores the sign of the value. Consequently, when you make it “unsigned,” that bit stores a value instead.

Short and Long

The “short” and “long” type modifiers are used with integer data types to specify the size of the data type. The size of an integer data type in C is determined by the compiler by default. You can still use these modifiers to explicitly specify the size. A “short” integer is smaller than the default size of an integer, while a “long” integer is larger.

Const

The “const” type modifier in the C language is used to create constants. If you declare a variable as “const,” then its value cannot be changed throughout the rest of the program. This is helpful when you want to create a variable that represents a constant value that should not be modified. A good example is Pi(3.14) or the number of days in a week. 

It is crucial to note that when you use the “const” modifier with a pointer, it means that the pointer is a constant pointer. This means the address it points to cannot be changed. The value stored at that address can however still be changed.

web developer programming software engineer PC computer monitor
In the C programming language, type modifiers are used to modify the properties of data types.

Type Casting

Type casting in C is the process of converting one data type into another. We need to use it in situations where a function expects an argument of a certain data type but is instead passed a value of a different data type. Type casting allows the value to be converted to the required data type so that the function can be executed.

To perform type casting in C, the data type to be casted is enclosed in parentheses, and placed before the value or variable to be casted. To cast an integer value to a floating-point value we would have to code like this:

int num = 5;

float floatNum = (float) num;

Type casting can be risky if the value being casted cannot be represented in the target data type. If we tried to cast a large integer value to a smaller integer data type, then this would result in an error. It is vital to ensure that the value being casted is within the range of the target data type to avoid any issues.

Final Thoughts

We have gone through the basic, derived, and enumerated data types in C. This is in addition to type modifiers and type casting. We should understand the data types in C so that we build better programs and also avoid common errors such as overflow or underflow. If we know the range and limitations of each data type, then it makes it easier for us to make informed decisions and write efficient code.

Frequently Asked Questions

What are data types in C, and why are they important for programming?

Data types in C refer to the classification of data used in programming, which specifies the type of data and how it can be manipulated. They are important for programming because they ensure correct data manipulation and improve program performance.

What is the difference between basic data types and data types that are derived in C?

Basic data types are predefined by the C language, while derived data types are created by combining basic data types.

How do type modifiers in C affect the properties of data types?

Type modifiers in C affect the properties of data types, such as size and range, by modifying their default behavior.

What is type casting, and why is it necessary in C programming?

Type casting in C is a process of converting one data type to another. It is necessary in C programming to avoid data loss during computations or to convert data to a suitable format

How do enumerated data types work in C, and what are some examples of their usage?

Enumerated data types in C define a set of named integer constants. They are useful for creating variables that can only take a limited number of possible values, such as days of the week or months of the year.

To top