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.
Types | Data Types |
---|---|
Basic Data Type | int, char, float, double |
Derived Data Type | array, pointer, structure, union |
Enumeration Data Type | enum |
Void Data Type | void |
Let’s consider a scenario of a company. A company stores various data of their employee such as Name, Employee ID, Age, Salary, Address, Phone No, etc.
Now, these data are values containing alphabets, numbers, etc, so to make the processing of these huge data for programs easy, the information was categorized into different types:
#include
int main()
{
/* declare all data types */
char charVariable;
unsigned char uCharVariable;
short shortVariable;
unsigned short uShortVariable;
int intVariable;
unsigned int uIntVariable;
long longVariable;
unsigned long uLongVariable;
long long longLongVariable;
unsigned long long uLongLongVariable;
float floatVariable;
double doubleVariable;
long double longDoubleVariable;
/*read input*/
printf("Enter a character: ");
charVariable = getchar();
getchar();
printf("Enter another character: ");
uCharVariable = getchar();
getchar();
printf("Enter a signed short value: ");
scanf("%hi", &shortVariable);
printf("Enter an unsigned short value: ");
scanf("%hu", &uShortVariable);
printf("Enter an signed integer value: ");
scanf("%d", &intVariable);
printf("Enter an unsigned integer value: ");
scanf("%d", &uIntVariable);
printf("Enter a signed long value: ");
scanf("%ld", &longVariable);
printf("Enter an unsigned long value: ");
scanf("%lu", &uLongVariable);
printf("Enter a signed long long value: ");
scanf("%lld", &longLongVariable);
printf("Enter an unsigned long long value: ");
scanf("%llu", &uLongLongVariable);
printf("Enter a float value: ");
scanf("%f", &floatVariable);
printf("Enter a double value: ");
scanf("%lf", &doubleVariable);
printf("Enter a long double value: ");
scanf("%Lf", &longDoubleVariable);
/* print the value of all variable */
printf("\nYou entered character: '%c' \n", charVariable);
printf("You entered unsigned character: '%c' \n\n", uCharVariable);
printf("You entered signed short: %hi \n", shortVariable);
printf("You entered unsigned short: %hu \n\n", uShortVariable);
printf("You entered signed int: %d \n", intVariable);
printf("You entered unsigned int: %d \n\n", uIntVariable);
printf("You entered signed long: %ld \n", longVariable);
printf("You entered unsigned long: %lu \n\n", uLongVariable);
printf("You entered signed long long: %lld \n", longLongVariable);
printf("You entered unsigned long long: %llu \n\n", uLongLongVariable);
printf("You entered float: %f \n", floatVariable);
printf("You entered double: %lf \n", doubleVariable);
printf("You entered long double: %Lf \n", longDoubleVariable);
return 0;
}
The size of each data type is defined in bits or bytes (8 bits). Each data type in C is associated with a specific range of values defined as below:
Data Type | Format Specifier | Minimal Range | Size in bit |
unsigned char | %c | 0 to 255 | 8 |
char | %c | -127 to 127 | 8 |
signed char | %c | -127 to 127 | 8 |
int | %d, %i | -32,767 to 32,767 | 16 or 32 |
unsigned int | %u | 0 to 65,535 | 16 or 32 |
signed int | %d, %i | -32,767 to 32,767 (same as int) | 16 or 32 |
short int | %hd | -32,767 to 32,767 | 16 |
unsigned short int | %hu | 0 to 65,535 | 16 |
signed short int | %hd | Same as short int | 16 |
long int | %ld, %li | -2,147,483,647 to 2,147,483,647 | 32 |
long long int | %lld, %lli | -(2^63) to (2^63)-1 | 64 |
signed long int | %ld, %li | Same as long int | 32 |
unsigned long int | %lu | 0 to 4,294,967,295 | 32 |
unsigned longlong int | %llu | (2^63)-1 | 64 |
float | %f | 1E-37 to 1E+37 along with six digits of the precisions | 32 |
double | %lf | 1E-37 to 1E+37 along with six digits of the precisions | 64 |
long double | %Lf | 1E-37 to 1E+37 along with six digits of the precisions | 80 |
Derived data types are primary data types that are grouped together. You can group many elements of similar data types. These data types are defined by the user. The following are the derived data types in C:
An array in C is a collection of multiple values of a similar data type and is stored in a contiguous memory location. An array can consist of chars, integers, doubles, etc.
Declaration of Array in C
data_type array_name[array_size];
#include
int main()
{
int i=0;
int marks[5];//declaration of array
marks[0]=50;//initialization of array
marks[1]=60;
marks[2]=75;
marks[3]=40;
marks[4]=85; //traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
return 0;
}
The pointer data type is used to store the address of another variable. A pointer can store the address of variables of any data type. Pointers allow users to perform dynamic memory allocation. They also help to pass variables by reference.
A pointer with no address is called a null pointer. A pointer with no data type is a void Pointer. It is defined by using a ‘*’ operator.
int main(void)
{
int *ptr1;
int *ptr2;
int a = 5;
int b = 10;
//address of a is assigned to ptr1
ptr1 = &a;
//address of b is assigned to ptr2
ptr2 = &b;
//display value of a and b
printf("%d", *ptr1); //prints 5
printf("\n%d", *ptr2); //prints 10
//print address of a and b
printf("\n%d", ptr1); // prints address
printf("\n%d", ptr2); // prints address
//pointer subtraction
int minus = ptr2 - ptr1;
printf("\n%d", minus); //prints the difference
return 0;
}
It is a data type that can store variables of similar or different data types. For example, we can use structures to store information about an employee, such as the employee’s name, employee ID, salary, and more. Each employee’s record will be represented by an object of the structure. The size of the structure is the sum of the storage size required by each variable. The ‘struct’ keyword defines a structure.
#include
#include
struct Employee
{
char name[50];
int emp_id;
float salary;
} employee1;
int main()
{
strcpy(employee1.name, "John");
employee1.emp_id = 1779;
employee1. salary = 3900;
printf("Name: %s\n", employee1.name);
printf("Employee ID: %d\n", employee1.emp_id);
printf("Salary: %.2f", employee1.salary);
return 0;
}
A union is a group of elements with similar or different data types. In a union, the memory location is the same for all the elements. Its size will be equal to the memory required for the largest data type defined. We use the keyword ‘union’ to define a union. You can declare many variables. However, just one variable can store the value at a time.
union Student
{
int id;
char name[20];
float marks[5];
}
st1, st2;
Enumerated data types are user-defined data types that consist of integer values. They are used to define variables that can only assign certain discrete integer values in the program. They are used to make a program more readable, flexible, and maintainable. We use the keyword ‘enum’ to declare new enumeration types in the C programming language.
Enum syntax:
enum flag {const1, const2, const3………};
#include
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Fri;
printf("%d",day);
return 0;
}
The void is just an empty data type that depicts that no value is available. Typically, the void is used for functions. When we declare a function as void, it doesn’t have to return anything.
Void is used in three situations:
Example – The below function will not return any value to the calling function.
void sum (int a, int b);
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.