Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Defensive Programming Principles | Writing Safe and Predictable Code
C Defensive Programming and Error Handling

bookDefensive Programming Principles

Prerequisites
Prérequis
Note
Definition

Defensive programming is the practice of writing code that anticipates errors, validates assumptions, and ensures that failures happen in a controlled and predictable way.

In C you should never assume that inputs are always valid or that functions always succeed. Instead, you write your code to check for unexpected or invalid situations, so your program can handle them safely and avoid unpredictable behavior or crashes. This approach leads to more robust and reliable software, especially in a language like C where the consequences of mistakes can be severe.

main.c

main.c

copy
123456789101112131415
#include <stdio.h> // Non-defensive version: assumes input is always valid (denominator != 0) double divide(int numerator, int denominator) { return (double)numerator / denominator; } int main() { int num = 10; int denom = 0; printf("Non-defensive result: %f\n", divide_nondefensive(num, denom)); // Undefined behavior return 0; }

Assuming inputs are always valid is risky in C. Without checks for invalid values, like zero division or out of bounds access, your program can crash or behave unpredictably. Defensive checks validate inputs and keep your code safe and reliable.

main.c

main.c

copy
12345678910111213141516171819
#include <stdio.h> // Defensive version: checks for invalid input (denominator == 0) double divide_defensive(int numerator, int denominator) { if (denominator == 0) { printf("Error: division by zero.\n"); return 0.0; // Or handle error as appropriate } return (double)numerator / denominator; } int main() { int num = 10; int denom = 0; printf("Defensive result: %f\n", divide_defensive(num, denom)); // Safe handling return 0; }

This is just a simple example of defensive programming in action. In real-world C programs, there are many more scenarios where careful validation, error checking, and safe coding practices are essential to ensure reliability and prevent serious bugs or crashes.

question mark

Which of the following best describes defensive programming?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 12.5

bookDefensive Programming Principles

Glissez pour afficher le menu

Prerequisites
Prérequis
Note
Definition

Defensive programming is the practice of writing code that anticipates errors, validates assumptions, and ensures that failures happen in a controlled and predictable way.

In C you should never assume that inputs are always valid or that functions always succeed. Instead, you write your code to check for unexpected or invalid situations, so your program can handle them safely and avoid unpredictable behavior or crashes. This approach leads to more robust and reliable software, especially in a language like C where the consequences of mistakes can be severe.

main.c

main.c

copy
123456789101112131415
#include <stdio.h> // Non-defensive version: assumes input is always valid (denominator != 0) double divide(int numerator, int denominator) { return (double)numerator / denominator; } int main() { int num = 10; int denom = 0; printf("Non-defensive result: %f\n", divide_nondefensive(num, denom)); // Undefined behavior return 0; }

Assuming inputs are always valid is risky in C. Without checks for invalid values, like zero division or out of bounds access, your program can crash or behave unpredictably. Defensive checks validate inputs and keep your code safe and reliable.

main.c

main.c

copy
12345678910111213141516171819
#include <stdio.h> // Defensive version: checks for invalid input (denominator == 0) double divide_defensive(int numerator, int denominator) { if (denominator == 0) { printf("Error: division by zero.\n"); return 0.0; // Or handle error as appropriate } return (double)numerator / denominator; } int main() { int num = 10; int denom = 0; printf("Defensive result: %f\n", divide_defensive(num, denom)); // Safe handling return 0; }

This is just a simple example of defensive programming in action. In real-world C programs, there are many more scenarios where careful validation, error checking, and safe coding practices are essential to ensure reliability and prevent serious bugs or crashes.

question mark

Which of the following best describes defensive programming?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 1
some-alt