(Created page with "<quiz display="simple"> {'''What will be the output of the following code?''' <syntaxhighlight lang="c"> #include<stdio.h> int main(){ int a = 5; int* p = &a; printf("%...")
 
 
(8 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
#include<stdio.h>
 
#include<stdio.h>
  
int main(){
+
int main()
 +
{
 
   int a = 5;
 
   int a = 5;
 
   int* p = &a;
 
   int* p = &a;
 
   printf("%d", ++*p);
 
   printf("%d", ++*p);
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 21: Line 21:
 
#include<stdio.h>
 
#include<stdio.h>
  
int main(){
+
int main()
 +
{
 
   char a[] = "Hello World";
 
   char a[] = "Hello World";
 
   char* p = &a;
 
   char* p = &a;
 
   printf("%s", p+2 );
 
   printf("%s", p+2 );
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 35: Line 35:
 
-Runtime Error
 
-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"
+
||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?'''
 
{'''What will be the output of the following code?'''
Line 41: Line 41:
 
#include<stdio.h>
 
#include<stdio.h>
  
int main(){
+
int main()
 +
{
 
   int a;
 
   int a;
 
   int* p = &a;
 
   int* p = &a;
 
   printf("%zu", sizeof( *(char*)p  ));
 
   printf("%zu", sizeof( *(char*)p  ));
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 56: Line 56:
 
-Compile Error
 
-Compile Error
  
||p is typecasted to char pointer and then dereferenced. So, returned type will be char and sizeof(char) is 1
+
||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">
 +
#include<stdio.h>
 +
 
 +
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.
 +
 
 +
{'''What will be the output of the following code?'''
 +
<syntaxhighlight lang="c">
 +
#include<stdio.h>
 +
 
 +
int main()
 +
{
 +
  int *a = (int*) 1;
 +
  printf("%d", a);
 +
}
 +
</syntaxhighlight>
 +
|type="()"
 +
/}
 +
-Garbage value
 +
+1
 +
-0
 +
-Compile error
 +
-Segmentation fault
 +
||Assigning int values to pointer variable is possible. Only when we dereference the variable using *, we get a segmentation fault.
 +
 
 +
{'''What will be the output of the following code?'''
 +
<syntaxhighlight lang="c">
 +
#include<stdio.h>
 +
 
 +
int main()
 +
{
 +
  int a = 1, *p, **pp;
 +
  p = &a;
 +
  pp = p;
 +
  printf("%d", **pp);
 +
}
 +
</syntaxhighlight>
 +
|type="()"
 +
/}
 +
-Garbage value
 +
-1
 +
-Compile error
 +
+Segmentation fault
 +
||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a, but **p will use 1 as an address and tries to access the memory location 1, giving segmentation fault.
  
 +
{'''What will be the output of the following code?'''
 +
<syntaxhighlight lang="c">
 +
#include<stdio.h>
 +
 +
int main()
 +
{
 +
  int a = 1, *p, **pp;
 +
  p = &a;
 +
  pp = p;
 +
  printf("%d", *pp);
 +
}
 +
</syntaxhighlight>
 +
|type="()"
 +
/}
 +
+1
 +
-Garbage value
 +
-Compile error
 +
-Segmentation fault
 +
||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a.
 +
 +
 +
{'''Assuming a little endian machine, what will be the output of the following program?'''
 +
<syntaxhighlight lang="c">
 +
#include<stdio.h>
 +
 +
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>

Latest revision as of 20:18, 16 June 2014

<quiz display="simple"> {What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

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">

  1. include<stdio.h>

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">

  1. include<stdio.h>

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">

  1. include<stdio.h>

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.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int *a = (int*) 1;
 printf("%d", a);

} </syntaxhighlight> |type="()" /} -Garbage value +1 -0 -Compile error -Segmentation fault ||Assigning int values to pointer variable is possible. Only when we dereference the variable using *, we get a segmentation fault.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 1, *p, **pp;
 p = &a;
 pp = p;
 printf("%d", **pp);

} </syntaxhighlight> |type="()" /} -Garbage value -1 -Compile error +Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a, but **p will use 1 as an address and tries to access the memory location 1, giving segmentation fault.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 1, *p, **pp;
 p = &a;
 pp = p;
 printf("%d", *pp);

} </syntaxhighlight> |type="()" /} +1 -Garbage value -Compile error -Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a.


{Assuming a little endian machine, what will be the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

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">

  1. include<stdio.h>

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">

  1. include<stdio.h>

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">

  1. include<stdio.h>

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

</quiz>