Machine-translated by Google. Questions are authored in English.
Top

Discussion

How many times the while loop will get executed if a short int is 2 byte wide?

#include <stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
        printf("%c %d\n", j, j);
        j++;
    }
    return 0;
}

 

  • A.Infinite times
  • B.255 times
  • C.256 times
  • D.254 times

Answer: B

Correct answer: 255 times

j runs from 1 and the loop continues while j <= 255, so it executes for j equal to 1, 2, …, 255 - 255 passes. The width of a short is a red herring: j is declared int, and 255 is nowhere near the limit of either type.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.