Data Types in C - GeeksforGeeks
Data Types in C - GeeksforGeeks
Data Types in C
Each variable in C has an associated data type. Each data type requires different
amounts of memory and has some specific operations which can be performed over it.
It specifies the type of data that the variable can store like integer, character, floating,
double, etc. The data type is a collection of data with values having fixed values,
meaning as well as its characteristics.
Types Description
Primitive Arithmetic types can be further classified into integer and floating
Data Types data types.
The data type has no value or operator and it does not provide a
Void Types
result to its caller. But void comes under Primitive data types.
User
It is mainly used to assign names to integral constants, which make a
Defined
program easy to read and maintain
DataTypes
Derived The data types that are derived from the primitive or built-in
types datatypes are referred to as Derived Data Types.
Different data types also have different ranges up to which they can store numbers.
These ranges may vary from compiler to compiler. Below is a list of ranges along with
the memory requirement and format specifiers on the 32-bit GCC compiler.
unsigned short
2 0 to 65,535 %hu
int
-2,147,483,648 to
int 4 %d
2,147,483,647
-2,147,483,648 to
long int 4 %ld
2,147,483,647
unsigned long 0 to
8 %llu
long int 18,446,744,073,709,551,615
float 4 %f
1.2E-38 to 3.4E+38
double 8 %lf
1.7E-308 to 1.7E+308
Integer Types
The integer data type in C is used to store the whole numbers without decimal values.
Octal values, hexadecimal values, and decimal values can be stored in int data type in
C. We can determine the size of the int data type by using the sizeof operator in C.
Unsigned int data type in C is used to store the data values from zero to positive
numbers but it can’t store negative values like signed int. Unsigned int is larger in size
than signed int and it uses “%u” as a format specifier in C programming language.
Below is the programming implementation of the int data type in C.
Note: The size of an integer data type is compiler-dependent, when processors are 16-
bit systems, then it shows the output of int as 2 bytes. And when processors are 32-
bit then it shows 2 bytes as well as 4 bytes.
int main()
{
// Integer value with positive data.
int a = 9;
return 0;
}
Output
Character Types
Character data type allows its variable to store only a single character. The storage
size of the character is 1. It is the most basic data type in C. It stores a single character
and requires a single byte of memory in almost all compilers.
int main()
{
char a = 'a';
char c;
a++;
printf("Value of a after increment is: %c\n", a);
return 0;
}
Output
Value of a: a
Value of a after increment is: b
Value of c: c
Floating-Point Types
int main()
{
float a = 9.0f;
float b = 2.5f;
// 2x10^-4
float c = 2E-4f;
printf("%f\n",a);
printf("%f\n",b);
printf("%f",c);
return 0;
}
Output
9.000000
2.500000
0.000200
Double Types
A Double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision. It is used to define numeric values which hold numbers
with decimal values in C. Double data type is basically a precision sort of data type
that is capable of holding 64 bits of decimal numbers or floating points. Since double
has more precision as compared to that float then it is much more obvious that it
occupies twice the memory as occupied by the floating-point type. It can easily
accommodate about 16 to 17 digits after or before a decimal point.
// C Program to demonstrate
// use of double data type
#include <stdio.h>
int main()
{
double a = 123123123.00;
double b = 12.293123;
double c = 2312312312.123123;
printf("%lf\n", a);
printf("%lf\n", b);
printf("%lf", c);
return 0;
}
Output
123123123.000000
12.293123
2312312312.123123
The void data type in C is used to specify that no value is present. It does not provide a
result value to its caller. It has no values and no operations. It is used to represent
nothing. Void is used in multiple ways as function return type, function arguments as
void, and pointers to void.
Syntax:
int print(void);
// C program to demonstrate
// use of void pointers
#include <stdio.h>
int main()
{
int val = 30;
void *ptr = &val;
printf("%d", *(int *)ptr);
return 0;
}
Output
30
We can use the sizeof() operator to check the size of a variable. See the following C
program for the usage of the various data types:
int main()
{
int size_of_int=sizeof(int);
int size_of_char= sizeof(char);
int size_of_float=sizeof(float);
int size_of_double=sizeof(double);
return 0;
}
Output
To check your knowledge of data types in C, go through the Quiz on Data Types.
Similar Reads
3. How to print range of basic data types without any library function and constant in C?
4. What are the data types for which it is not possible to create an array?
Next
bool in C
Article Contributed By :
GeeksforGeeks
C o u rs e s
90k+ interested Geeks 107k+ interested Geeks 795k+ interested Geeks 18k+ interested Gee
Master C Programming with Master C++ Programming - Complete Interview DevOps Engineering
Data Structures Complete Beginner to… Preparation - Self Paced Planning to Product
Advanced
Beginner to Advance Beginner to Advance Beginner to Advance Beginner to Advanc
Advertise with us
Data Science Interview Python GfG School UPSC/SSC/BA Write & Earn
& ML Corner Python Tutorial CBSE Notes for NKING Write an Article
Aptitude
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
Got It !
acknowledge that you have read and @geeksforgeeks , Some
understood our Cookie rights
▲Policy reserved
& Privacy Policy