(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("%...")
 
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 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 58: Line 58:
 
||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
 +
 +
{'''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
 
</quiz>
 
</quiz>

Revision as of 21:38, 15 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

{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 </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>