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

TypeSize (typical)RangeFormat Specifier
char1 byte-128 to 127%c (char), %d (int)
unsigned char1 byte0 to 255%u
short2 bytes-32,768 to 32,767%hd
unsigned short2 bytes0 to 65,535%hu
int4 bytes-2,147,483,648 to 2,147,483,647%d
unsigned int4 bytes0 to 4,294,967,295%u
long4 or 8 bytesVaries%ld
unsigned long4 or 8 bytesVaries%lu
long long8 bytesยฑ9.2ร—10ยนโธ%lld
unsigned long long8 bytes0 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.

TypeSizePrecisionRangeFormat Specifier
float4 bytes6-7 digitsยฑ3.4ร—10ยณโธ%f, %e, %g
double8 bytes15-16 digitsยฑ1.7ร—10ยณโฐโธ%lf, %le, %lg
long double10-16 bytes18-19+ digitsVaries%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:

CharacterASCIICharacterASCII
'0'-'9'48-57'A'-'Z'65-90
'a'-'z'97-122' ' (space)32
'\n'10'\t'9
'\0'0

Escape Sequences:

EscapeMeaning
\nNewline
\tTab
\\Backslash
\'Single quote
\"Double quote
\0Null character
\rCarriage return
\bBackspace
\aAlert (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:

ConstantDescription
CHAR_MIN, CHAR_MAXRange of char
SHRT_MIN, SHRT_MAXRange of short
INT_MIN, INT_MAXRange of int
LONG_MIN, LONG_MAXRange of long
LLONG_MIN, LLONG_MAXRange of long long
UCHAR_MAXMax unsigned char
USHRT_MAXMax unsigned short
UINT_MAXMax unsigned int
ULONG_MAXMax unsigned long
ULLONG_MAXMax unsigned long long

float.h Constants:

ConstantDescription
FLT_MIN, FLT_MAXRange of float
DBL_MIN, DBL_MAXRange of double
LDBL_MIN, LDBL_MAXRange of long double
FLT_DIGDecimal digits of precision (float)
DBL_DIGDecimal 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

TypeFormatExample
char%cprintf("%c", 'A');
int%d or %iprintf("%d", 42);
unsigned int%uprintf("%u", 100);
short%hdprintf("%hd", 10);
long%ldprintf("%ld", 100000L);
long long%lldprintf("%lld", 10000000000LL);
float%fprintf("%f", 3.14f);
double%lfprintf("%lf", 3.14159);
long double%Lfprintf("%Lf", 3.14159L);
pointer%pprintf("%p", &x);
string%sprintf("%s", "Hello");
hexadecimal%x or %Xprintf("%x", 255);
octal%oprintf("%o", 64);
scientific%e or %Eprintf("%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

  1. โ€ขUse int for most integer needs - it's the natural size for the CPU
  2. โ€ขUse double for floating-point - unless memory is critical
  3. โ€ขUse unsigned for non-negative values - like sizes, counts
  4. โ€ขAlways initialize variables - avoid undefined behavior
  5. โ€ขCheck for overflow - especially in loops and calculations
  6. โ€ขUse size_t for array indices - guaranteed to be large enough
  7. โ€ขUse stdint.h for 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

  1. โ€ขC has four basic types: char, int, float, double
  2. โ€ขModifiers (signed, unsigned, short, long) alter type properties
  3. โ€ขSize varies by system - use sizeof() to check
  4. โ€ขUse correct format specifiers - mismatches cause undefined behavior
  5. โ€ขUnderstand overflow - values wrap around at type limits
  6. โ€ขvoid means "no type" - used for functions and generic pointers

โญ๏ธ Next Topic

Continue to Variables and Constants to learn how to declare and use variables.

Basic Syntax - C Programming Tutorial | DeepML