Se está descargando tu SlideShare. ×

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Cargando en…3
×

1 de 2 Anuncio

If char is 8 bits and int is 16- predict the values of -7 - 3 and size.docx

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
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;
}
.

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
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;
}
.

Anuncio
Anuncio