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!

age
13
integer
height
5.2
decimal
grade
A
character

๐Ÿท๏ธ 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!