1 / 18
๐ Welcome Back, Programmer! ๐
Session 4: Variables and Data Types
Ready to teach computers to remember? ๐ง
Today we learn how to store information in our programs!
โจ Let's give our programs memory! โจ
๐ Structure & Syntax Homework Review! ๐
๐ค
Show us your "About Me" program!
๐จ
What creative outputs did you make?
๐ง
Did you practice escape sequences (\n, \t)?
๐
What bugs did you create and fix?
โก Quick Structure Quiz! โก
Let's test your Session 3 memory!
๐ What does #include <stdio.h> do?
๐ช Where does every C program start?
๐ฏ What does \n do in printf?
โ๏ธ What punctuation ends every statement?
๐ฌ How do you write comments in C?
๐ฏ Today's Epic Mission! ๐ฏ
๐ฆ Learn what variables are and why we need them
๐ญ Discover the main data types: int, float, char
๐ง Understand how computer memory works
๐ Learn to declare and initialize variables
๐ท๏ธ Master variable naming rules
๐จ Create programs that remember information
From static text to dynamic programs!
๐ค The Problem with Our Current Programs ๐ค
printf("My age is 13");
printf("My height is 5.2 feet");
printf("My grade is A");
What if we want to:
๐ Update our age when we have a birthday?
๐ Change our height as we grow?
๐ Calculate our grade average?
๐ Use the same information multiple times?
We need a way to store and change information!
๐ฆ What Are Variables? ๐ฆ
Think of variables as labeled boxes that store information!
๐ท๏ธ Named Storage
Each variable has a meaningful name
๐ Changeable
Values can be updated anytime
๐ง How Computer Memory Works ๐ง
Computer memory is like a giant apartment building with numbered addresses!
Memory Address: 1000
๐ฆ
Variable: age
Value: 13
Memory Address: 1004
๐ฆ
Variable: height
Value: 5.2
The computer knows exactly where to find each piece of information!
๐ญ Meet the Big Three Data Types! ๐ญ
Just like we organize different types of items, C organizes different types of data!
๐ข
int
Whole Numbers
13, 42, -7, 0, 1000
%d
๐
float
Decimal Numbers
5.2, 3.14, -2.5, 0.1
%f
๐ค
char
Single Characters
'A', 'z', '5', '@', ' '
%c
๐ How to Create Variables ๐
Declaration Syntax:
datatype variableName;
// Examples:
int age;
float height;
char grade;
Declaration + Initialization:
int age = 13;
float height = 5.2;
char grade = 'A';
๐จ Format Specifiers: Telling printf What to Expect ๐จ
%d
For integers
printf("%d", age);
Output: 13
%f
For floats
printf("%f", height);
Output: 5.200000
%c
For characters
printf("%c", grade);
Output: A
%.1f
Float with 1 decimal
printf("%.1f", height);
Output: 5.2
๐ป Let's Write Our First Variable Program! ๐ป
#include <stdio.h>
int main() {
// Declare and initialize variables
int age = 13;
float height = 5.2;
char grade = 'A';
// Display the information
printf("My age is %d years\n", age);
printf("My height is %.1f feet\n", height);
printf("My grade is %c\n", grade);
return 0;
}
Let's type this together and see the magic happen!
๐ท๏ธ Variable Naming Rules & Best Practices ๐ท๏ธ
โ
Valid Names
- age
- studentGrade
- my_height
- score2
- _temp
โ Invalid Names
- 2age (starts with number)
- my-height (hyphen not allowed)
- student grade (spaces not allowed)
- int (reserved keyword)
- @score (special characters)
๐ฏ Golden Rules:
Start with letter or underscore
Only letters, numbers, and underscores allowed
Case-sensitive (Age โ age)
Use meaningful names (age, not x)
Use camelCase or snake_case
๐ง Quick Variable Quiz! ๐ง
Which variable declaration is CORRECT?
A)
int 2age = 15;
B)
float average = 9.5;
C)
char name = "John";
D)
int my-score = 100;
Type your answer in the chat!
๐ Updating Variable Values ๐
#include <stdio.h>
int main() {
int score = 80; // Initial value
printf("Initial score: %d\n", score);
score = 90; // Update the value
printf("Updated score: %d\n", score);
score = score + 5; // Math with variables
printf("Final score: %d\n", score);
return 0;
}
Variables are called "variable" because they can vary (change)!
๐จ Common Variable Mistakes to Avoid! ๐จ
โ Wrong Quotes for char
char grade = "A";
Fix: Use single quotes
char grade = 'A';
โ Using Without Declaration
score = 100;
Fix: Declare first
int score = 100;
โ Wrong Format Specifier
printf("%d", height);
Fix: Use correct specifier
printf("%f", height);
โ Mixing Data Types
int age = 13.5;
Fix: Use correct type
float age = 13.5;
๐ฏ Your Turn: Personal Info Program! ๐ฏ
Create a program that uses all three data types:
๐ Declare an int variable for your age
๐ Declare a float variable for your height
๐ Declare a char variable for your favorite subject's first letter
๐จ๏ธ Use printf to display all three with proper format specifiers
๐ Update one variable and print it again
// Example structure:
#include <stdio.h>
int main() {
// Your variable declarations here
// Your printf statements here
return 0;
}
๐ Look What You've Mastered! ๐
๐ฆ Understanding Variables
๐ญ Three Data Types
๐ง Memory Concepts
๐ Declaration Syntax
๐จ Format Specifiers
๐ Updating Values
Your programs can now remember and change information! ๐
You've leveled up from static to dynamic programming!
๐ This Week's Variable Adventures! ๐
๐ Practice Assignments:
- ๐ฏ Complete the Personal Info Program - Use int, float, and char
- ๐ Create an "Update Program" - Start with values, then change them
- ๐จ Make a "Family Info Program" - Store info about 3 family members
- ๐งช Experiment Challenge - Try different format specifiers (%.2f, %5d)
- ๐ Bug Hunt - Intentionally make mistakes, then fix them
๐ Next Week: Session 5
Input and Calculations
We'll learn how to ask users for information and do math with variables!
๐ค Think About This Week:
What information would you like your program to ask the user for?
๐ Amazing work! You're now a variable master!