The values of -7 % 3 and sizeof(-5 % 3) would be either -1 and 2, or -1 and 4, depending on whether int is 16 bits or 32 bits. The size of an int can vary between compilers, and is often 16 bits for older processors and 32 bits for newer 32-bit systems. The output would be -1 and either 2 or 4 bytes, based on the implementation's definition of int.
If char is 8 bits and int is 16- predict the values of -7 - 3 and size.docx
1. If char is 8 bits and int is 16, predict the
values of -7 % 3 and sizeof(-5 % 3). Why?
A. two possibilities: -1 and 2 or 2 and 2
B. A general prediction cannot be made
for all implementations.
C. -1 and 2 or -1 and 4 depending upon
the data type of sizeof
D. two possibilities: -1 and 2 or 1 and 2
E. three possibilities: -1 and 2 or 2 and 2
or -2.3 and 2
Solution
ANSWER - Option C
If char is 8 bits and int is 16, then the values of -7 % 3 (considering -7%3 as by default signed
integer expression ) and sizeof(-5 % 3) would be -1 and 2 or -1 and 4 as the size of an int is
really compiler dependent. Previously, when processors were 16 bit, an int was 2 bytes.
Nowadays, it's most often 4 bytes on a 32 bits system. C99 added new types where you can
explicitly mention for a certain sized integer, for example int16_t or int32_t. Prior to that, there
was no universal way to get an integer of a specific size.
e.g.
#include <iostream>
using namespace std;
int main() {
int a=-7%3;
cout<<a<<endl;
int b=-5 % 3;
cout<<sizeof(b);
return 0;
}