Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.
There are the following data types in C language.
Basic Data Types
The basic data types are integer-based and floating-point based. C language supports both signed and unsigned literals.
The memory size of the basic data types may change according to 32 or 64-bit operating system.
Let's see the basic data types. Its size is given according to 32-bit architecture.
Integers are entire numbers without any fractional or decimal parts, and the int data type is used to represent them.
It is frequently applied to variables that include values, such as counts, indices, or other numerical numbers. The int data type may represent both positive and negative numbers because it is signed by default.
An int takes up 4 bytes of memory on most devices, allowing it to store values between around -2 billion and +2 billion.
Individual characters are represented by the char data type. Typically used to hold ASCII or UTF-8 encoding scheme characters, such as letters, numbers, symbols, or commas. There are 256 characters that can be represented by a single char, which takes up one byte of memory. Characters such as 'A', 'b', '5', or '$' are enclosed in single quotes.
To represent integers, use the floating data type. Floating numbers can be used to represent fractional units or numbers with decimal places.
The float type is usually used for variables that require very good precision but may not be very precise. It can store values with an accuracy of about 6 decimal places and a range of about 3.4 x 1038 in 4 bytes of memory.
Use two data types to represent two floating integers. When additional precision is needed, such as in scientific calculations or financial applications, it provides greater accuracy compared to float.
Double type, which uses 8 bytes of memory and has an accuracy of about 15 decimal places, yields larger values. C treats floating point numbers as doubles by default if no explicit type is supplied.
In the example above, we declare four variables: an int variable for the person's age, a char variable for the student's grade, a float variable for the temperature reading, and two variables for the number pi.
Derived Data Type
Beyond the fundamental data types, C also supports derived data types, including arrays, pointers, structures, and unions. These data types give programmers the ability to handle heterogeneous data, directly modify memory, and build complicated data structures.
An array, a derived data type, lets you store a sequence of fixed-size elements of the same type. It provides a mechanism for joining multiple targets of the same data under the same name.
The index is used to access the elements of the array, with a 0 index for the first entry. The size of the array is fixed at declaration time and cannot be changed during program execution. The array components are placed in adjacent memory regions.
Here is an example of declaring and utilizing an array:
Values in the array: 10 20 30 40 50
A pointer is a derived data type that keeps track of another data type's memory address. When a pointer is declared, the data type it refers to is stated first, and then the variable name is preceded by an asterisk (*).
You can have incorrect access and change the value of variable using pointers by specifying the memory address of the variable. Pointers are commonly used in tasks such as function pointers, data structures, and dynamic memory allocation.
Here is an example of declaring and employing a pointer:
Value of num: 42
A structure is a derived data type that enables the creation of composite data types by allowing the grouping of many data types under a single name. It gives you the ability to create your own unique data structures by fusing together variables of various sorts.
A declaration and use of a structure is demonstrated here:
Name: John Doe Age: 30 Height: 1.80
A derived data type called a union enables you to store various data types in the same memory address. In contrast to structures, where each member has a separate memory space, members of a union all share a single memory space. A value can only be held by one member of a union at any given moment. When you need to represent many data types interchangeably, unions come in handy. Like structures, you can access the members of a union by using the dot (.) operator.
Here is an example of a union being declared and used:
Integer Value: 42 Float Value: 3.14
Enumeration Data Type
A set of named constants or enumerators that represent a collection of connected values can be defined in C using the enumeration data type (enum). Enumerations give you the means to give names that make sense to a group of integral values, which makes your code easier to read and maintain.
Here is an example of how to define and use an enumeration in C:
Today is 2
Void Data Type
The void data type in the C language is used to denote the lack of a particular type. Function return types, function parameters, and pointers are three situations where it is frequently utilized.
Function Return Type:
A void return type function does not produce a value. A void function executes a task or action and ends rather than returning a value.
The parameter void can be used to indicate that a function accepts no arguments.
Any address can be stored in a pointer of type void*, making it a universal pointer. It offers a method for working with pointers to ambiguous or atypical types.
The void data type is helpful for defining functions that don't accept any arguments when working with generic pointers or when you wish to signal that a function doesn't return a value. It is significant to note that while void* can be used to build generic pointers, void itself cannot be declared as a variable type.
Here is a sample of code that shows how to utilize void in various situations:
Hello, world! Processing input... Value of number: 10
As a result, data types are essential in the C programming language because they define the kinds of information that variables can hold. They provide the data's size and format, enabling the compiler to allot memory and carry out the necessary actions. Data types supported by C include void, enumeration, derived, and basic types. In addition to floating-point types like float and double, basic data types in C also include integer-based kinds like int, char, and short. These forms can be signed or unsigned, and they fluctuate in size and range. To create dependable and efficient code, it is crucial to comprehend the memory size and scope of these types.
A few examples of derived data types are unions, pointers, structures, and arrays. Multiple elements of the same kind can be stored together in contiguous memory due to arrays. Pointers keep track of memory addresses, allowing for fast data structure operations and dynamic memory allocation. While unions allow numerous variables to share the same memory space, structures group relevant variables together.
Code becomes more legible and maintainable when named constants are defined using enumeration data types. Enumerations give named constants integer values to enable the meaningful representation of related data. The void data type indicates the lack of a particular type. It is used as a return type for both functions and function parameters that don't take any arguments and don't return a value. The void* pointer also functions as a general pointer that can store addresses of various types.
C programming requires a solid understanding of data types. Programmers can ensure adequate memory allocation, avoid data overflow or truncation, and enhance the readability and maintainability of their code by selecting the right data type. C programmers may create effective, dependable, and well-structured code that satisfies the requirements of their applications by having a firm understanding of data types.