C Fundamentals - Data Types, Variables, and Basic I/O

This page covers the absolute basics of C programming: fundamental data types, how to declare and use variables and constants, and basic input/output operations. Each section includes explanations, runnable code snippets, and their expected outputs.

1. Data Types

In C, every variable must have a data type, which specifies the size and type of values it can store. C is a statically-typed language, meaning a variable's type is fixed once declared.

Code Example (Data Types)

Copied!
#include <stdio.h>
#include <stdbool.h> // For bool, true, false

int main() {
    // Integer types
    int age = 30;
    unsigned int population = 8000000000U; // U suffix for unsigned
    long long big_number = 123456789012345LL; // LL suffix for long long

    // Floating-point types
    float temperature = 25.5f; // f suffix for float literal
    double pi = 3.1415926535;
    long double very_precise_pi = 3.14159265358979323846L; // L suffix for long double

    // Character type
    char grade = 'A';
    char newline_char = '\n'; // Escape sequence for newline

    // Boolean type
    bool is_active = true;
    _Bool has_data = 0; // Using _Bool directly

    printf("Age: %d\n", age);
    printf("Population: %u\n", population);
    printf("Big Number: %lld\n", big_number);
    printf("Temperature: %.1f\n", temperature);
    printf("Pi (double): %.10lf\n", pi);
    printf("Very Precise Pi (long double): %.20Lf\n", very_precise_pi);
    printf("Grade: %c\n", grade);
    printf("Newline char (ASCII): %d\n", newline_char); // Prints ASCII value
    printf("Is Active: %d\n", is_active);
    printf("Has Data: %d\n", has_data);

    return 0;
}
            

Expected Output

Age: 30
Population: 8000000000
Big Number: 123456789012345
Temperature: 25.5
Pi (double): 3.1415926535
Very Precise Pi (long double): 3.14159265358979323846
Grade: A
Newline char (ASCII): 10
Is Active: 1
Has Data: 0
            

2. Variables and Constants

Code Example (Variables and Constants)

Copied!
#include <stdio.h>

#define MAX_VALUE 100 // Symbolic constant

int global_var = 50; // Global variable (data segment/BSS)

void function_scope_example() {
    int local_var = 10; // Local variable (stack)
    static int static_var = 0; // Static local variable (data segment/BSS), retains value across calls
    static_var++;
    printf("Inside function: local_var = %d, static_var = %d\n", local_var, static_var);
}

int main() {
    int x = 10; // Variable declaration and initialization (lvalue)
    const int immutable_val = 20; // Constant variable

    printf("x = %d\n", x);
    printf("MAX_VALUE = %d\n", MAX_VALUE);
    printf("immutable_val = %d\n", immutable_val);
    printf("global_var = %d\n", global_var);

    x = 15; // Modifying a variable
    // immutable_val = 25; // Error: cannot assign to read-only variable

    int result = x + 5; // x is lvalue, 5 is rvalue, x+5 is rvalue
    printf("Result of x + 5 = %d\n", result);

    function_scope_example(); // Call function once
    function_scope_example(); // Call function again

    // Volatile example (simplified, usually for hardware interaction)
    volatile int sensor_reading = 0;
    printf("Sensor reading: %d\n", sensor_reading);
    // Imagine an interrupt changes sensor_reading here
    printf("Sensor reading (after potential change): %d\n", sensor_reading); // Compiler won't optimize this read away

    return 0;
}
            

Expected Output

x = 10
MAX_VALUE = 100
immutable_val = 20
global_var = 50
Result of x + 5 = 15
Inside function: local_var = 10, static_var = 1
Inside function: local_var = 10, static_var = 2
Sensor reading: 0
Sensor reading (after potential change): 0
            

(Note: The "after potential change" output for volatile would only show a difference if sensor_reading was actually modified by an external factor between the printf calls in a real-world scenario.)

3. Basic Input/Output (I/O)

How a C program interacts with the user (getting input) and displays information (giving output).

Code Example (Basic I/O)

Copied!
#include <stdio.h>

int main() {
    int num;
    float price;
    char initial;
    char name[20]; // Character array for string

    printf("Enter an integer: ");
    scanf("%d", &num);

    printf("Enter a price (e.g., 19.99): ");
    scanf("%f", &price);

    // Clear input buffer before reading char/string
    while (getchar() != '\n'); // Consume remaining newline character

    printf("Enter your initial: ");
    scanf("%c", &initial);

    // Clear input buffer again
    while (getchar() != '\n');

    printf("Enter your first name: ");
    // scanf("%s", name); // scanf for string stops at whitespace.
    fgets(name, sizeof(name), stdin); // Better for strings with spaces, reads newline

    printf("\nYou entered:\n");
    printf("Integer: %d\n", num);
    printf("Price: %.2f\n", price);
    printf("Initial: %c\n", initial);
    printf("Name: %s", name); // Note: fgets includes newline, so no \n here

    return 0;
}
            

Expected Output

Enter an integer: 123
Enter a price (e.g., 19.99): 45.67
Enter your initial: J
Enter your first name: John Doe

You entered:
Integer: 123
Price: 45.67
Initial: J
Name: John Doe
            

(User input is shown on the same line as the prompt in this example. `fgets` reads the newline character, so `printf` for `Name` does not need `\n`.)