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:
- Writing: Creating the source code (hello.c).
-
Compiling: Using a compiler (e.g., GCC) to
translate C code into object code (machine code).
Key Point: Compilers translate the entire program at once. (Contrast with Interpreters, which translate and execute line-by-line). - Linking: The linker combines your object code with code from the C Standard Library (e.g., printf) to create a single executable file (a.out or hello.exe).
- Executing: Running the executable file.
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:
- Structured Language: Code is broken into functions (blocks), making it organized and manageable.
- Programmer's Language: Gives the programmer a lot of control over the hardware (memory, CPU). "It trusts the programmer."
Why It's Still Relevant Today:
- Foundation for modern languages (C++, C#, Java).
- Used in Operating Systems, Embedded Systems, Game Development, Databases.
- It's fast and efficient.
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:
-
#include <stdio.h>
: Includes the Standard Input Output header file. Why? To use printf. -
int main()
: The essential function where execution begins. -
{ }
: Delineates the code block for the main function. -
printf
: A function from the standard library to print text. -
return 0;
: Indicates the program ended successfully.
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:
- int: For integers (e.g., 10, -5, 3000).
- char: For single characters (e.g., 'A', '$', '5').
- float: For single-precision floating-point numbers (e.g., 3.14, -0.001).
- double: For double-precision floating-point numbers (more decimal places).
Topic 6: Modifying the Basic Types & Type Qualifiers
Modifiers: Change the size/range of a basic type.
-
short
,long
(e.g.,short int
,long double
) -
signed
(can be + or -, default) vs.unsigned
(only 0 and +).
Qualifiers: Add special properties.
-
const
: The value is constant and cannot be changed after initialization.
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):
- Can contain letters, digits, and underscores (_).
- Must begin with a letter or underscore.
-
Case-sensitive (
age
vs.Age
are different). -
Cannot be a keyword (e.g.,
int
,return
).
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.
-
Using
const
keyword:const float PI = 3.14159;
-
Using
#define
preprocessor:#define PI 3.14159
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:
-
Declaration alone: The compiler knows the type of
the variable (in this case,
int
), but no value is assigned to it. - Purpose: Just letting the compiler know about the variable.
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:
- Initialization gives a variable its first value.
- Declaring and initializing together is common and simplifies code.
- If you don't initialize a variable, its value is garbage (unless it's a static or global variable).
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:
-
The
const
keyword: Used for defining constants with a specific type. The type is still checked by the compiler. -
Scope: A
const
constant can be scoped just like any other variable (local or global).
(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:
-
#define
is not type-safe because it just replaces text, not a variable. It doesn't have a data type (e.g., float, int). -
Global scope: The constant is available throughout
the file after the
#define
statement. - Preprocessor substitution: The value is directly substituted into the code, so there’s no runtime storage for PI (i.e., no memory is allocated).
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:
- Declaration: Tells the compiler the type and name of a variable (but no value).
- Initialization: Assigns a first value to a declared variable.
-
Constants:
-
const
: A constant variable with a type, value can't be changed after initialization. -
#define
: A preprocessor directive for defining a constant, no type is associated with it, and it just replaces the constant with its value before compilation.
-
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:
- C is a compiled, middle-level, structured language.
- Every C program must have a
main()
function. - Variables must be declared with a specific data type before use.
Next Lecture Preview: We will dive deeper into identifiers, Variables, and Basic Programs