Skip to main content

Data Types in C Programming

2 min read Updated May 29, 2025
Share:
On this page (11sections)

Introduction

Every variable in C has a data type that tells the compiler how much memory to allocate and what operations are allowed. C provides several fundamental types and lets you build complex types from them (arrays, structures, pointers).

Fundamental Data Types

TypeTypical sizePurposeExample
char1 byteSingle character or small integer'A', 65
int4 bytesWhole numbers42, -100
float4 bytesSingle-precision decimal3.14f
double8 bytesDouble-precision decimal3.14159
voidNo value (functions with no return)void printMsg(void)

Use sizeof(type) to check actual sizes on your system:

#include <stdio.h>

int main(void) {
    printf("char:   %zu byte(s)\n", sizeof(char));
    printf("int:    %zu byte(s)\n", sizeof(int));
    printf("float:  %zu byte(s)\n", sizeof(float));
    printf("double: %zu byte(s)\n", sizeof(double));
    return 0;
}

Sample Output (typical 64-bit system)

char:   1 byte(s)
int:    4 byte(s)
float:  4 byte(s)
double: 8 byte(s)

Type Modifiers

C extends basic types with modifiers:

ModifierEffect
signed / unsignedWith or without sign (default for int is signed)
shortSmaller range, less memory (short int)
longWider range on some platforms (long int, long double)
unsigned int count = 100;
short int age = 25;
long int bigNumber = 1000000L;

Format Specifiers for printf

TypeSpecifierExample
int%dprintf("%d", n);
unsigned int%uprintf("%u", n);
float / double%fprintf("%.2f", pi);
char%cprintf("%c", ch);
char* (string)%sprintf("%s", name);

Example — Declaring and Printing Values

#include <stdio.h>

int main(void) {
    int age = 20;
    float height = 5.9f;
    char grade = 'A';

    printf("Age:    %d years\n", age);
    printf("Height: %.1f feet\n", height);
    printf("Grade:  %c\n", grade);
    return 0;
}

Sample Output

Age:    20 years
Height: 5.9 feet
Grade:  A

Best Practices

  • Choose the smallest type that fits your data to save memory.
  • Use double instead of float unless you have a specific reason.
  • Append f to float literals (3.14f) to avoid unintended double promotion.
  • Always match format specifiers to the actual variable type.

Common Mistakes

  • Using %d to print a float — causes undefined behaviour; use %f.
  • Assuming fixed sizes — always use sizeof or <stdint.h> types (int32_t) for portability.

Continue learning with these related tutorials and programs:

Frequently Asked Questions

What is the size of int in C?
The size of int is platform-dependent. On most modern systems it is 4 bytes (32 bits), but C only guarantees a minimum range — use sizeof(int) to check on your compiler.
What is the difference between float and double?
double typically has twice the precision and range of float. Use float for memory-sensitive graphics; use double for general numeric calculations.

Related Tutorials

Search tutorials