Docs
Basic Syntax
Data Types in C
๐ Introduction
Data types specify the type of data a variable can hold. C is a statically-typed language, meaning you must declare the type of each variable before using it. Understanding data types is fundamental to writing correct and efficient C programs.
๐ฏ Categories of Data Types
C Data Types
โ
โโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโ
โ โ โ
Primary/Basic Derived User-Defined
โ โ โ
โโโโโโดโโโโโ โโโโโโโดโโโโโโ โโโโโโดโโโโโ
โ โ โ โ โ โ
Integer Floating Arrays Pointers Structures Unions
โ Point Functions typedef enum
โ โ
int float
char double
short long double
long
long long
๐ Primary (Basic) Data Types
1. Integer Types
Integer types store whole numbers (no decimal point).
| Type | Size (typical) | Range | Format Specifier |
|---|---|---|---|
char | 1 byte | -128 to 127 | %c (char), %d (int) |
unsigned char | 1 byte | 0 to 255 | %u |
short | 2 bytes | -32,768 to 32,767 | %hd |
unsigned short | 2 bytes | 0 to 65,535 | %hu |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 | %d |
unsigned int | 4 bytes | 0 to 4,294,967,295 | %u |
long | 4 or 8 bytes | Varies | %ld |
unsigned long | 4 or 8 bytes | Varies | %lu |
long long | 8 bytes | ยฑ9.2ร10ยนโธ | %lld |
unsigned long long | 8 bytes | 0 to 1.8ร10ยนโน | %llu |
Integer Declaration Examples:
// Signed integers (can hold negative values)
int age = 25;
short temperature = -10;
long population = 7800000000L;
long long bigNumber = 9223372036854775807LL;
// Unsigned integers (only positive values)
unsigned int count = 100;
unsigned long fileSize = 4294967295UL;
Integer Literal Suffixes:
int a = 100; // int
long b = 100L; // long
long long c = 100LL; // long long
unsigned d = 100U; // unsigned int
unsigned long e = 100UL; // unsigned long
Integer in Different Bases:
int decimal = 42; // Decimal (base 10)
int octal = 052; // Octal (base 8) - starts with 0
int hex = 0x2A; // Hexadecimal (base 16) - starts with 0x
int binary = 0b101010; // Binary (base 2) - C23, starts with 0b
2. Floating-Point Types
Floating-point types store decimal numbers.
| Type | Size | Precision | Range | Format Specifier |
|---|---|---|---|---|
float | 4 bytes | 6-7 digits | ยฑ3.4ร10ยณโธ | %f, %e, %g |
double | 8 bytes | 15-16 digits | ยฑ1.7ร10ยณโฐโธ | %lf, %le, %lg |
long double | 10-16 bytes | 18-19+ digits | Varies | %Lf, %Le, %Lg |
Floating-Point Declaration Examples:
float price = 19.99f; // Note: 'f' suffix for float
double pi = 3.14159265358979; // More precision
long double precise = 3.14159265358979323846L; // Maximum precision
Floating-Point Literal Suffixes:
float f = 3.14f; // float (f or F suffix)
double d = 3.14; // double (default)
long double ld = 3.14L; // long double (L suffix)
Scientific Notation:
double avogadro = 6.022e23; // 6.022 ร 10ยฒยณ
double planck = 6.626e-34; // 6.626 ร 10โปยณโด
float small = 1.5E-10f; // 1.5 ร 10โปยนโฐ
3. Character Type
The char type stores a single character (1 byte).
char letter = 'A'; // Character literal
char newline = '\n'; // Escape sequence
char digit = '5'; // Character '5' (not integer 5)
char asciiValue = 65; // Also 'A' (ASCII value)
ASCII Values:
| Character | ASCII | Character | ASCII |
|---|---|---|---|
| '0'-'9' | 48-57 | 'A'-'Z' | 65-90 |
| 'a'-'z' | 97-122 | ' ' (space) | 32 |
| '\n' | 10 | '\t' | 9 |
| '\0' | 0 |
Escape Sequences:
| Escape | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\0 | Null character |
\r | Carriage return |
\b | Backspace |
\a | Alert (bell) |
4. Void Type
void represents "no type" or "no value."
// Function that returns nothing
void printMessage(void) {
printf("Hello!\n");
}
// Void pointer (generic pointer)
void *ptr;
// Function with no parameters
int getValue(void) {
return 42;
}
๐ Size and Range Verification
The exact size of data types can vary by system. Use sizeof to check:
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main() {
// Check sizes
printf("char: %zu bytes\n", sizeof(char));
printf("short: %zu bytes\n", sizeof(short));
printf("int: %zu bytes\n", sizeof(int));
printf("long: %zu bytes\n", sizeof(long));
printf("long long: %zu bytes\n", sizeof(long long));
printf("float: %zu bytes\n", sizeof(float));
printf("double: %zu bytes\n", sizeof(double));
// Check ranges (from limits.h)
printf("\nint range: %d to %d\n", INT_MIN, INT_MAX);
printf("unsigned int max: %u\n", UINT_MAX);
// Check float limits (from float.h)
printf("\nfloat max: %e\n", FLT_MAX);
printf("double max: %e\n", DBL_MAX);
return 0;
}
limits.h Constants:
| Constant | Description |
|---|---|
CHAR_MIN, CHAR_MAX | Range of char |
SHRT_MIN, SHRT_MAX | Range of short |
INT_MIN, INT_MAX | Range of int |
LONG_MIN, LONG_MAX | Range of long |
LLONG_MIN, LLONG_MAX | Range of long long |
UCHAR_MAX | Max unsigned char |
USHRT_MAX | Max unsigned short |
UINT_MAX | Max unsigned int |
ULONG_MAX | Max unsigned long |
ULLONG_MAX | Max unsigned long long |
float.h Constants:
| Constant | Description |
|---|---|
FLT_MIN, FLT_MAX | Range of float |
DBL_MIN, DBL_MAX | Range of double |
LDBL_MIN, LDBL_MAX | Range of long double |
FLT_DIG | Decimal digits of precision (float) |
DBL_DIG | Decimal digits of precision (double) |
๐ Type Modifiers
Modifiers change the properties of basic types:
signed and unsigned
signed int x = -10; // Can hold negative (default for int)
unsigned int y = 10; // Only positive, larger range
signed char a = -100; // -128 to 127
unsigned char b = 200; // 0 to 255
short and long
short int small = 100; // At least 16 bits
long int large = 1000000L; // At least 32 bits
long long int huge = 10000000000LL; // At least 64 bits
Modifier Combinations:
unsigned long int value = 4000000000UL;
signed short int temp = -32000;
long long unsigned int bigPositive = 18000000000000000000ULL;
๐จ Format Specifiers Summary
| Type | Format | Example |
|---|---|---|
char | %c | printf("%c", 'A'); |
int | %d or %i | printf("%d", 42); |
unsigned int | %u | printf("%u", 100); |
short | %hd | printf("%hd", 10); |
long | %ld | printf("%ld", 100000L); |
long long | %lld | printf("%lld", 10000000000LL); |
float | %f | printf("%f", 3.14f); |
double | %lf | printf("%lf", 3.14159); |
long double | %Lf | printf("%Lf", 3.14159L); |
pointer | %p | printf("%p", &x); |
string | %s | printf("%s", "Hello"); |
hexadecimal | %x or %X | printf("%x", 255); |
octal | %o | printf("%o", 64); |
scientific | %e or %E | printf("%e", 1234.5); |
percent | %% | printf("100%%"); |
Format Modifiers:
printf("%10d\n", 42); // Right-aligned, width 10
printf("%-10d\n", 42); // Left-aligned, width 10
printf("%010d\n", 42); // Zero-padded, width 10
printf("%.2f\n", 3.14159); // 2 decimal places
printf("%8.2f\n", 3.14159);// Width 8, 2 decimal places
printf("%+d\n", 42); // Show sign
โ ๏ธ Common Mistakes
1. Overflow
int max = 2147483647;
int overflow = max + 1; // OVERFLOW! Wraps to -2147483648
unsigned char c = 255;
c = c + 1; // Wraps to 0
2. Precision Loss
float f = 123456789.0f; // May lose precision
printf("%f\n", f); // Might print 123456792.000000
double d = 123456789.0; // Better precision
3. Integer Division
int a = 5, b = 2;
float result = a / b; // result = 2.0, not 2.5!
float correct = (float)a / b; // result = 2.5
4. Uninitialized Variables
int x; // Contains garbage value!
printf("%d", x); // Undefined behavior
โ Best Practices
- โขUse
intfor most integer needs - it's the natural size for the CPU - โขUse
doublefor floating-point - unless memory is critical - โขUse
unsignedfor non-negative values - like sizes, counts - โขAlways initialize variables - avoid undefined behavior
- โขCheck for overflow - especially in loops and calculations
- โขUse
size_tfor array indices - guaranteed to be large enough - โขUse
stdint.hfor exact sizes -int32_t,uint64_t, etc.
Fixed-Width Types (stdint.h):
#include <stdint.h>
int8_t a; // Exactly 8 bits, signed
uint8_t b; // Exactly 8 bits, unsigned
int16_t c; // Exactly 16 bits, signed
uint16_t d; // Exactly 16 bits, unsigned
int32_t e; // Exactly 32 bits, signed
uint32_t f; // Exactly 32 bits, unsigned
int64_t g; // Exactly 64 bits, signed
uint64_t h; // Exactly 64 bits, unsigned
๐ Key Takeaways
- โขC has four basic types:
char,int,float,double - โขModifiers (
signed,unsigned,short,long) alter type properties - โขSize varies by system - use
sizeof()to check - โขUse correct format specifiers - mismatches cause undefined behavior
- โขUnderstand overflow - values wrap around at type limits
- โข
voidmeans "no type" - used for functions and generic pointers
โญ๏ธ Next Topic
Continue to Variables and Constants to learn how to declare and use variables.