Data Types in C
A data type tells the C compiler what kind of value an object can represent and provides the information needed to interpret that object's stored representation. Choosing the appropriate data type is one of the foundations of writing correct and efficient C programs.
A data type describes the kind of data associated with an object, expression, function return value, or other declaration in C.
For example:
int age = 20;
float price = 99.50f;
char grade = 'A';
The declarations tell the compiler that these objects have different types.
DATA TYPE
│
┌────────┼────────┐
│ │ │
▼ ▼ ▼
int float char
│ │ │
▼ ▼ ▼
20 99.5 A
The type is not merely a label. It is part of the information the compiler uses to determine how an object or expression is interpreted and what operations are valid.
A program works with different kinds of information.
- whole numbers
- fractional values
- characters
- very large or very small numeric values
- addresses
- collections of related data
C needs a way to distinguish these different kinds of data.
int students = 60;
float temperature = 36.5f;
char section = 'A';
These objects represent different kinds of information and therefore use different types.
C TYPES │ ├── Basic / Fundamental Types │ ├── char │ ├── signed integer types │ ├── unsigned integer types │ ├── _Bool │ ├── float │ ├── double │ └── long double │ ├── void │ ├── Derived Types │ ├── Arrays │ ├── Pointers │ └── Function types │ ├── Structure Types │ ├── Union Types │ └── Enumeration Types
C's type system is richer than the small list of types usually shown in beginner examples. We will study each category gradually.
| Type | Typical Use | Example |
|---|---|---|
char |
Character-sized integer type | 'A' |
short |
Short integer type | 100 |
int |
General-purpose integer | 500 |
long |
Integer type with at least as much range as int | 100000L |
long long |
Large integer values | 9000000000LL |
float |
Single-precision floating-point | 3.14f |
double |
Double-precision floating-point | 3.14159 |
long double |
Extended floating-point type | 3.14159L |
The exact size and range of many C types are implementation-defined. Do not memorize one fixed size as a universal rule for every C compiler and platform.
char Type
The char type is designed for character data and is
also an integer type in C.
char grade = 'A';
char symbol = '#';
Character constants use single quotation marks.
'A'
'B'
'7'
'@'
'A'
"A"
'A' is a character constant.
"A" is a string literal containing the
character A followed by a terminating null character.
Integer types represent whole-number values.
int count = 100;
short smallNumber = 25;
long population = 1000000L;
long long distance = 9000000000LL;
C provides several integer types because programs may need different ranges and storage requirements.
Can represent negative and non-negative values within its implementation-defined range.
Represents non-negative values and generally provides a larger non-negative range for the same width.
signed int temperature = -10;
unsigned int students = 500;
The unsigned keyword specifies that the type is
non-negative.
| Type | Can represent negative values? |
|---|---|
int |
Yes |
signed int |
Yes |
unsigned int |
No |
Unsigned arithmetic has its own rules. Converting between signed and unsigned integer types can also produce surprising results if you do not understand the conversion rules.
float Type
float is a floating-point type used for values
that may contain a fractional component.
float temperature = 36.5f;
float price = 99.99f;
The f suffix indicates a float
floating constant.
3.14f
Without the suffix:
3.14
the floating constant has type double.
double Type
double is a floating-point type that normally provides
more precision than float.
double pi = 3.141592653589793;
Floating-point types represent real-number approximations rather than arbitrary exact decimal values.
Do not assume that every decimal value can be represented exactly in binary floating-point.
long double
long double is an extended floating-point type.
Its precision and representation are implementation-dependent.
long double value = 3.141592653589793238L;
The L suffix identifies a long double
floating constant.
_Bool Type
C provides the built-in integer type _Bool.
_Bool isPassed = 1;
The standard header <stdbool.h> can provide
the convenient spelling bool together with
true and false macros.
#include <stdbool.h>
bool isPassed = true;
bool is provided by the standard header
<stdbool.h>; the fundamental C type is
_Bool.
void Type
void represents the absence of a value or type
information in several C contexts.
A common example is a function that does not return a value:
void displayMessage(void)
{
printf("Hello");
}
A pointer to void, written void *,
is a separate concept and is useful for generic object pointers.
sizeof Operator
The sizeof operator determines the size in bytes
of a type or object.
#include <stdio.h>
int main(void)
{
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(double));
printf("%zu\n", sizeof(char));
return 0;
}
sizeof(type)
│
▼
number of bytes
│
▼
depends on implementation
The result of sizeof has type
size_t, which is why %zu
is the appropriate printf conversion specifier in C99
and later.
A common beginner mistake is memorizing:
int = 4 bytes
char = 1 byte
float = 4 bytes
double = 8 bytes
These are common on many modern systems, but C does not require every implementation to use exactly those sizes.
The correct way to discover the size on the current implementation is:
sizeof(type)
C guarantees that sizeof(char) is exactly
1. A C byte is not required to contain exactly
8 bits; the number of bits in a byte is represented by
CHAR_BIT.
The header <limits.h> provides macros describing
limits of integer types on the current implementation.
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("INT_MIN = %d\n", INT_MIN);
printf("INT_MAX = %d\n", INT_MAX);
printf("UINT_MAX = %u\n", UINT_MAX);
return 0;
}
This is safer than assuming one universal numeric range.
The header <float.h> provides information
about the characteristics of floating-point types.
#include <stdio.h>
#include <float.h>
int main(void)
{
printf("FLT_DIG = %d\n", FLT_DIG);
printf("DBL_DIG = %d\n", DBL_DIG);
return 0;
}
This becomes useful when programs need to reason about floating-point precision.
C provides keywords that combine with integer and floating types to describe different type forms.
| Keyword | Purpose |
|---|---|
signed |
Specifies a signed integer type. |
unsigned |
Specifies a non-negative integer type. |
short |
Specifies a short integer type. |
long |
Specifies a long integer or extended floating type. |
short int a;
unsigned int b;
long int c;
long long int d;
Integer types signed │ ├── signed char ├── short ├── int ├── long └── long long unsigned │ ├── unsigned char ├── unsigned short ├── unsigned int ├── unsigned long └── unsigned long long
The exact representation and range depend on the implementation, but C defines minimum requirements and ordering relationships among these standard integer types.
C provides three character types:
char
signed char
unsigned char
Whether plain char behaves as signed or unsigned
is implementation-defined.
Do not assume plain char is always signed
or always unsigned.
Integer literals can use suffixes to influence their type.
100
100U
100L
100UL
100LL
100ULL
| Literal | Meaning |
|---|---|
100 |
Integer constant without an explicit suffix |
100U |
Unsigned integer constant |
100L |
Long integer constant |
100LL |
Long long integer constant |
100ULL |
Unsigned long long integer constant |
3.14
3.14f
3.14L
| Literal | Type |
|---|---|
3.14 |
double |
3.14f |
float |
3.14L |
long double |
When different arithmetic types participate in an expression, C may convert values according to its conversion rules.
int a = 10;
double b = 2.5;
double result = a + b;
The integer value participates in the calculation after the appropriate conversion.
10 + 2.5
│ │
│ │
└──────┬───────────┘
▼
common arithmetic
conversion
│
▼
12.5
Type conversion is covered in greater depth when expressions and operators are introduced.
The language rules cause a conversion automatically.
double x = 10;
The programmer requests a conversion using a cast.
double x = (double)10;
int a = 5;
int b = 2;
printf("%d\n", a / b);
Because both operands are integers, the division is integer
division and the result is 2.
Compare:
double result = 5.0 / 2.0;
Here the result is 2.5.
Data types can affect not only storage but also the behavior of expressions and operations.
C also supports derived and user-defined type constructs.
int numbers[5];
int *ptr;
struct Student
{
int roll;
};
Arrays, pointers, structures, unions, enumerations and function types will be studied separately.
| Requirement | Possible Choice |
|---|---|
| General whole number | int |
| Non-negative count | Appropriate unsigned integer type when justified |
| Character-sized data | char |
| Fractional value | float or double |
| Higher floating-point precision | double or long double, as appropriate |
| Boolean-style state | _Bool or bool with <stdbool.h> |
Choose a type based on the required range, representation, operations and portability requirements rather than simply choosing the smallest type you can remember.
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int age = 21;
float height = 5.8f;
double percentage = 87.4567;
char grade = 'A';
bool passed = true;
printf("Age = %d\n", age);
printf("Height = %.1f\n", height);
printf("Percentage = %.4f\n", percentage);
printf("Grade = %c\n", grade);
printf("Passed = %d\n", passed);
return 0;
}
Program
│
├── int
│ └── age
│
├── float
│ └── height
│
├── double
│ └── percentage
│
├── char
│ └── grade
│
├── bool
│ └── passed
│
└── printf()
└── displays values
- Assuming every C compiler uses exactly the same type sizes.
-
Using
intfor a value that cannot fit in the required range. - Expecting integer division to produce a fractional result.
-
Confusing
charwith a string. -
Using the wrong
printf()format specifier. -
Assuming plain
charis always signed. - Treating floating-point values as if they were always exact decimal numbers.
printf() Format Specifiers
| Specifier | Common Use |
|---|---|
%d |
Signed decimal integer |
%u |
Unsigned decimal integer |
%c |
Character |
%f |
Floating-point output |
%zu |
size_t, commonly used with sizeof |
Correct format specifiers matter. Passing an argument of the
wrong type to a variadic function such as printf()
can result in undefined behavior.
| Concept | Remember |
|---|---|
| Data type | Describes the type associated with a C entity. |
char |
Character type and integer type. |
int |
General-purpose integer type. |
float |
Single-precision floating-point type. |
double |
Double-precision floating-point type. |
void |
Represents absence of a value/type in several contexts. |
_Bool |
Built-in Boolean-capable integer type. |
sizeof |
Reports size in bytes. |
limits.h |
Provides integer type limits. |
float.h |
Provides floating-point characteristics. |
int always 4 bytes?
sizeof(int) to determine the size on the
current implementation.
float and
double?
double
normally provides greater precision. Their exact
representation is implementation-dependent.
sizeof return?
size_t.
5 / 2 produce 2
instead of 2.5?
C Data Types — Interview Questions
These questions focus on concepts frequently tested in C programming interviews and placement examinations.
- What is a data type? It describes the type of data associated with a C entity.
- What is the difference between signed and unsigned? Unsigned integer types represent only non-negative values.
-
Is
charan integer type? Yes. C definescharas an integer type. -
What is
sizeof? An operator that determines size in bytes. -
What is
size_t? An unsigned integer type suitable for representing object sizes. -
What is
void? A type used to represent absence of a value/type in several contexts. -
What is
_Bool? C's built-in Boolean-capable integer type. - Why should fixed type sizes not be assumed? C permits implementation-defined sizes for many types.
Data Types — Placement Tips
-
Know the difference between
char,int,floatanddouble. - Do not memorize type sizes as universal constants.
-
Remember
sizeof(char)is always1. - Understand signed versus unsigned integer types.
-
Remember that
5 / 2is integer division. -
Know why
3.14is adoublefloating constant and3.14fis afloatconstant. -
Know what
limits.handfloat.hprovide.
Integer and Character
Declare an integer variable with value
25 and a character variable with value
'A'. Print both values.
int number = 25;
and
char letter = 'A';
Then use %d and %c.
Your Code
Input
Output
#include <stdio.h>
int main(void)
{
int number = 25;
char letter = 'A';
printf("%d\n", number);
printf("%c\n", letter);
return 0;
}
Calculate a Decimal Result
Read two integers and calculate their average as a floating-point value.
Your Code
Input
Output
#include <stdio.h>
int main(void)
{
int a, b;
double average;
scanf("%d %d", &a, &b);
average = (double)(a + b) / 2;
printf("%.2f\n", average);
return 0;
}
Find the Size of a Type
Print the size of int,
char, and double using
sizeof.
sizeof(int),
sizeof(char),
and
sizeof(double).
Print the results using %zu.
Your Code
Input
Output
#include <stdio.h>
int main(void)
{
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(char));
printf("%zu\n", sizeof(double));
return 0;
}
Integer vs Floating-Point Division
Read two integers and print their division as a floating-point value with exactly two decimal places.
double before
division.
Your Code
Input
Output
#include <stdio.h>
int main(void)
{
int a, b;
double result;
scanf("%d %d", &a, &b);
result = (double)a / b;
printf("%.2f\n", result);
return 0;
}
Store Different Types
Read an integer age, a floating-point height, and a character grade. Print them in the same order.
int, float, and
char. A suitable scanf()
pattern is:
%d %f %c
Your Code
Input
Output
#include <stdio.h>
int main(void)
{
int age;
float height;
char grade;
scanf("%d %f %c", &age, &height, &grade);
printf("Age = %d\n", age);
printf("Height = %.1f\n", height);
printf("Grade = %c\n", grade);
return 0;
}
More Data Type Problems
-
Print the sizes of all common integer types using
sizeof. - Read two integers and print their sum, difference, product and floating-point quotient.
- Read a character and print it.
-
Store a temperature using
floatand print it with two decimal places. -
Use
doubleto calculate the area of a circle. -
Create a program that demonstrates the difference between
5 / 2and5.0 / 2.0. -
Use
limits.hto displayINT_MINandINT_MAX. -
Use
float.hto display floating-point precision information.
Data Types — What You Must Remember
- Data types describe the kind of data associated with C entities.
-
intis the common general-purpose integer type. -
charis an integer type used for character-sized data. -
float,double, andlong doubleare floating-point types. -
_Boolis C's built-in Boolean-capable integer type. -
voidrepresents absence of a value/type in several contexts. - Integer types can be signed or unsigned.
-
Type sizes are implementation-dependent; use
sizeofwhen you need the actual size. -
limits.hprovides integer limits. -
float.hprovides floating-point characteristics. - Data types can affect expression results, such as integer versus floating-point division.