What is Data Type in C ?
In C, a data type is a classification that specifies the type of data that a variable can hold. It also determines the size of the variable, the range of values that it can hold, and the operations that can be performed on it.
Why Data Types are used ?
Data Types are used to define the type of data that a variable can hold. This helps the compiler to allocate memory for the variable and to check for type compatibility during operations.
Data Types in C:
Given below are the different types of Data Types in C:
Data Type | Information | Byte Size | Value Range | Syntax | Example | Format Specifier |
---|---|---|---|---|---|---|
int | Represents a standard integer. | 4 bytes | -2,147,483,648 to 2,147,483,647 | int variableName = 100; | int age = 25; | %d |
char | Represents a single character. | 1 byte | -128 to 127 or 0 to 255 | char variableName = ‘a’; | char firstInitial = ‘J’; | %c |
short | Represents an integer with a smaller range than int. | 2 bytes | -32,768 to 32,767 | short variableName = 10; | short temperature = -20; | %d |
long | Represents an integer with a larger range than int. | 4 bytes | -2,147,483,648 to 2,147,483,647 | long variableName = 1000000L; | long population = 123456789L; | %ld |
float | Represents a floating-point number with single precision. | 4 bytes | 1.2E-38 to 3.4E+38 | float variableName = 3.14159f; | float pi = 3.14f; | %f |
double | Represents a floating-point number with double precision. | 8 bytes | 2.3E-308 to 1.7E+308 | double variableName = 3.14159; | double pi = 3.141592653589793; | %lf |
bool | Represents a boolean value of true or false. | 1 byte | true or false | bool variableName = true; | bool isCorrect = true; | %d or %i |
Code:
Lets see how we can declare all these variables:
#include <stdio.h>
int main() {
int isCorrect = 1;
char firstInitial = 'J';
short temperature = -20;
int age = 25;
long population = 123456789L;
float pi = 3.14f;
double e = 2.71828;
printf("bool: %d\n", isCorrect);
printf("char: %c\n", firstInitial);
printf("short: %hd\n", temperature);
printf("int: %d\n", age);
printf("long: %ld\n", population);
printf("float: %f\n", pi);
printf("double: %lf\n", e);
return 0;
}
Output:
bool: 1
char: J
short: -20
int: 25
long: 123456789
float: 3.140000
double: 2.718280
Signed and Unsigned Modifiers in C
Signed Modifier:
In C, the term “signed” is used as a modifier for integer data types to indicate that the data type can represent both positive and negative values. By default, integer data types are signed in C, which means that if you do not specify signed or unsigned, the compiler will assume that the data type is signed.
For example, a signed int
can represent values between -2,147,483,648 and 2,147,483,647. Similarly, a signed short
can represent values between -32,768 and 32,767.
Signed data types are often used for variables that represent quantities that can take on negative values, such as temperature, displacement, or velocity.
Unsigned Modifier:
In C, the term “unsigned” is used as a modifier for integer data types to indicate that the data type can only represent non-negative values. Using unsigned data types can be useful when you need to represent only positive values, and you want to increase the maximum value that can be stored.
For example, an unsigned int
can represent values between 0 and 4,294,967,295. Similarly, an unsigned short
can represent values between 0 and 65,535.
Unsigned data types are often used for variables that represent quantities that cannot take on negative values, such as lengths, counts, or indices.
Given below are the different types of Signed and Unsigned Data Types in C:
Data Type | Information | Byte Size | Value Range | Syntax | Example | Format Specifier |
---|---|---|---|---|---|---|
signed char | Signed character type | 1 | -128 to 127 | signed char | signed chat x = -5; | %c or %hhd |
unsigned char | Unsigned character type | 1 | 0 to 255 | unsigned char | unsigned char y = 200; | %c 0r %hhu |
signed short int | Signed short integer type | 2 | -32,768 to 32,767 | signed short int or signed short or short int or short | signed short int x = -100; | %hd |
unsigned short int | Unsigned short integer type | 2 | 0 to 65,535 | unsigned short int or unsigned short | unsigned short int y = 5000; | %hu |
signed int | Signed integer type | 4 | -2,147,483,648 to 2,147,483,647 | signed int or signed or int | signed int x = -1000; | %d |
unsigned int | Unsigned integer type | 4 | 0 to 4,294,967,295 | unsigned int or unsigned | unsigned int y = 40000; | %u |
signed long int | Signed long integer type | 4 or 8 | -2,147,483,648 to 2,147,483,647 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | signed long int or signed long or long int or long | signed long int x = -100000; | %ld |
unsigned long int | Unsigned long integer type | 4 or 8 | 0 to 4,294,967,295 or 0 to 18,446,744,073,709,551,615 | unsigned long int or unsigned long | unsigned long int y = 4000000000; | %lu |
signed long long int | Signed long long integer type | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | signed long long int or signed long long or long long int or long long | signed long long int x = -1000000000; | %lld |
unsigned long long int | Unsigned long long integer type | 8 | 0 to 18,446,744,073,709,551,615 | unsigned long long int or unsigned long long | unsigned long long int y = 10000000000; | %llu |
Again, lets see how we can declare all these variables:
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
int main() {
// Signed Types
bool isCorrect = true;
char firstInitial = 'J';
signed char temperature = -20;
short int age = -25;
signed int score = -100;
int32_t population = -123456789L; // int32_t is 32 bit signed int
long long int bankBalance = -123456789012345LL;
float pi = -3.14f;
double e = -2.71828;
// Unsigned Types
unsigned char asciiCode = 65;
unsigned short int numberOfStudents = 100;
unsigned int distance = 2500U;
uint32_t websiteHits = 1234567890UL; // uint32_t is 32 bit unsigned int
unsigned long long int largeNumber = 12345678901234567890ULL;
printf("bool: %d\n", isCorrect);
printf("char: %c\n", firstInitial);
printf("signed char: %hhd\n", temperature);
printf("short int: %hd\n", age);
printf("signed int: %d\n", score);
printf("int32_t: %ld\n", population);
printf("long long int: %lld\n", bankBalance);
printf("float: %f\n", pi);
printf("double: %lf\n", e);
printf("unsigned char: %hhu\n", asciiCode);
printf("unsigned short int: %hu\n", numberOfStudents);
printf("unsigned int: %u\n", distance);
printf("uint32_t: %lu\n", websiteHits);
printf("unsigned long long int: %llu\n", largeNumber);
return 0;
}
Output:
bool: 1
char: J
signed char: -20
short int: -25
signed int: -100
int32_t: -123456789
long long int: -123456789012345
float: -3.140000
double: -2.718280
unsigned char: 65
unsigned short int: 100
unsigned int: 2500
uint32_t: 1234567890
unsigned long long int: 12345678901234567890
Rules while declaring/using Data Types:
- Choose the appropriate data type for the specific data you are working with. For example, use a
bool
data type for true/false values,char
for single characters,int
for integers,float
for decimal numbers with single precision,double
for decimal numbers with double precision, and so on. - Make sure the data type you choose can accommodate the expected range of values for the variable. For example, if you need to store large numbers, you may need to use a
long
orlong long
data type instead of anint
. - Use the
sizeof
operator to determine the size of a data type in bytes. This can help you ensure that you have allocated enough memory for a variable. - Be aware of the limitations of signed and unsigned data types, and use them appropriately. For example, an unsigned data type cannot store negative values, so be careful when using them for calculations that may result in negative values.
- Use the appropriate format specifier when printing variables to the console or writing to a file. This will ensure that the output is formatted correctly and can be read by the intended audience.
- Avoid using variables with the same name as built-in functions or keywords in C. This can cause conflicts and unexpected behavior in your code.
- Declare variables before using them, and initialize them to a default value if possible. This will prevent errors and help ensure that your program behaves predictably.
- Use
const
to declare variables that should not be modified during program execution. This can help prevent accidental changes to important values. - Avoid using global variables whenever possible. They can make it difficult to reason about program behavior and can lead to unexpected interactions between different parts of your code.