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.

5.1 What Is a Data Type?

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
Core idea:

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.

5.2 Why Does C Need Data Types?

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.

5.3 Major Categories of C 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.

5.4 Fundamental Numeric and Character Types
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
Important:

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.

5.5 The 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'
'@'
Do not confuse:
'A'
"A"

'A' is a character constant. "A" is a string literal containing the character A followed by a terminating null character.

5.6 Integer Types

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.

Signed integer

Can represent negative and non-negative values within its implementation-defined range.

Unsigned integer

Represents non-negative values and generally provides a larger non-negative range for the same width.

5.7 Signed and Unsigned Integer Types
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
Important:

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.

5.8 The 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.

5.9 The 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.

Placement point:

Do not assume that every decimal value can be represented exactly in binary floating-point.

5.10 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.

5.11 The _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;
Remember:

bool is provided by the standard header <stdbool.h>; the fundamental C type is _Bool.

5.12 The 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.

5.13 The 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
Important:

The result of sizeof has type size_t, which is why %zu is the appropriate printf conversion specifier in C99 and later.

5.14 Do Not Memorize Fixed Sizes

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)
Special point:

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.

5.15 Finding Integer Limits

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.

5.16 Floating-Point Properties

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.

5.17 Type Specifiers and Modifiers

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;
5.18 Integer Type Relationships
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.

5.19 Character Type Variants

C provides three character types:

char
signed char
unsigned char

Whether plain char behaves as signed or unsigned is implementation-defined.

Placement point:

Do not assume plain char is always signed or always unsigned.

5.20 Integer Constant Suffixes

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
5.21 Floating-Point Constant Suffixes
3.14
3.14f
3.14L
Literal Type
3.14 double
3.14f float
3.14L long double
5.22 Data Types and Type Conversion

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.

5.23 Implicit and Explicit Conversion
Implicit conversion

The language rules cause a conversion automatically.

double x = 10;
Explicit conversion

The programmer requests a conversion using a cast.

double x = (double)10;
5.24 Why Data Types Affect Results
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.

Key lesson:

Data types can affect not only storage but also the behavior of expressions and operations.

5.25 Derived Types — Preview

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.

5.26 How Do You Choose a Data Type?
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>
Programming principle:

Choose a type based on the required range, representation, operations and portability requirements rather than simply choosing the smallest type you can remember.

5.27 Complete Program — Different Data Types
#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
5.28 Common Data Type Mistakes
  1. Assuming every C compiler uses exactly the same type sizes.
  2. Using int for a value that cannot fit in the required range.
  3. Expecting integer division to produce a fractional result.
  4. Confusing char with a string.
  5. Using the wrong printf() format specifier.
  6. Assuming plain char is always signed.
  7. Treating floating-point values as if they were always exact decimal numbers.
5.29 Common 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
Important:

Correct format specifiers matter. Passing an argument of the wrong type to a variadic function such as printf() can result in undefined behavior.

5.30 Quick Revision
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.
Test Your Understanding
1 Why does C have different data types?
2 Is int always 4 bytes?
3 What is the difference between float and double?
4 What does sizeof return?
5 Why can 5 / 2 produce 2 instead of 2.5?
INTERVIEW PREPARATION

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 char an integer type? Yes. C defines char as 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.
PLACEMENT TIPS

Data Types — Placement Tips

  1. Know the difference between char, int, float and double.
  2. Do not memorize type sizes as universal constants.
  3. Remember sizeof(char) is always 1.
  4. Understand signed versus unsigned integer types.
  5. Remember that 5 / 2 is integer division.
  6. Know why 3.14 is a double floating constant and 3.14f is a float constant.
  7. Know what limits.h and float.h provide.
PRACTICE
Data Types Practice
0 Solved
0 / 5 Completed
0% Score
Problem 1

Integer and Character

Declare an integer variable with value 25 and a character variable with value 'A'. Print both values.

Problem 2

Calculate a Decimal Result

Read two integers and calculate their average as a floating-point value.

Problem 3

Find the Size of a Type

Print the size of int, char, and double using sizeof.

Problem 4

Integer vs Floating-Point Division

Read two integers and print their division as a floating-point value with exactly two decimal places.

Problem 5

Store Different Types

Read an integer age, a floating-point height, and a character grade. Print them in the same order.

EXTRA PRACTICE

More Data Type Problems

  1. Print the sizes of all common integer types using sizeof.
  2. Read two integers and print their sum, difference, product and floating-point quotient.
  3. Read a character and print it.
  4. Store a temperature using float and print it with two decimal places.
  5. Use double to calculate the area of a circle.
  6. Create a program that demonstrates the difference between 5 / 2 and 5.0 / 2.0.
  7. Use limits.h to display INT_MIN and INT_MAX.
  8. Use float.h to display floating-point precision information.
KEY TAKEAWAY

Data Types — What You Must Remember

  • Data types describe the kind of data associated with C entities.
  • int is the common general-purpose integer type.
  • char is an integer type used for character-sized data.
  • float, double, and long double are floating-point types.
  • _Bool is C's built-in Boolean-capable integer type.
  • void represents absence of a value/type in several contexts.
  • Integer types can be signed or unsigned.
  • Type sizes are implementation-dependent; use sizeof when you need the actual size.
  • limits.h provides integer limits.
  • float.h provides floating-point characteristics.
  • Data types can affect expression results, such as integer versus floating-point division.
← Previous Topic: Variables & Constants Next Topic: Input & Output →