Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Overflow and underflow with short int in C
#1
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.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparisons with short int in C Qomplainerz 0 409 07-26-2023, 03:51 PM
Last Post: Qomplainerz
  Type casting with short int in C Qomplainerz 0 361 07-26-2023, 03:50 PM
Last Post: Qomplainerz
  User input and short int conversion in C Qomplainerz 0 394 07-26-2023, 03:49 PM
Last Post: Qomplainerz
  Arithmetic operations with short int in C Qomplainerz 0 408 07-26-2023, 03:46 PM
Last Post: Qomplainerz
  Storing short integers in C Qomplainerz 0 393 07-26-2023, 03:45 PM
Last Post: Qomplainerz
  Buffer overflow in C Qomplainerz 0 234 07-26-2023, 07:08 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 8 Guest(s)