Fundamentals of C Programming

Aug 19, 2024

Notes on C Programming Lecture

Introduction to C Programming

  • C is a middle-level programming language, first developed in the 1970s.
  • It acts as a bridge between high-level programming languages and low-level machine code.
  • Many programming languages are influenced by C.

Key Characteristics of C

  • C is procedural and not object-oriented.
  • Important features introduced by C: variables, data types, loops, arrays, and functions.
  • Used widely in systems programming, operating systems, and embedded systems.

Setting Up Environment

  1. Install an IDE: Recommended VS Code.
  2. Install GCC: GNU Compiler Collection to compile C code.
  3. Create a new C file with the .c extension.
  4. Configure the build task in VS Code to compile the code.

Basic Structure of a C Program

  • The main function is the entry point of a C program.
  • Use #include <stdio.h> for input/output functions.
  • Use printf for displaying output to the console.

Writing and Running a Simple Program

  • Example: Print "I like pizza".
  • Use return 0; to signify successful execution.

Variables and Data Types

  • Variables: Store data in memory.
  • Data Types:
    • int: for integers.
    • float: for floating-point numbers (decimals).
    • char: for characters.

User Input

  • Use scanf to accept user input.
  • Handle strings with fgets for inputs including spaces.

Control Structures

  • If Statements: For decision-making (conditions).
  • Loops: for, while, and do while loops for repeated execution.

Functions

  • Functions are reusable blocks of code.
  • Can accept parameters and return values.
  • Use void for functions that do not return a value.

Pointers

  • Pointers hold memory addresses of other variables.
  • Use * to declare a pointer and & to get the address of a variable.
  • Use * again to dereference a pointer to access the value stored at that address.

File Handling

  • Use fopen to open files and fclose to close them.
  • Use fprintf to write to files and fgets to read from files.

Arrays

  • Arrays store multiple values of the same data type.
  • Can be one-dimensional or multi-dimensional.

Structs and Enums

  • Structs: Custom data types that group related variables.
  • Enums: User-defined types that assign names to integral constants.

Final Project: Tic-Tac-Toe Game

  1. Create a 2D array to represent the game board.
  2. Implement player and computer moves.
  3. Check for winning conditions and display the result.
  4. Use functions to modularize code.
  5. Option to play again after the game ends.

Conclusion

  • This lecture covered the fundamentals of C programming, including basic syntax, control structures, functions, pointers, file handling, arrays, structs, and enums.
  • Practical examples demonstrate how to implement these concepts, culminating in a simple game of tic-tac-toe.