What if a tiny unchecked mistake could crash your whole program--how can you stop that from happening?
Why Defensive programming practices? - Purpose & Use Cases
Imagine writing a C program that reads user input and processes it without checking if the input is valid or if pointers are null. Suddenly, the program crashes or behaves unpredictably when unexpected data arrives.
Without defensive programming, your code is fragile. It can crash, produce wrong results, or cause security holes because it blindly trusts inputs and assumptions. Debugging these issues is slow and frustrating.
Defensive programming teaches you to anticipate problems before they happen. By checking inputs, validating pointers, and handling errors early, your code becomes more reliable and easier to maintain.
int *ptr = NULL;
*ptr = 5; // no check, causes crashint *ptr = NULL; if (ptr != NULL) { *ptr = 5; } else { // handle error }
It enables you to build programs that keep running safely even when unexpected things happen.
Think of a banking app that checks every transaction input carefully to avoid errors or fraud, protecting both the user and the bank.
Defensive programming prevents crashes by validating inputs and pointers.
It makes debugging easier by catching errors early.
It helps build safer, more trustworthy software.
Practice
Solution
Step 1: Understand the purpose of defensive programming
Defensive programming is about preventing errors by checking inputs and conditions early.Step 2: Identify the correct goal from options
To check for errors before they cause problems clearly states checking for errors before problems happen, which matches defensive programming.Final Answer:
To check for errors before they cause problems -> Option AQuick Check:
Defensive programming = check errors early [OK]
- Thinking it means ignoring errors
- Confusing it with faster coding
- Believing it removes need for comments
Solution
Step 1: Identify correct NULL pointer check syntax
In C, to check if a pointer is not NULL, use 'ptr != NULL'. Assignment '=' is wrong here.Step 2: Analyze each option
'if (ptr != NULL) { /* use ptr */ }' is correct because it checks that ptr is not NULL before use. B and D use '=' for assignment, setting ptr to NULL/0 and making the condition false. 'if (ptr == 0) { /* use ptr */ }' checks if ptr IS null (equivalent to NULL) and then uses it, causing a null dereference crash.Final Answer:
if (ptr != NULL) { /* use ptr */ } -> Option BQuick Check:
Use '!=' to compare pointer with NULL [OK]
- Using '=' instead of '==' or '!=' in conditions
- Confusing NULL with zero without clarity
- Not checking pointer before use
int *ptr = NULL;
if (ptr != NULL) {
printf("Pointer is valid\n");
} else {
printf("Pointer is NULL\n");
}Solution
Step 1: Analyze pointer initialization
The pointer 'ptr' is set to NULL, meaning it points to nothing.Step 2: Check the if condition
The condition 'ptr != NULL' is false because ptr is NULL, so the else block runs.Final Answer:
Pointer is NULL -> Option AQuick Check:
NULL pointer triggers else block [OK]
- Assuming NULL pointer is valid
- Expecting compilation error
- Thinking no output occurs
void print_length(char *str) {
if (str = NULL) {
printf("String is NULL\n");
return;
}
printf("Length: %d\n", strlen(str));
}Solution
Step 1: Check the if condition syntax
The condition uses 'str = NULL' which assigns NULL to str instead of comparing it.Step 2: Understand impact of assignment in condition
Assignment returns NULL (false), so the if block never runs as intended. It should be 'str == NULL'.Final Answer:
Using assignment '=' instead of comparison '==' in if condition -> Option CQuick Check:
Use '==' to compare pointers, not '=' [OK]
- Confusing '=' and '==' in conditions
- Thinking strlen() is invalid here
- Ignoring function return type
Solution
Step 1: Understand safe input reading
Using scanf requires checking its return value to confirm input was read correctly.Step 2: Validate input range
After reading, check if the integer is within expected limits to avoid errors later.Final Answer:
Check scanf's return value and validate the input range -> Option DQuick Check:
Check input success and validity [OK]
- Ignoring scanf return value
- Using unsafe input functions like gets()
- Assuming user input is always valid
Ready to prove it?
Pass the test and this pattern moves unknown
