Data Types & Variables
C is a low-level language where you must understand how data is stored in memory. Every variable has a type that determines its size, range, and operations. Understanding data types is essential for writing efficient C programs.
40 min•By Priygop Team•Last updated: Feb 2026
C Data Types
- int (4 bytes) — Whole numbers: -2 billion to +2 billion. Most common integer type
- float (4 bytes) — Single-precision decimal: ~7 digits precision. Use for approximate values
- double (8 bytes) — Double-precision decimal: ~15 digits. Preferred for scientific calculations
- char (1 byte) — Single character: 'A', '0', '\n'. Actually stores ASCII number (65 = 'A')
- short, long, long long — 2, 4/8, 8 bytes respectively. For smaller/larger integer ranges
- unsigned — No negative values: unsigned int (0 to 4 billion). Double the positive range
- sizeof() — Returns size in bytes: sizeof(int) = 4. Essential for memory management
Variables & I/O Code
Example
#include <stdio.h>
int main() {
// Variable declarations
int age = 25;
float temperature = 98.6f;
double pi = 3.14159265358979;
char grade = 'A';
char name[50] = "Alice";
// sizeof operator
printf("int: %zu bytes\n", sizeof(int)); // 4
printf("float: %zu bytes\n", sizeof(float)); // 4
printf("double: %zu bytes\n", sizeof(double)); // 8
printf("char: %zu bytes\n", sizeof(char)); // 1
// printf: formatted output
printf("Name: %s, Age: %d\n", name, age);
printf("Temp: %.1f, Pi: %.5f\n", temperature, pi);
printf("Grade: %c (ASCII: %d)\n", grade, grade);
// Format specifiers: %d (int), %f (float/double), %c (char), %s (string)
// %x (hex), %o (octal), %u (unsigned), %p (pointer), %zu (size_t)
// scanf: formatted input
int userAge;
printf("Enter age: ");
scanf("%d", &userAge); // & = address-of operator (required!)
// Type casting
int a = 7, b = 2;
float result = (float)a / b; // 3.5 (cast before division)
printf("7 / 2 = %.1f\n", result);
// Constants
const double GRAVITY = 9.81;
#define MAX_SIZE 100 // Preprocessor constant
return 0;
}