Introduction to C Programming Basics

Sep 17, 2024

C Programming Course Notes

Overview of C Programming

  • C is one of the oldest programming languages.
  • Learning C provides a good foundation for modern languages like C++.
  • The course aims to cover:
    • Installing a text editor and C compiler
    • Writing basic C code
    • Core concepts like statements, loops, variables, functions, and pointers.

Setting Up the C Programming Environment

Required Tools

  1. Text Editor or IDE (Integrated Development Environment)
    • Recommended IDE: Code::Blocks.
  2. C Compiler
    • GNU GCC Compiler included with Code::Blocks.

Installation Steps for Code::Blocks

  1. Search for "Code::Blocks C" in the web browser.
  2. Download the binary release applicable to your OS.
  3. Follow the installation prompts to set up Code::Blocks and GCC Compiler.

Basic Code Structure in C

  • Every C program has a main function.
  • A simple program structure:
    #include <stdio.h>
    int main() {
        printf("Hello, World!\n");
        return 0;
    }
    

Writing Your First C Program

Writing Code

  • The printf function is used to print output to the console.
  • To execute code, the program must be built and run.

Compiling and Running Code

  • Use the "Build and Run" option in Code::Blocks to execute your program.

Basic Concepts in C

Variables

  • Variables are containers for storing data values.
  • Types of variables include integers, doubles, and characters.
  • Syntax for declaring variables:
    int age;
    double gpa;
    char grade;
    

Data Types

  • Basic data types:
    • int: Integer numbers.
    • double: Decimal numbers.
    • char: Single characters.
  • char[] is used for strings (e.g., arrays of characters).

Control Structures

If Statements

  • Used to make decisions based on conditions.
  • Syntax:
    if (condition) {
        // code to execute if true
    } else {
        // code to execute if false
    }
    

Switch Statements

  • A cleaner alternative to multiple if statements for comparing one variable against different values.
  • Syntax:
    switch (variable) {
        case value1:
            // code
            break;
        case value2:
            // code
            break;
        default:
            // code
    }
    

Loops

While Loops

  • Used for repeatedly executing a block of code as long as a condition is true.
  • Syntax:
    while (condition) {
        // code to execute
    }
    

For Loops

  • A more compact way to express a loop, particularly when using an indexing variable.
  • Syntax:
    for (initialization; condition; increment) {
        // code to execute
    }
    

Functions

  • Functions are blocks of code that perform specific tasks.
  • Can accept parameters and return values.
  • Syntax:
    returnType functionName(parameters) {
        // code
        return value;
    }
    

Structs

  • Structs allow grouping of different data types under a single name.
  • Useful for representing complex data structures like a student with attributes (name, age).
  • Syntax:
    struct Student {
        char name[50];
        int age;
        double gpa;
    };
    

Pointers

  • Pointers store memory addresses of other variables.
  • Syntax for declaring a pointer:
    int *ptr;
    
  • Dereferencing pointers to access the value at the memory address:
    printf("%d", *ptr);
    

File I/O

  • Reading and writing files using functions like fopen, fprintf, and fgets.
  • Modes include:
    • r: Read
    • w: Write (overwrites existing files)
    • a: Append (adds to end of file)
  • Example of writing to a file:
    FILE *fp;
    fp = fopen("file.txt", "w");
    fprintf(fp, "Hello, World!");
    fclose(fp);
    
  • Example of reading from a file:
    FILE *fp;
    fp = fopen("file.txt", "r");
    fgets(line, sizeof(line), fp);
    fclose(fp);
    

Summary

  • Following the course will provide a solid foundation in C programming.
  • Key concepts include variables, control structures, functions, structs, pointers, and file I/O.
  • Practice using each concept to solidify understanding.