Programming in C - Part 1

Introduction & Context

Part 1: Introduction & Context (20 mins)

Topic 1: What is a Programming Language?

Computer languages are systems of communication used to instruct a computer to perform specific tasks. They consist of syntax (rules) and semantics (meaning) that programmers use to write programs that the computer can execute. These languages bridge the gap between human instructions and machine instructions.

Definition: A formal language comprising a set of instructions that produce various kinds of output. It is used to communicate with a computer.

Analogy: Human languages (English, Spanish) vs. Computer languages (C, Python, Java).

Levels of Languages:

High-Level vs Low-Level Languages

Low-Level Languages:

Definition: These are languages that are closer to machine code (binary) and interact directly with the hardware.

Example: Assembly Language, Machine Code (binary).

Assembly Language is a symbolic representation of machine code that makes it easier for humans to understand and write.

In Assembly, instructions map directly to machine-level operations (e.g., moving data between registers in the CPU).

MOV AX, 1 ; Load the value 1 into register
AX ADD AX, 5 ; Add 5 to the value in AX

Pros: Faster execution, direct control over hardware.

Cons: Difficult to write, maintain, and debug. Hardware-specific.

High-Level Languages:

Definition: These are languages that are more abstract and closer to human languages. They are portable across different hardware systems, meaning the same program can run on different machines with minimal modifications.

Example: C, Python, Java, JavaScript.

int main() {
    int a = 1;
    int b = 5;
    int sum = a + b;
    return 0;
}
      

Pros: Easier to learn and use, faster development, portable across different systems.

Cons: Slower execution compared to low-level languages, less control over hardware.

High-Level (Java, Python): Easier for humans, closer to English. "Write once, run anywhere."

Low-Level (Assembly, Machine Code): Direct instructions to the CPU. Hard for humans, easy for machines.

Introduce C as a "Middle-Level" Language: It combines the best of both—power and efficiency of low-level with the readability and structure of high-level.

Topic 2: The Program Creation Process

The Steps:

Part 2: Overview of the C Language (30 mins)

Topic 3: Why Learn C? (The "Why")

A Brief History: Developed at Bell Labs by Dennis Ritchie (1972). Created to write the UNIX operating system.

Characteristics:

Why It's Still Relevant Today:

Topic 4: The Anatomy of a Simple C Program (The "How")

#include <stdio.h> // Preprocessor Directive
int main() {
// Main Function - program entry point
printf("Hello, World!");
//Library Function
return 0; // Return Statement }

Breakdown of each line:

Hello World Program

The “Hello World” program is the first step towards learning any programming language. It is also one of the simplest programs that is used to introduce aspiring programmers to the programming language. It typically outputs the text "Hello, World!" to the console screen.

C Program to Print "Hello World"

To print the “Hello World”, we can use the printf function from the stdio.h library that prints the given string on the screen. Provide the string "Hello World" to this function as shown in the below code:

// Header file for input output functions
#include <stdio.h>
// Main function: entry point for execution
int main()
{
// Writing print statement to print hello world
printf("Hello World");
return 0;
}

Output

Hello World

Explanation:

  • #include <stdio.h> – This line includes the standard input-output library in the program.
  • int main() – The main function where the execution of the program begins.
  • printf(“Hello, World!\n”); – This function call prints “Hello, World!” followed by a new line.
  • return 0; -This statement indicates that the program ended successfully.

Part 3: The Building Blocks - Data & Expressions (60 mins)

Topic 5: Basic Data Types - The "Containers" for Data

Purpose: To define the type of data a variable can hold (e.g., number, character, decimal).

The Core Types:

Topic 6: Modifying the Basic Types & Type Qualifiers

Modifiers: Change the size/range of a basic type.

Qualifiers: Add special properties.

Topic 7: Variables - Naming and Creating Containers

What is a variable? A named location in memory that stores a value of a specific type.

Identifier Names (Rules):

Good Practices: Use meaningful names (student_count instead of x).

Topic 8: Declaring, Initializing, and Constants

Declaration: Telling the compiler about the variable (int age;).

Initialization: Giving a variable its first value (age = 21; or int score = 95;).

Constants: Values that don't change.

Explanation

1. Declaring a Variable

Declaration is the process of telling the compiler about the type and name of a variable. This is done so the compiler can allocate space in memory for that variable and understand how to use it during execution.

Syntax:

type variable_name;
      

Example:

int age;
      

This declares an integer variable named age, but doesn't give it an initial value. At this point, age is uninitialized, and its value is undefined (i.e., it contains garbage data).

Key Points:

2. Initializing a Variable

Initialization means giving a variable its first value. This can happen at the time of declaration or later in the program.

Syntax:

variable_name = value;
      

Or, when declaring and initializing together:

type variable_name = value;
      

Example:

int age = 21; // Declaration + Initialization
      

Here, age is declared as an int and is also initialized with a value of 21 at the same time.

Alternatively, you could declare the variable first and initialize it later:

int score;   // Declaration
score = 95;  // Initialization
      

Key Points:

3. Constants

Constants are variables whose value cannot change after they are set. In C, constants are used when you need a fixed value throughout the program, such as the value of $π$ or the size of a buffer.

There are two primary ways to define constants in C:

(a) Using the const Keyword

The const keyword allows you to declare a constant variable. When a variable is declared as const, its value cannot be changed after it is initialized. If you try to modify it, the compiler will generate an error.

Syntax:

const type variable_name = value;
      

Example:

const float PI = 3.14159;
      

Here, PI is a constant with the value 3.14159. If you try to later assign a new value to PI, the compiler will generate an error:

PI = 3.14; // Error: PI is read-only.
      

Key Points:

(b) Using the #define Preprocessor Directive

The #define directive is used to create a constant that is replaced by the preprocessor before the code is compiled. It's more like a textual replacement than a variable with a specific type.

Syntax:

#define CONSTANT_NAME value
      

Example:

#define PI 3.14159
      

Here, the preprocessor will replace all occurrences of PI with 3.14159 before the code is actually compiled.

Key Points:

Key Differences Between const and #define for Constants
Feature const #define
Type Safety Yes, type is checked by the compiler. No, it’s a simple text substitution.
Scope Local or global (like any variable). Global (after the #define line).
Memory Usage Allocates memory for the constant. No memory allocation (preprocessor replacement).
Error Checking The compiler checks for illegal assignments. No error checking, just replacement.
Best Use Case When you need type safety and variable-like behavior. When you want simple, type-agnostic constants.
Example Comparison
#include <stdio.h>
// Using #define
#define PI 3.14159
// Using const
const float PI_CONST = 3.14159;
int main() {

// No memory allocated, preprocessor replaces PI with 3.14159

printf("Using #define: PI = %f\n", PI);
// No memory allocated, preprocessor replaces PI with 3.14159
printf("Using const: PI_CONST = %f\n", PI_CONST);
// Allocates memory for PI_CONST and enforces type safety
// Uncommenting the next line will cause a compiler error because PI_CONST is a constant.
// PI_CONST = 3.14;
return 0;
}

With #define: The value of PI is substituted into the code before compilation. It has no type or memory allocated.

With const: PI_CONST is a typed constant (type is checked by the compiler) and uses memory to store the value.

In Summary:

Both const and #define are used to define constants, but const is better for type safety, while #define is useful for simple, type-independent constants.

Topic 9: Introduction to Operators & Expressions

What is an expression? A combination of variables, constants, and operators that evaluates to a single value.

Basic Arithmetic Operators:

+ (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus - remainder).

Example: int result = (10 + 5) * 2; // result is 30

Part 4: Wrap-up & Look Ahead (10 mins)

Summary: Today we covered the journey from problem to executable program, why C is a powerful and foundational language, and the basic vocabulary of C (data types, variables, expressions).

Key Takeaways:

Next Lecture Preview: We will dive deeper into identifiers, Variables, and Basic Programs