Day 2: Introduction to C Programming#
Overview#
Now that we have our environment set up, let’s dive into the fundamentals of C programming. We’ll learn the structure of a C program, how it works, and how to use basic input/output functions.
What We’ll Learn Today#
- The structure of a C program
- Understanding the main function
- Basic input and output (printf, scanf)
- Writing interactive programs
- Common pitfalls to avoid
The Structure of a C Program#
Every C program follows this basic structure:
#include <stdio.h> // 1. Include libraries
int main() { // 2. Main function
// 3. Variable declarations
int age = 25;
// 4. Program statements
printf("Hello, World!\n");
// 5. Return statement
return 0;
}Breaking It Down#
1. Preprocessor Directives
#include <stdio.h>#includetells the compiler to include a file<stdio.h>- Standard Input/Output library- Must appear at the top of the file
2. Main Function
int main()- Every C program needs exactly one
main()function - Execution always starts here
intmeans it returns an integer
3. Function Body
{
// Code goes here
}- Curly braces
{}define the function body - Code is executed sequentially
4. Return Statement
return 0;- Ends the program
0indicates successful execution- Non-zero values indicate errors
Basic Output: printf()#
printf() is used to print text and values to the console.
Simple Text Output#
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}Output:
Hello, World!Common Escape Sequences#
| Sequence | Meaning |
|---|---|
\n | Newline |
\t | Tab (spaces) |
\\ | Backslash |
\" | Double quote |
\' | Single quote |
Printing Variables#
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char initial = 'J';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Initial: %c\n", initial);
return 0;
}Output:
Age: 25
Height: 5.9
Initial: JFormat Specifiers#
| Specifier | Data Type | Example |
|---|---|---|
%d | int | 42 |
%f | float/double | 3.14 |
%.2f | float (2 decimals) | 3.14 |
%c | char | ‘A’ |
%s | string | “Hello” |
%x | hexadecimal | 1A |
Basic Input: scanf()#
scanf() is used to read input from the user.
Syntax#
scanf("format", &variable);Important: Notice the & before the variable name! This is very important for pointers (we’ll learn more later).
Example: Reading an Integer#
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d\n", number);
return 0;
}Sample Run:
Enter a number: 42
You entered: 42Reading Different Data Types#
#include <stdio.h>
int main() {
int age;
float salary;
char grade;
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your salary: ");
scanf("%f", &salary);
printf("Enter your grade: ");
scanf(" %c", &grade); // Note the space before %c
printf("\nYour Details:\n");
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
return 0;
}Complete Example: Interactive Program#
Let’s write a program that calculates the area of a rectangle:
#include <stdio.h>
int main() {
float length, width, area;
printf("=== Rectangle Area Calculator ===\n");
printf("Enter length: ");
scanf("%f", &length);
printf("Enter width: ");
scanf("%f", &width);
area = length * width;
printf("\n--- Results ---\n");
printf("Length: %.2f\n", length);
printf("Width: %.2f\n", width);
printf("Area: %.2f\n", area);
return 0;
}Sample Run:
=== Rectangle Area Calculator ===
Enter length: 5.5
Enter width: 3.2
--- Results ---
Length: 5.50
Width: 3.20
Area: 17.60Understanding the Program Flow#
START
↓
Print "Enter length: "
↓
Read length from user
↓
Print "Enter width: "
↓
Read width from user
↓
Calculate: area = length × width
↓
Print results
↓
return 0
↓
ENDCommon Mistakes and How to Avoid Them#
Mistake 1: Forgetting the & in scanf()#
// ❌ Wrong
scanf("%d", number);
// ✅ Correct
scanf("%d", &number);Mistake 2: Using Wrong Format Specifier#
// ❌ Wrong
float price = 9.99;
scanf("%d", &price); // Should be %f
// ✅ Correct
scanf("%f", &price);Mistake 3: Not Initializing Variables#
// ❌ Can produce garbage values
int x;
printf("%d\n", x);
// ✅ Always initialize
int x = 0;
printf("%d\n", x);Mistake 4: Forgetting newlines#
// ❌ Output runs together
printf("Name: ");
printf("Age: ");
// ✅ Better formatting
printf("Name: \n");
printf("Age: \n");Practice Exercises#
Exercise 1: Temperature Converter#
Write a program that:
- Asks user for temperature in Celsius
- Converts to Fahrenheit using: F = (C × 9/5) + 32
- Prints the result
Exercise 2: Simple Grading System#
Write a program that:
- Takes 3 test scores
- Calculates the average
- Prints the average
Exercise 3: Personal Information#
Write a program that:
- Asks for name (use a string - we’ll learn more tomorrow)
- Asks for age
- Asks for favorite number
- Prints all information in a nice format
Summary#
✅ Learned C program structure
✅ Understood main() function
✅ Used printf() for output
✅ Used scanf() for input
✅ Created interactive programs
✅ Learned common format specifiers
Key Points to Remember#
- Every C program starts with
main() - Use
#include <stdio.h>for input/output - Always use
&withscanf()(we’ll understand why later) - Match format specifiers to data types
- Test your programs with different inputs
Next Steps#
Tomorrow we’ll learn about data types in detail, how to declare variables, and work with constants. We’ll also explore different ways to store and manipulate data!