1 / 18

Session 5: Input/Output Mastery

💬 Teaching Your Program to Have Conversations

Today we learn how to make our programs interactive and responsive!

From one-way messages to two-way conversations

✨ Let's make programs that listen and respond! ✨

🎯 Today's Communication Mission

💬

Master scanf()
Get user input like a pro

🎨

Advanced printf()
Beautiful formatting tricks

Input Validation
Handle errors gracefully

🔄

Interactive Programs
Real conversations with code

🛡️

User-Friendly Design
Programs people love to use

🎮

Build Applications
Your first interactive tools

📚 Variables Memory Check!

🔢 int

Whole numbers like 42, -10, 0

%d

🌊 float

Decimal numbers like 3.14, -5.7

%f

🔤 char

Single letters like 'A', 'x', '5'

%c

💭 Quick Question

Which format specifier displays whole numbers?

Think about it...

🏠 Homework Detective Report!

Show and Tell Time! 🎉

Challenge 1: Did you declare 3 variables with your personal info?

Challenge 2: Could you use printf to display all your variable values?

Challenge 3: Any cool discoveries with format specifiers?

Bonus: Did you experiment with different data types?

🌟 Let's see your amazing variable programs!

🤔 The One-Way Conversation Problem

💻 Computer: "Hello! I can talk to you!"

💻 Computer: "I'm going to tell you my age: 25"

💻 Computer: "Goodbye!"

😢 You: "But I wanted to tell you MY age!"

🔄 Today we fix this! Time for REAL conversations!

🎤 Meet scanf() - Your Input Superhero!

🦸‍♂️

📥 Input Power

Reads what the user types in real-time

🏷️ Smart Storage

Puts input directly into variables

🎯 Format Awareness

Knows numbers from letters automatically

⚡ Real-Time

Gets input exactly when your program needs it

scanf() = "scan formatted" - scans and formats user input like magic!

📝 scanf() Basic Syntax

// Getting a number from user int age; scanf("%d", &age); // Getting a decimal number float height; scanf("%f", &height); // Getting a single character char grade; scanf(" %c", &grade); // Note the space!
🔑 The Magic & Symbol

&variable

"Give me the ADDRESS where to store this data"

Memory Box

"Hey scanf, put the input IN this memory box!"

💻 Let's Build Our First Interactive Program!

#include <stdio.h> int main() { int age; printf("Hi! What's your age? "); scanf("%d", &age); printf("Wow! You are %d years old!\n", age); return 0; }

📺 What happens when you run this:

Program: Hi! What's your age?

You type: 13

Program: Wow! You are 13 years old!

🎮 Interactive Challenge Time!

🎯 Code Building Challenge

Let's write a program together that asks for your name and favorite number!

Step 1: Declare variables for name and number
Step 2: Ask user for their name
Step 3: Ask user for their favorite number
Step 4: Display a personalized message

🚀 Let's code this together step by step!

🎨 Advanced printf() Formatting Magic!

int score = 95; float average = 87.456; printf("Score: %5d\n", score); // Width of 5 printf("Average: %.2f\n", average); // 2 decimal places printf("Padded: %08d\n", score); // Pad with zeros

✨ Beautiful Output:

Score: 95
Average: 87.46
Padded: 00000095

📋 Format Specifier Cheat Sheet

%d

Integer

printf("%d", 42);

Perfect for whole numbers!

%f

Float (6 decimals)

printf("%f", 3.14);

Shows: 3.140000

%.2f

Float (2 decimals)

printf("%.2f", 3.14);

Shows: 3.14

%c

Single character

printf("%c", 'A');

Shows: A

%5d

Integer with width 5

printf("%5d", 42);

Shows: 42

%08d

Padded with zeros

printf("%08d", 42);

Shows: 00000042

🔢 Multiple Input Challenge!

#include <stdio.h> int main() { int num1, num2, sum; printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); sum = num1 + num2; printf("%d + %d = %d\n", num1, num2, sum); return 0; }

📺 Try this yourself!

Program: Enter two numbers:

You type: 15 27

Program: 15 + 27 = 42

🚨 Common scanf() Traps to Avoid!

❌ Forgetting the & symbol

scanf("%d", age);

Should be:

scanf("%d", &age);

❌ Wrong format specifier

scanf("%f", &age);

Should be:

scanf("%d", &age);

❌ Character input without space

scanf("%c", &grade);

Should be:

scanf(" %c", &grade);

✅ Pro Tip!

Always add a space before %c to catch leftover newlines from previous inputs!

🧠 Quick scanf() Quiz!

Which scanf() statement is correct for getting a float?

A)

scanf("%d", &price);

B)

scanf("%f", &price);

C)

scanf("%c", &price);

D)

scanf("%f", price);

Click your answer!

😊 Making User-Friendly Programs

✅ Clear Prompts

printf("Enter your age: ");

Better than just: scanf("%d", &age);

✅ Helpful Instructions

printf("Enter two numbers (separated by space): ");

Users know exactly what to do!

✅ Confirmation Messages

printf("Got it! You entered: %d\n", age);

Let users know their input was received!

✅ Meaningful Output

printf("Your age in dog years: %d\n", age * 7);

Make the output interesting and fun!

🧮 Your Turn: Build a Simple Calculator!

💡 Challenge: Personal Calculator

Create a program that:

1. Asks user for their name
2. Asks for two numbers
3. Calculates sum, difference, and product
4. Displays results with the user's name

🎯 Example Output:

Program: Hi! What's your name?

You: Rahul

Program: Hello Rahul! Enter two numbers:

You: 10 5

Program: Rahul's Math Results:

10 + 5 = 15

10 - 5 = 5

10 × 5 = 50

🏆 Look What You Mastered Today!

🎤 scanf() Input Master
🎨 printf() Format Artist
💬 Interactive Program Builder
🛡️ Input Validation Expert
🧮 Calculator Creator
😊 User-Friendly Designer

Your programs can now have real conversations! 🎉

You've transformed from static output to dynamic interaction!

🏠 Your Interactive Programming Mission!

📝 This Week's Challenges:

  • 🧮 Complete the Personal Calculator - Make it beautiful with formatted output and user's name
  • 🎮 Create an "About Me" Interview Program - Ask 5 questions and display a profile
  • 🔢 Build a "Favorite Numbers" Program - Ask for 3 numbers and show their average
  • 🎨 printf() Formatting Experiments - Try different width and precision settings
  • 💭 Think Challenge: What would you like your program to ask users about?

🚀 Next Week: Session 6

Arithmetic Operations & Math Magic

We'll make computers solve complex math problems automatically!

🤔 Think About This Week:

What math problems would you like your program to solve for you?

🎊 Awesome work! Your programs are now interactive! 🎊