Introduction to Writing Code in C

Jul 17, 2024

Introduction to Writing Code in C

Introduction

  • Instructor welcomes everyone and introduces the topic: getting started with C programming.
  • Encourages audience to like, comment, and subscribe.

Why Learn C?

  • C is a middle-level language, originating in the 1970s.
  • It's one of the most widely used programming languages.
  • Middle-Level Language: Acts as a bridge between high-level (easier, more abstract) and low-level languages (efficient, closer to machine code).
  • Advantages:
    • Efficient and fast.
    • Influenced many modern languages.
    • Used in areas like compilers, kernels, operating systems.
    • Deeper understanding of how other languages operate.
  • Applications: Databases, self-driving cars, operating systems, embedded systems.

Things to Note

  • C is procedural, not object-oriented.
  • C++ is an extension of C and is object-oriented.
  • C is challenging for beginners but doable.

Tools Needed

  • IDE (Integrated Development Environment): VS Code recommended.
  • GNU Compiler Collection (GCC): Converts C code to machine code.

Setting Up Your Environment

Downloading VS Code

  1. Visit code.visualstudio.com.
  2. Select the appropriate version for your operating system.
  3. Follow installer steps:
    • Accept license agreement.
    • Create desktop icon and add to PATH (optional).
    • Install and launch.

Extensions Needed in VS Code

  • C/C++ extension: Provides IntelliSense and other features.
  • Code Runner extension: Helps run code.

Create a C Project Folder

  1. Create a folder on the desktop (e.g., C Files).
  2. Add the folder to VS Code.
  3. Create a new file with .c extension.

Installing GCC Compiler

  • Check for GCC installation:
    • Open Command Prompt (Windows): g++ --version.
    • macOS: clang --version (install via xcode-select --install if needed).
    • Linux: gcc -v (install via sudo apt-get update and additional commands).
  • Install GCC on Windows:
    • Google mingw-w64-install.exe and download from SourceForge.
    • Follow the installation steps.
    • Add GCC to PATH via Environment Variables in Control Panel.

Configure Build Task in VS Code

  1. Go to Terminal > Configure Default Build Task.
  2. Select the task to create a tasks.json file.

Writing Your First C Program

Basic Program Structure

  • Include libraries: #include <stdio.h> (for input/output functions).
  • Main function: Entry point of the program.
    int main() {
        return 0;
    }
    
  • Return Statement: Indicates whether the program ran successfully.
    • return 0; for success, return 1; for error.

Writing and Running Code

  • Use printf to print to console.
    printf("I like pizza");
    
  • Add new line with \n.
    printf("I like pizza\nIt's really good");
    
  • Running code: Right-click to Run Code or use the Run button in VS Code.
  • Error Handling: Example of a typo leading to error.
    print("This will cause an error");
    

Conclusion

  • Recap of writing the first C program.
  • Teaser for the next video on escape sequences and comments.
  • Encouragement to like, comment, and subscribe.