Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
Line 79: | Line 79: | ||
+Yes | +Yes | ||
-No | -No | ||
+ | ||On a little endian machine the lower address will contain the least significant byte. Suppose a is stored at address 1000 and contains 1, then character at 1000 will be 1, if the machine is little endian | ||
+ | |||
{'''Assuming a little endian machine, what will be the output of the following program?''' | {'''Assuming a little endian machine, what will be the output of the following program?''' | ||
Line 113: | Line 115: | ||
-Runtime error | -Runtime error | ||
-Compiler dependent output | -Compiler dependent output | ||
+ | ||The code is printing the binary equivalent of the input number. Suppose a is stored starting from address 1000. Since, we assume a little endian machine, the LSB of a will be stored at address 1000 and MSB will be stored at address 1003. So, we make a char pointer to 1003 and take out the MSB. Then using shift operator we get the most significant 4 bits from it and then the lest significant 4 bits. We repeat the same for the other three bytes of a. | ||
</quiz> | </quiz> |
<quiz display="simple"> {What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
int a = 5; int* p = &a; printf("%d", ++*p);
} </syntaxhighlight> |type="{}" /} { 6 } ||p is pointing to the address of a. *p will return the content of a which is 5 and ++ will increment it to 6.
{What will be the output of the following code?
<syntaxhighlight lang="c">
int main() {
char a[] = "Hello World"; char* p = &a; printf("%s", p+2 );
} </syntaxhighlight> |type="()" /} -Compiler Error -World +llo World -Runtime Error
||Since p is a char pointer p+2 will add 2 to p (since sizeof(char) is 1). So, p+2 will be pointing to the string "llo World"
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
int a; int* p = &a; printf("%zu", sizeof( *(char*)p ));
} </syntaxhighlight> |type="()" /}
+1 -2 -4 -Compile Error
||p is typecasted to char pointer and then dereferenced. So, returned type will be char and sizeof(char) is 1
{Is the following code legal? <syntaxhighlight lang="c">
int main() {
int a = 1; if((char*) &a) { printf("My machine is little endian"); } else { printf("My machine is big endian\n"); }
} </syntaxhighlight> |type="()" /} +Yes -No ||On a little endian machine the lower address will contain the least significant byte. Suppose a is stored at address 1000 and contains 1, then character at 1000 will be 1, if the machine is little endian
{Assuming a little endian machine, what will be the output of the following program?
<syntaxhighlight lang="c">
fun(int a) {
char *arr[] = {"0000", "0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}; unsigned char* p = (unsigned char*) &a ; p+=3; int i; for(i = 0; i < sizeof a; i++) { int d = (*p)>>4; printf("%s", arr[d]); d = (*p) & 0xf; printf("%s ", arr[d]); p--; }
}
int main() {
int a; scanf("%d", &a); fun(a);
} </syntaxhighlight> |type="()" /} +Print the binary of the input number -Compile error -Runtime error -Compiler dependent output ||The code is printing the binary equivalent of the input number. Suppose a is stored starting from address 1000. Since, we assume a little endian machine, the LSB of a will be stored at address 1000 and MSB will be stored at address 1003. So, we make a char pointer to 1003 and take out the MSB. Then using shift operator we get the most significant 4 bits from it and then the lest significant 4 bits. We repeat the same for the other three bytes of a. </quiz>
<quiz display="simple"> {What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
int a = 5; int* p = &a; printf("%d", ++*p);
} </syntaxhighlight> |type="{}" /} { 6 } ||p is pointing to the address of a. *p will return the content of a which is 5 and ++ will increment it to 6.
{What will be the output of the following code?
<syntaxhighlight lang="c">
int main() {
char a[] = "Hello World"; char* p = &a; printf("%s", p+2 );
} </syntaxhighlight> |type="()" /} -Compiler Error -World +llo World -Runtime Error
||Since p is a char pointer p+2 will add 2 to p (since sizeof(char) is 1). So, p+2 will be pointing to the string "llo World"
{What will be the output of the following code? <syntaxhighlight lang="c">
int main() {
int a; int* p = &a; printf("%zu", sizeof( *(char*)p ));
} </syntaxhighlight> |type="()" /}
+1 -2 -4 -Compile Error
||p is typecasted to char pointer and then dereferenced. So, returned type will be char and sizeof(char) is 1
{Is the following code legal? <syntaxhighlight lang="c">
int main() {
int a = 1; if((char*) &a) { printf("My machine is little endian"); } else { printf("My machine is big endian\n"); }
} </syntaxhighlight> |type="()" /} +Yes -No
{Assuming a little endian machine, what will be the output of the following program? <syntaxhighlight lang="c">
fun(int a) {
char *arr[] = {"0000", "0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}; unsigned char* p = (unsigned char*) &a ; p+=3; int i; for(i = 0; i < sizeof a; i++) { int d = (*p)>>4; printf("%s", arr[d]); d = (*p) & 0xf; printf("%s ", arr[d]); p--; }
}
int main() {
int a; scanf("%d", &a); fun(a);
} </syntaxhighlight> |type="()" /} +Print the binary of the input number -Compile error -Runtime error -Compiler dependent output </quiz>