c

examples

examples.c🔧
/*
 * ============================================================================
 * DATA TYPES IN C - EXAMPLES
 * ============================================================================
 * This file demonstrates all C data types with practical examples.
 * 
 * Compile: gcc -Wall examples.c -o examples
 * Run: ./examples
 * ============================================================================
 */

#include <stdio.h>
#include <limits.h>    // For INT_MAX, INT_MIN, etc.
#include <float.h>     // For FLT_MAX, DBL_MAX, etc.
#include <stdint.h>    // For fixed-width types
#include <stdbool.h>   // For bool type (C99)

/*
 * Example 1: Integer Types
 * ------------------------
 * Demonstrates different integer types and their ranges.
 */
void example_integer_types() {
    printf("\n=== Example 1: Integer Types ===\n");
    
    // Signed integers
    char c = 'A';                    // 1 byte
    short s = 32000;                 // At least 2 bytes
    int i = 2147483647;              // At least 2 bytes (usually 4)
    long l = 2147483647L;            // At least 4 bytes
    long long ll = 9223372036854775807LL;  // At least 8 bytes
    
    printf("char: %c (ASCII: %d)\n", c, c);
    printf("short: %hd\n", s);
    printf("int: %d\n", i);
    printf("long: %ld\n", l);
    printf("long long: %lld\n", ll);
    
    // Unsigned integers
    unsigned char uc = 255;
    unsigned short us = 65535;
    unsigned int ui = 4294967295U;
    unsigned long ul = 4294967295UL;
    
    printf("\nunsigned char: %u\n", uc);
    printf("unsigned short: %hu\n", us);
    printf("unsigned int: %u\n", ui);
    printf("unsigned long: %lu\n", ul);
    
    // Size of each type
    printf("\nSizes:\n");
    printf("sizeof(char) = %zu byte\n", sizeof(char));
    printf("sizeof(short) = %zu bytes\n", sizeof(short));
    printf("sizeof(int) = %zu bytes\n", sizeof(int));
    printf("sizeof(long) = %zu bytes\n", sizeof(long));
    printf("sizeof(long long) = %zu bytes\n", sizeof(long long));
}

/*
 * Example 2: Floating-Point Types
 * -------------------------------
 * Demonstrates float, double, and long double.
 */
void example_floating_types() {
    printf("\n=== Example 2: Floating-Point Types ===\n");
    
    float f = 3.14159265f;           // 4 bytes, ~7 digits precision
    double d = 3.14159265358979;     // 8 bytes, ~15 digits precision
    long double ld = 3.14159265358979323846L;  // 10-16 bytes
    
    printf("float: %.7f\n", f);
    printf("double: %.15f\n", d);
    printf("long double: %.19Lf\n", ld);
    
    // Precision demonstration
    printf("\nPrecision comparison:\n");
    float f2 = 123456789.0f;
    double d2 = 123456789.0;
    printf("float 123456789: %.0f\n", f2);    // May lose precision
    printf("double 123456789: %.0lf\n", d2);  // Accurate
    
    // Scientific notation
    double avogadro = 6.022e23;
    double planck = 6.626e-34;
    printf("\nScientific notation:\n");
    printf("Avogadro's number: %e\n", avogadro);
    printf("Planck's constant: %e\n", planck);
    
    // Sizes
    printf("\nSizes:\n");
    printf("sizeof(float) = %zu bytes\n", sizeof(float));
    printf("sizeof(double) = %zu bytes\n", sizeof(double));
    printf("sizeof(long double) = %zu bytes\n", sizeof(long double));
}

/*
 * Example 3: Character Type
 * -------------------------
 * Characters and ASCII values.
 */
void example_char_type() {
    printf("\n=== Example 3: Character Type ===\n");
    
    // Character literals
    char letter = 'A';
    char digit = '5';
    char newline = '\n';
    char tab = '\t';
    
    printf("letter: %c (ASCII: %d)\n", letter, letter);
    printf("digit: %c (ASCII: %d)\n", digit, digit);
    printf("Note: '5' is NOT the same as 5\n");
    printf("'5' = ASCII %d, 5 = integer 5\n", digit);
    
    // Character arithmetic
    printf("\nCharacter arithmetic:\n");
    char a = 'A';
    printf("'A' + 1 = '%c'\n", a + 1);
    printf("'A' + 32 = '%c' (lowercase)\n", a + 32);
    
    // Escape sequences
    printf("\nEscape sequences:\n");
    printf("Newline: Hello\\nWorld\n");
    printf("Tab: Hello\tWorld\n");
    printf("Backslash: C:\\Users\n");
    printf("Quote: \"Hello\"\n");
    printf("Single quote: \'A\'\n");
    
    // ASCII table sample
    printf("\nASCII sample:\n");
    for (int i = 65; i <= 90; i++) {  // A-Z
        printf("%c ", i);
    }
    printf("\n");
}

/*
 * Example 4: Different Number Bases
 * ---------------------------------
 * Decimal, octal, hexadecimal representation.
 */
void example_number_bases() {
    printf("\n=== Example 4: Number Bases ===\n");
    
    int decimal = 42;       // Base 10
    int octal = 052;        // Base 8 (starts with 0)
    int hex = 0x2A;         // Base 16 (starts with 0x)
    
    printf("All represent the same value:\n");
    printf("Decimal: %d\n", decimal);
    printf("Octal: %d (written as 052)\n", octal);
    printf("Hexadecimal: %d (written as 0x2A)\n", hex);
    
    // Printing in different bases
    int value = 255;
    printf("\n255 in different bases:\n");
    printf("Decimal: %d\n", value);
    printf("Octal: %o\n", value);
    printf("Hex (lowercase): %x\n", value);
    printf("Hex (uppercase): %X\n", value);
    
    // Hexadecimal for colors
    int red = 0xFF0000;
    int green = 0x00FF00;
    int blue = 0x0000FF;
    printf("\nRGB Colors (hex):\n");
    printf("Red: 0x%06X\n", red);
    printf("Green: 0x%06X\n", green);
    printf("Blue: 0x%06X\n", blue);
}

/*
 * Example 5: Type Limits
 * ----------------------
 * Using limits.h and float.h to see type limits.
 */
void example_type_limits() {
    printf("\n=== Example 5: Type Limits ===\n");
    
    // Integer limits
    printf("Integer limits (from limits.h):\n");
    printf("char: %d to %d\n", CHAR_MIN, CHAR_MAX);
    printf("short: %d to %d\n", SHRT_MIN, SHRT_MAX);
    printf("int: %d to %d\n", INT_MIN, INT_MAX);
    printf("long: %ld to %ld\n", LONG_MIN, LONG_MAX);
    printf("long long: %lld to %lld\n", LLONG_MIN, LLONG_MAX);
    
    printf("\nUnsigned limits:\n");
    printf("unsigned char max: %u\n", UCHAR_MAX);
    printf("unsigned short max: %u\n", USHRT_MAX);
    printf("unsigned int max: %u\n", UINT_MAX);
    printf("unsigned long max: %lu\n", ULONG_MAX);
    
    // Floating-point limits
    printf("\nFloating-point limits (from float.h):\n");
    printf("float: %e to %e\n", FLT_MIN, FLT_MAX);
    printf("double: %e to %e\n", DBL_MIN, DBL_MAX);
    printf("float precision: %d digits\n", FLT_DIG);
    printf("double precision: %d digits\n", DBL_DIG);
}

/*
 * Example 6: sizeof Operator
 * --------------------------
 * Using sizeof to determine type sizes.
 */
void example_sizeof() {
    printf("\n=== Example 6: sizeof Operator ===\n");
    
    printf("Sizes of fundamental types:\n");
    printf("sizeof(char)      = %zu byte\n", sizeof(char));
    printf("sizeof(short)     = %zu bytes\n", sizeof(short));
    printf("sizeof(int)       = %zu bytes\n", sizeof(int));
    printf("sizeof(long)      = %zu bytes\n", sizeof(long));
    printf("sizeof(long long) = %zu bytes\n", sizeof(long long));
    printf("sizeof(float)     = %zu bytes\n", sizeof(float));
    printf("sizeof(double)    = %zu bytes\n", sizeof(double));
    printf("sizeof(void*)     = %zu bytes\n", sizeof(void*));
    
    // sizeof with expressions
    int x = 10;
    double y = 3.14;
    printf("\nsizeof with variables:\n");
    printf("sizeof(x) = %zu bytes\n", sizeof(x));
    printf("sizeof(y) = %zu bytes\n", sizeof(y));
    printf("sizeof(x + y) = %zu bytes (promoted to double)\n", sizeof(x + y));
    
    // sizeof with arrays
    int arr[10];
    printf("\nsizeof with arrays:\n");
    printf("sizeof(arr) = %zu bytes\n", sizeof(arr));
    printf("Number of elements = %zu\n", sizeof(arr) / sizeof(arr[0]));
}

/*
 * Example 7: Fixed-Width Types (C99)
 * ----------------------------------
 * Types with guaranteed sizes from stdint.h
 */
void example_fixed_width_types() {
    printf("\n=== Example 7: Fixed-Width Types ===\n");
    
    // Exact-width types
    int8_t   i8  = 127;
    int16_t  i16 = 32767;
    int32_t  i32 = 2147483647;
    int64_t  i64 = 9223372036854775807LL;
    
    uint8_t  u8  = 255;
    uint16_t u16 = 65535;
    uint32_t u32 = 4294967295U;
    uint64_t u64 = 18446744073709551615ULL;
    
    printf("Signed exact-width types:\n");
    printf("int8_t: %d (size: %zu)\n", i8, sizeof(int8_t));
    printf("int16_t: %d (size: %zu)\n", i16, sizeof(int16_t));
    printf("int32_t: %d (size: %zu)\n", i32, sizeof(int32_t));
    printf("int64_t: %lld (size: %zu)\n", (long long)i64, sizeof(int64_t));
    
    printf("\nUnsigned exact-width types:\n");
    printf("uint8_t: %u (size: %zu)\n", u8, sizeof(uint8_t));
    printf("uint16_t: %u (size: %zu)\n", u16, sizeof(uint16_t));
    printf("uint32_t: %u (size: %zu)\n", u32, sizeof(uint32_t));
    printf("uint64_t: %llu (size: %zu)\n", (unsigned long long)u64, sizeof(uint64_t));
    
    // size_t for sizes
    size_t size = sizeof(int);
    printf("\nsize_t: %zu (size: %zu bytes)\n", size, sizeof(size_t));
}

/*
 * Example 8: Boolean Type (C99)
 * -----------------------------
 * Using stdbool.h for boolean values.
 */
void example_bool_type() {
    printf("\n=== Example 8: Boolean Type ===\n");
    
    // Using stdbool.h (C99)
    bool isTrue = true;
    bool isFalse = false;
    
    printf("bool true: %d\n", isTrue);
    printf("bool false: %d\n", isFalse);
    printf("sizeof(bool) = %zu byte\n", sizeof(bool));
    
    // Boolean expressions
    int a = 10, b = 20;
    bool isGreater = a > b;
    bool isEqual = a == b;
    bool isLess = a < b;
    
    printf("\na = %d, b = %d\n", a, b);
    printf("a > b: %d\n", isGreater);
    printf("a == b: %d\n", isEqual);
    printf("a < b: %d\n", isLess);
    
    // Traditional C approach (before C99)
    printf("\nTraditional C (using int):\n");
    int traditional_true = 1;
    int traditional_false = 0;
    printf("true: %d, false: %d\n", traditional_true, traditional_false);
}

/*
 * Example 9: Type Overflow
 * ------------------------
 * Demonstrating what happens when values exceed type limits.
 */
void example_overflow() {
    printf("\n=== Example 9: Type Overflow ===\n");
    
    // Signed overflow (undefined behavior, but usually wraps)
    printf("Signed overflow:\n");
    int maxInt = INT_MAX;
    printf("INT_MAX = %d\n", maxInt);
    printf("INT_MAX + 1 = %d (wrapped to negative!)\n", maxInt + 1);
    
    // Unsigned overflow (well-defined, wraps around)
    printf("\nUnsigned overflow (well-defined):\n");
    unsigned char maxChar = 255;
    printf("unsigned char max = %u\n", maxChar);
    maxChar = maxChar + 1;
    printf("After +1: %u (wrapped to 0)\n", maxChar);
    
    unsigned int maxUint = UINT_MAX;
    printf("UINT_MAX = %u\n", maxUint);
    printf("UINT_MAX + 1 = %u (wrapped to 0)\n", maxUint + 1);
    
    // Underflow
    printf("\nUnderflow:\n");
    unsigned int zero = 0;
    printf("0 - 1 (unsigned) = %u (wrapped to max!)\n", zero - 1);
}

/*
 * Example 10: Format Specifiers
 * -----------------------------
 * Using correct format specifiers for each type.
 */
void example_format_specifiers() {
    printf("\n=== Example 10: Format Specifiers ===\n");
    
    // Integer formats
    int i = 42;
    printf("%%d: %d\n", i);
    printf("%%i: %i\n", i);
    printf("%%o: %o (octal)\n", i);
    printf("%%x: %x (hex lowercase)\n", i);
    printf("%%X: %X (hex uppercase)\n", i);
    
    // Unsigned
    unsigned int u = 42;
    printf("%%u: %u\n", u);
    
    // Long types
    long l = 1000000;
    long long ll = 1000000000000LL;
    printf("%%ld: %ld\n", l);
    printf("%%lld: %lld\n", ll);
    
    // Floating-point formats
    double d = 1234.56789;
    printf("\n%%f: %f\n", d);
    printf("%%.2f: %.2f\n", d);
    printf("%%e: %e\n", d);
    printf("%%E: %E\n", d);
    printf("%%g: %g\n", d);
    
    // Width and alignment
    printf("\nWidth and alignment:\n");
    printf("[%10d] right-aligned, width 10\n", i);
    printf("[%-10d] left-aligned, width 10\n", i);
    printf("[%010d] zero-padded, width 10\n", i);
    printf("[%+d] show positive sign\n", i);
    printf("[% d] space for positive\n", i);
    
    // Character and string
    char c = 'A';
    char str[] = "Hello";
    printf("\n%%c: %c\n", c);
    printf("%%s: %s\n", str);
    printf("%%10s: [%10s] right-aligned\n", str);
    printf("%%-10s: [%-10s] left-aligned\n", str);
}

/*
 * Main Function
 */
int main() {
    printf("╔════════════════════════════════════════════════╗\n");
    printf("║    DATA TYPES IN C - EXAMPLES                  ║\n");
    printf("║    Understanding C's fundamental types         ║\n");
    printf("╚════════════════════════════════════════════════╝\n");
    
    example_integer_types();
    example_floating_types();
    example_char_type();
    example_number_bases();
    example_type_limits();
    example_sizeof();
    example_fixed_width_types();
    example_bool_type();
    example_overflow();
    example_format_specifiers();
    
    printf("\n=== All Examples Completed! ===\n");
    
    return 0;
}
Examples - C Programming Tutorial | DeepML