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 B
Quick Check:
Use '!=' to compare pointer with NULL [OK]
Hint: Use '!=' to compare pointer with NULL, not '=' [OK]
Common Mistakes:
Using '=' instead of '==' or '!=' in conditions
Confusing NULL with zero without clarity
Not checking pointer before use
3. What will be the output of this C code snippet?
int *ptr = NULL;
if (ptr != NULL) {
printf("Pointer is valid\n");
} else {
printf("Pointer is NULL\n");
}
medium
A. Pointer is NULL
B. Pointer is valid
C. Compilation error
D. No output
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 A
Quick Check:
NULL pointer triggers else block [OK]
Hint: NULL pointer means else block runs [OK]
Common Mistakes:
Assuming NULL pointer is valid
Expecting compilation error
Thinking no output occurs
4. Identify the error in this defensive programming snippet:
void print_length(char *str) {
if (str = NULL) {
printf("String is NULL\n");
return;
}
printf("Length: %d\n", strlen(str));
}
medium
A. strlen() cannot be used on strings
B. Missing semicolon after printf
C. Using assignment '=' instead of comparison '==' in if condition
D. Function return type should be int
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 C
Quick Check:
Use '==' to compare pointers, not '=' [OK]
Hint: Use '==' to compare, '=' is assignment [OK]
Common Mistakes:
Confusing '=' and '==' in conditions
Thinking strlen() is invalid here
Ignoring function return type
5. You want to safely read an integer from user input in C. Which defensive programming practice should you apply?
hard
A. Use scanf without checking its return value
B. Use gets() to read input without validation
C. Assume input is always correct and proceed
D. Check scanf's return value and validate the input range
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 D
Quick Check:
Check input success and validity [OK]
Hint: Always check scanf return and input validity [OK]
Common Mistakes:
Ignoring scanf return value
Using unsafe input functions like gets()
Assuming user input is always valid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.