07-26-2023, 03:48 PM
Example:
#include <stdio.h>
int main() {
short int max = 32767;
short int min = -32768;
// Overflow example
short int overflowed = max + 1;
printf("Overflowed value: %d\n", overflowed);
// Underflow example
short int underflowed = min - 1;
printf("Underflowed value: %d\n", underflowed);
return 0;
}
Explanation:
In this example, we demonstrate what happens when a short int variable encounters overflow and underflow. The max value for a short int is 32767, and adding 1 to it causes an overflow, resulting in a value of -32768, which is the min value for a short int. Similarly, subtracting 1 from the min value causes an underflow, resulting in 32767, which is the max value for a short int.
#include <stdio.h>
int main() {
short int max = 32767;
short int min = -32768;
// Overflow example
short int overflowed = max + 1;
printf("Overflowed value: %d\n", overflowed);
// Underflow example
short int underflowed = min - 1;
printf("Underflowed value: %d\n", underflowed);
return 0;
}
Explanation:
In this example, we demonstrate what happens when a short int variable encounters overflow and underflow. The max value for a short int is 32767, and adding 1 to it causes an overflow, resulting in a value of -32768, which is the min value for a short int. Similarly, subtracting 1 from the min value causes an underflow, resulting in 32767, which is the max value for a short int.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos