Defensive programming practices
📖 Scenario: You are writing a small program that calculates the average of three test scores entered by a user. To make sure your program works well even if the user makes mistakes, you will use defensive programming practices.
🎯 Goal: Build a C program that safely reads three test scores, checks if they are valid (between 0 and 100), calculates the average only if all scores are valid, and prints the result.
📋 What You'll Learn
Create three integer variables to store test scores.
Create a helper variable to track if all inputs are valid.
Use if statements to check if each score is between 0 and 100 inclusive.
Calculate the average only if all scores are valid.
Print the average or an error message if any score is invalid.
💡 Why This Matters
🌍 Real World
Defensive programming helps prevent bugs and crashes by checking data before using it, which is important in real software that interacts with users or external data.
💼 Career
Many programming jobs require writing safe and reliable code that handles unexpected or incorrect inputs gracefully.
Progress0 / 4 steps