The data type specifies how to enter data into the program and the type of data to enter. The C language has several predefined sets of data types to handle the different data types available to your program. These types of data have different storage capacities. Each type of data requires different amounts of memory and there are some specific operations that can be performed on it.
Types of Data Types in C Language:
There are two type of data types in C Language:
1. Primary data types:
These are the basic data types of C: integers (ints), floats (floats), characters (characters), and voids.
2. Derived data types:
Derived data types are none other than primary data types, but are slightly twisted or grouped as arrays, structures, unions, and pointers.
Let me explain them all with examples.
1. Integer data type (int):
If you need to store an integer, you can use int as the data type. You can set a range of numbers based on the size you choose in memory, and you can specify all positives or from. Numeric values in the negative to positive range, depending on the user’s code design choice.
For example:
Here Code:
#include <stdio.h>
int main()
{
int a = 1;
printf(" %d is the integer value ",a);
unsigned short int x = -3278989;
printf(" %hu is the integer value ",x);
}
2. Floating data type:
Any real number can be stored in the floating data type. Again, you can specify a range based on your data type and size selection, allowing a range of numbers.
For example:
Here Code:
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main() {
printf("max float value allowed in positive range : %g\n", (float) FLT_MAX);
printf("max float value allowed in negative range : %g\n", (float) -FLT_MAX);
printf("max double value possible in positive range : %g\n", (double) DBL_MAX);
printf("max double value possible in negative range : %g\n", (double) -DBL_MAX);
}
3. Char type:
Represents a character data type, which is signed or unsigned with a fixed size of 1 byte in both cases.
For example:
Here Code:
#include <stdio.h>
int main() {
char c ='a';
char f = 65; //ASCII char value
printf("%c %c ", c, f);
}
4. Void type:
If you don’t want to assign a type to a function (i.e. if the above snippet doesn’t return anything as you saw the main function with the type prefix void), you can mark it as type void.
For example:
Here Code:
#include <stdio.h>
void main() {
char c ='a';
char f = 65; //ASCII char value
printf("%c %c ", c, f);
}
5. Arrays:
This data type is selected when it is necessary to store similar data sets in contiguous memory locations. In use cases, the code may return multiple results and the function may need to return cumulatively. If you need to find a list of all the months of the year, it will be 12. Therefore, you cannot put 12 months individually in a variable, so use an array for the same variable.
For example:
Here Code:
#include <stdio.h>
int main() {
int i;
char arr[] = {'a', 'b', 'c'};
for(i = 0 ; i < 3 ; i++)
{
printf("%c\n",arr[i]);
}
}
6. Structures:
This type is useful if you have a requirement that the physical world structure be represented in the encoding world. For example, you can define a student class as a structure and use the student’s brand and the student’s role number as variables in it. For many students, we can present arrays that can contain data related to these structures.
For example:
Here Code:
#include <stdio.h>
struct class{
int marks;
int rollNo;};
void main() {
struct class c;
c.marks=10;
c.rollNo=1;
printf("%d\n", c.marks);
printf("%d", c.rollNo);
}
7. Pointer:
This is one of the most important data types because I am not interested in the world of COOP. It is not used in languages like Java, but it is always used in functional programming languages. The concept of pointers is to allocate memory to a variable and then refer to that memory location for read and write operations. Its location in memory is the address of a function, the address of a variable, etc. It also provides C structure and array processing and dynamic memory management.
For example:
Here Code:
#include <stdio.h>
int main() {
int a, *p; // variable and pointer declaration
a = 10;
p = &a;
printf("%d", *p);// print the value of 'a'
printf("%u", &a);//print the address of 'a'
printf("%u", p);// print the address of 'a' in different way
// remember & represents address of variable
}
8. Union:
Union is a c user-defined data type that allows different data items of different data types to be stored in the same memory location. Provides an efficient way to use memory, as only one union member can be accessed at any given time. Therefore, the size of the junction is equal to the size of its largest element at any given time.
For example:
Here Code:
#include <stdio.h>
#include <string.h>
union test {
int tint;
float tf;
char tstr[20];
};
int main( ) {
union test t;
t.tint = 100;
printf( "record.i : %d\n", t.tint);
t.tf= 200.5;
printf( "record.f : %f\n", t.tf);
strcpy( t.tstr, "Test");
printf( "record.str : %s\n", t.tstr);
return 0;
}
Conclusion:
So we looked at the different C data types and how they work with the C language to handle coding scenarios. The same utility has also been developed, so you can also perform C embedded programming. Therefore, C is a versatile language, but in real world scenarios, the coding becomes more complex and more complex. All data types serve their own way, which makes C a robust language.
References:
- https://www.studytonight.com/c/datatype-in-c.php/
- https://www.programiz.com/c-programming/c-data-types
- https://www.geeksforgeeks.org/data-types-in-c/
- https://www.tutorialspoint.com/cprogramming/c_data_types.htm
- https://www.javatpoint.com/data-types-in-c
- https://fresh2refresh.com/c-programming/c-data-types/