Fundamentals of C Programming

Sep 11, 2024

Lecture Notes: Introduction to C Programming

Overview

  • Introduction to C programming language
    • C is one of the oldest programming languages
    • Many modern languages are based on C (e.g., C++)
    • The course will cover everything needed to get started with C

Course Content

  • Installing a text editor and C compiler
  • Writing basic code in C
  • Understanding programs and how they work
  • Advanced topics
    • If statements and loops
    • Variables and data types
    • Structures and functions
    • Pointers

Getting Started with C

Setting Up a Development Environment

  • Need an environment to write C programs: a text editor or an IDE
  • IDE (Integrated Development Environment) makes writing C programs easier
  • Recommended IDE: Code Blocks

Installing Code Blocks (Windows)

  • Go to codeblocks.org
  • Download the binary release suitable for your OS
  • Install Code Blocks with minGW setup
    • It includes both the IDE and a C compiler
  • Set GNU GCC compiler as default

Installing Code Blocks (OS X)

  • Use Terminal to check if a C compiler is already installed
  • If not, use xcode-select --install
  • Download Code Blocks from codeblocks.org
  • Move Code Blocks to Applications folder

Writing Your First C Program

Creating a New Project in Code Blocks

  • Start Code Blocks and create a new project
  • Choose Console application and C language
  • Create project with a suitable title
  • Default code ("Hello World") is generated

Running Your Program

  • Build and run your program using the IDE
  • Understand the role of printf in outputting text

Programming Concepts

Variables

  • Containers for storing data (e.g., numbers, text, characters)
  • Example: storing a story character’s name and age using variables
  • Benefits of using variables include easy modification and management

Data Types

  • Different types of data that can be stored in C
    • Integer (int)
    • Double (decimal numbers)
    • Character (char)
    • Strings (array of characters)

More on printf

  • printf syntax and special characters
  • Format specifiers: %d, %s, %f, %c
  • Printing variables with printf

Working with Numbers

  • Performing basic arithmetic operations
  • Interaction between integers and floating-point numbers
  • Using math functions like pow, sqrt, ceil, floor

Comments and Constants

Comments

  • Used to explain code or temporarily disable code

Constants

  • Variables that cannot be modified
  • Use const keyword

User Input

Getting Input from Users

  • Using scanf to get input
  • Example: getting age, GPA, grade from user

Building a Basic Calculator

  • Example: calculator that adds two numbers

Advanced Topics

Control Structures

If Statements

  • Allow decision making in programs
  • Example: max function comparing two numbers

Switch Statements

  • Comparison of one value to multiple values
  • Example: responding to test grades

Data Structures

Structs

  • Custom data structures to store grouped data
  • Example: modeling a student

Loops

While Loops

  • Execute code repeatedly while a condition is true
  • Example: guessing game with a secret number

For Loops

  • Use an indexing variable to control loop execution
  • Example: printing elements of an array

Nested Loops

  • Loop inside another loop
  • Example: printing 2D arrays

Pointers and Memory

Memory Addresses

  • Every variable has a memory address
  • Memory addresses can be accessed using pointers

Pointers

  • Variables that store memory addresses
  • Declaring pointers and dereferencing them

Using Files

Writing to Files

  • Create, overwrite, append files

Reading from Files

  • Using fgets to read lines from a file

Conclusion

  • This course provides a comprehensive foundation in C programming
  • Encouraged to explore and practice with the provided examples