💻

C Programming Basics

Oct 5, 2025,

Overview

This lecture introduces C programming, covering environment setup, core concepts, variables, user input/output, control structures, arrays, functions, pointers, file operations, memory management, and practical projects.

Setting Up the C Development Environment

  • Install an IDE (e.g., Visual Studio Code) and a C compiler (like GCC).
  • Download and configure necessary extensions: C/C++ and CodeRunner.
  • Check compiler installation: gcc --version for Windows/Linux, clang --version for Mac.
  • On Windows, set the compiler path in environment variables.

Writing and Running Your First C Program

  • Start with #include <stdio.h> for input/output functions.
  • Every C program requires a main function as the entry point.
  • Use return 0; in main to indicate successful execution.
  • Use printf() to display output, ending statements with ;.

Comments in C

  • Single-line comments use //.
  • Multi-line comments use /* ... */.
  • Comments are ignored by the compiler.

Variables and Data Types

  • Variables are named containers for values.
  • Common data types: int (whole numbers), float (decimals, 6-7 digits), double (decimals, ~15 digits), char (single character), string (array of char), bool (true/false, needs #include <stdbool.h>).
  • Format specifiers in printf: %d (int), %f (float), %lf (double), %c (char), %s (string), %d (bool as int).

Format Specifiers and Output Formatting

  • Control output width, justification, leading zeros, and precision using modifiers within format specifiers (e.g., %7.2f).

Arithmetic and Assignment Operators

  • Use +, -, *, /, % (modulus) for math operations.
  • ++/-- increment/decrement.
  • +=, -=, *=, /= are augmented assignments.

User Input

  • Use scanf() or fgets() for input; clear input buffer as needed.
  • Strings with spaces need fgets() and buffer size calculation.
  • Always initialize variables to avoid undefined behavior.

Control Structures

  • if, else if, else for decision making; use logical operators (&&, ||, !).
  • switch is preferred over multiple if else for fixed integer or char values.
  • Nested if statements enable multi-level decisions.

Loops

  • while and do-while loops repeat code based on condition.
  • for loops are ideal for known number of iterations.
  • Use break to exit loops, continue to skip current iteration.
  • Nested loops can handle matrices or generate tables.

Arrays and Strings

  • Array declaration: type name[size];.
  • Strings are arrays of char; array of strings is a 2D array or array of pointers.
  • Always initialize arrays to zero or default values.

Functions

  • Functions are reusable code blocks, defined with a return type, name, and parameters.
  • Arguments are values passed to functions; parameters are received inside the function.
  • Use return to send values back; void means no return.
  • Function prototypes declare functions before use for type checking.

Pointers and Memory Management

  • A pointer stores the address of a variable; declare with *.
  • The & operator gets a variable’s address; * dereferences a pointer to get the value.
  • Use malloc(), calloc(), realloc() for dynamic memory allocation; free() to release memory.

Structs, Typedefs, and Enums

  • struct groups related variables; dot . or arrow -> accesses members.
  • typedef creates type aliases for readability.
  • enum creates named integer constants for improved clarity.

File Input/Output

  • Use FILE * pointers and fopen(), fprintf(), fscanf(), fclose() for file operations.
  • Always check if the file opened successfully and close files after use.

Practical Projects Covered

  • Shopping cart, mad libs, weight/temperature converter, calculator, quiz game, number guessing, rock-paper-scissors, digital clock, and banking program.

Key Terms & Definitions

  • IDE — Integrated Development Environment; workspace for coding.
  • Compiler — Translates C code into machine code.
  • Variable — Named data storage.
  • Format Specifier — Symbol in printf/scanf for data type formatting.
  • Pointer — Variable storing a memory address.
  • Struct — Custom data type grouping related variables.
  • Enum — User-defined type of named integer constants.
  • Dynamic Memory — Runtime-allocated memory via malloc, calloc, realloc.

Action Items / Next Steps

  • Practice: Write at least one C program using variables, input/output, control structures, loops, functions, arrays, and pointers.
  • Review: Complete sample projects such as calculator, quiz game, and digital clock.
  • Reading: Review C standard library documentation for string, math, and memory functions.