Integer Bounds Write a program (largest.c) that finds the largest short. Your pr
ID: 3672099 • Letter: I
Question
Integer Bounds Write a program (largest.c) that finds the largest short. Your program should define a short int and initialize it to 0. Then it should enter an infinite loop that keeps on incrementing the short by 1 and displaying the result. When the program exceeds the limit that can be stored in a short, it will fail and start printing the wrong answers. To be able to view the results, modify your loop so that it waits for any user input (one char is enough) after every 1000 iterations. Kill the program after the overflow is reached. Compile your program into the executable largest and run your program. What is the largest short?Explanation / Answer
largest.c
#include <stdio.h>
int main()
{
short int number = 0;
char ch;
short int largestShort = 0;
while (1)
{
printf("%d ", number);
largestShort = number;
//after every 1000 numbers it asks the user to input a character
if (number % 1000 == 0 && number != 0)
{
printf("Enter an character ");
scanf(" %c", &ch);
}
number++;
//after the overflow number becomes -ve in c,so as soon as number becomes -ve we break the loop and show the last +ve number
//which infact would be the largest short number in c.
if (number < 0)
{
printf("largest short number is %d", largestShort);
break;
}
}
}
after running the program largest short is 32767
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.