Lecture: Getting Started with C Programming

Jul 1, 2024

Lecture: Getting Started with C Programming 💻

Introduction

  • C is a middle-level programming language from the 1970s.
  • It's widely used: efficient like low-level languages, but more abstract like high-level languages.
  • Influential: many languages are based on C (e.g., Python's default implementation is CPython).
  • Not object-oriented (procedural).

Tools Needed

  1. IDE (Integrated Development Environment)
    • Recommended: VS Code.
  2. GNU Compiler Collection (GCC)
    • Used to compile C code to machine code.

Setting Up VS Code for C Programming

  1. Go to code.visualstudio.com to download VS Code.
  2. Install extensions:
    • C/C++ for intellisense.
    • Code Runner to run code.
  3. Create a C file:
    • Create a new folder for your project.
    • Inside the folder, create a file with a .c extension (e.g., helloworld.c).

Installing GCC

  • Windows: Check if GCC is installed via g++ --version in command prompt.
  • Mac: clang --version. If not installed: xcode-select --install.
  • Linux: gcc -v and install using sudo apt-get install build-essential.

Configure Build Tasks in VS Code

  1. Open VS Code terminal.
  2. Configure default build task: terminal -> configure default build task -> C++ (G++ Task).
  3. This generates a tasks.json file within .vscode that tells VS Code how to compile the program.

Writing Your First C Program

  1. Include necessary headers: #include <stdio.h>.
  2. Write a main function:
    int main() {
        printf("I like pizza\nIt's really good");
        return 0;
    }
    
  3. Compile and run the program using run code or manually via terminal.

Handling Errors in C

  • Ensure proper syntax: missing semicolons or wrong function names will cause errors while compiling.
  • Error example: Misspelling printf as just print.

User Input in C

  • Switch from output tab to terminal to accept user input.
  • Use scanf for simple inputs or fgets for strings that may contain whitespaces.
    // for integers
    int age;
    printf("How old are you?");
    scanf("%d", &age);
    printf("You are %d years old", age);
    
    // for strings with white spaces
    char name[25];
    printf("What is your name?");
    fgets(name, 25, stdin);
    name[strlen(name) - 1] = '\0'; // removes newline
    

Writing and Compiling C Programs

  • Save your C file and run it from the terminal to see outputs or debug errors.

Comments and Escape Sequences

  • Comments: // for single-line, /* */ for multi-line.
  • Escape Sequences: \n for newline, \t for tab, \" for quotes, \\ for backslash.

Using Variables in C

  • Variables must be declared before use.
    • Basic types: int, float, char, arrays, etc.
    • Example: int age = 21;
  • Using format specifiers in printf to display variables.

Logical Operators

  • AND (&&): Checks if multiple conditions are true.
  • OR (||): Checks if at least one condition is true.
  • NOT (!): Reverses the state of a condition.

Functions in C

  • Function declaration and calling.
    void birthday() {
        printf("Happy Birthday!");
    }
    
    int main() {
        birthday(); // calling the function
        return 0;
    }
    
  • Arguments and Parameters: Pass values to functions.
  • Return Statement: Return values from functions.
  • Function prototypes: Ensure calls to functions are made correctly.

Working with Arrays

  • Declaration: int numbers[5];
  • Initialization: int numbers[] = {1, 2, 3, 4, 5};
  • Accessing elements using index numbers starting from 0.
  • Iterating arrays using loops.
  • Multidimensional arrays (e.g., 2D arrays).

Structs in C

  • Creating structs to group related variables under one name.
    struct Player {
        char name[50];
        int score;
    } player1, player2;
    
  • Access struct members using the dot operator.
  • Typedef: Giving a new name to an existing type (e.g., typedef struct Player Player;).

Enumerations

  • Use enum keyword to create a type with named integer constants.
    enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
    enum Day today = MON;
    

Pointers in C

  • Pointers store memory addresses of variables.
  • Declaration: int *p = &var;
  • Dereferencing pointers: *p
  • Useful for passing large variables to functions, dynamic memory allocation, etc.

File Operations

  • Writing to a file: Use `FILE *fp = fopen(