Fundamentals of C Programming for Beginners

Variables and Constants

Variables can be classified as global or local, each with distinct scopes and lifetimes.

Scope

  • Local Variable: Accessible only within the block where it is declared (e.g., inside {}).
  • Global Variable: Accessible throughout the entire program. To use across files, declare with extern (e.g., extern int value;).

Lifetime

  • Local Variable: Created upon entering its scope and destroyed upon exiting.
  • Global Variable: Exists for the duration of the program's execution.

Constants in C come in four forms:

  1. Literal Constants: Direct values like numbers (42), characters ('x'), or strings ("text").
  2. Const Variables: Variables made read-only using the const qualifier.
int main() {
    const int fixed = 50;
    // fixed = 60; // This line would cause a compilation error
    printf("Value: %d", fixed);
    return 0;
}
  1. Macro Constants: Defined using #define preprocessor directive.
#define LIMIT 100
int main() {
    int counter = LIMIT;
    printf("Counter: %d", counter); // Outputs 100
    return 0;
}
  1. Enumeration Constants: Defined within an enum to represent a set of named integer constants.
enum Direction { NORTH, EAST, SOUTH, WEST };
int main() {
    enum Direction heading = EAST;
    printf("Heading code: %d", heading); // Outputs 1
    return 0;
}

Strings, Escape Sequences, and Comments

Strings

A string is a sequence of characters terminated by a null character \0. The strlen() function calculates the length excluding the null terminator.

#include <string.h>
int main() {
    char str1[] = "Hello"; // Automatically includes '\0'
    char str2[] = {'H', 'e', 'l', 'l', 'o', '\0'};
    printf("Length: %zu", strlen(str1)); // Outputs 5
    return 0;
}

Escape Sequences

Special character combinations that represent non-printable characters or change interpretation.

int main() {
    printf("Path: C:\\Users\\Doc\\file.txt\n"); // Prints backslashes
    printf("Tab\tSeparated\n"); // Inserts a tab
    printf("Octal: \101\n"); // Prints 'A' (ASCII 65 in octal)
    printf("Hex: \x42\n"); // Prints 'B' (ASCII 66 in hexadecimal)
    return 0;
}

Comments

  • Multi-line: /* comment text */
  • Single-line: // comment text

Control Flow: Selection and Loops

Selection with if-else

int main() {
    int choice = 0;
    printf("Enter 1 to continue, 0 to exit: ");
    scanf("%d", &choice);
    if (choice == 1) {
        printf("Proceeding...\n");
    } else {
        printf("Exiting.\n");
    }
    return 0;
}

Loops with while

int main() {
    int attempts = 0;
    while (attempts < 5) {
        printf("Attempt #%d\n", attempts + 1);
        attempts++;
    }
    printf("Loop finished.\n");
    return 0;
}

Functions and Arrays

Functions

Functions encapsulate reusable code blocks.

int calculateSum(int x, int y) {
    return x + y;
}
int main() {
    int a = 10, b = 20;
    int result = calculateSum(a, b);
    printf("Sum: %d", result); // Outputs 30
    return 0;
}

Arrays

Arrays store collections of elements of the same type, indexed from 0.

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};
    for (int idx = 0; idx < 5; idx++) {
        printf("Element %d: %d\n", idx, numbers[idx]);
    }
    return 0;
}

Basic Operators

C provides various operators for arithmetic, comparison, assignment, and logical operations, which are fundamental for building expressions and controlilng program logic.

Tags: c programming Variables Control Flow functions Arrays

Posted on Tue, 16 Jun 2026 17:51:20 +0000 by funkdrm