💻

Basics of C Programming and Setup

Apr 17, 2025

Lecture: Introduction to Programming in C

Importance of Learning C

  • C is a middle-level programming language developed in the 1970s.
  • Bridges the gap between high-level languages (easier to work with, more abstract) and low-level languages (efficient, faster, closer to machine architecture).
  • Influences most programming languages today; foundational concepts include variables, data types, loops, arrays, functions.
  • Used in systems programming, databases, embedded systems.
  • C is procedural, not object-oriented. Unlike C++, which is an extension of C.

Setting Up the Environment

  • Integrated Development Environment (IDE): Recommended to use Visual Studio Code (VS Code).
  • GNU Compiler Collection (GCC): Required to compile C code into machine code.
  • Installation Instructions:
    • Download VS Code from code.visualstudio.com.
    • Install necessary extensions: C/C++ extension (IntelliSense) and Code Runner.
    • Configure the default build task in VS Code for C programming.
    • Verify GCC installation using command g++ --version.

Writing Your First C Program

  • Include Standard Library: Use #include <stdio.h> for input/output functions.
  • Main Function: Entry point of any C program, int main() { ... }.
  • Print to Console: Use printf with format specifiers, e.g., %d for integers.
  • Compile and Run:
    • Use command prompt for Windows or terminal for Mac/Linux.
    • Compile with gcc filename.c -o output and run with ./output.

Basic C Programming Concepts

  • Variables and Data Types:
    • int, float, char, double, bool (include <stdbool.h> for booleans).
    • Declaration followed by initialization, e.g., int age = 21;.
  • Operators:
    • Arithmetic: +, -, *, /, %.
    • Augmented Assignment: +=, -=, etc.
    • Logical: &&, ||, !.
  • Control Structures:
    • If Statements: Conditional execution of code blocks.
    • Switch Statements: Handling multiple conditions.*

Functions and Prototypes

  • Function Declaration: Define reusable code blocks with a return type and parameters, e.g., int add(int a, int b);.
  • Function Prototypes: Declaration of functions before the main() to ensure correct usage.
  • Return Statement: Functions can return values to the calling function.

Working with Arrays and Strings

  • Arrays: Fixed-size collections of elements of the same type, accessed via indices.
  • Strings: Arrays of characters, typically null-terminated.
  • Two-Dimensional Arrays: Arrays of arrays, useful for matrices and grids.

File Handling

  • Reading and Writing Files:
    • Use fopen(), fclose(), fprintf(), fgets() for file operations.
    • Modes: `