Friday, January 27, 2012

puzzle code of structure

Q1) Output of the program.

#include "stdio.h"
#include "conio.h"
struct
{
       int a;
       char c;
}t[2];

main()
{
    int i; 
    printf("%d",sizeof(t));  
    getch();
}
Ans:- Compiler dependent.
Discussion:-Size of variable depends upon platform.

Q2)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
  struct abc
   {
         int a;
         struct xyz
         {
                int b;
         }t;
   }p;
  
   p.t.b=100;
   printf("%d",t.b);
   getch();
}
Ans:-Compile time error.
Discussion:-Here xyz is a inner structure. And the scope of structure variable of inner structure is within outer structure abc. To print the member of inner structure, outer structure variable is necessary.
So the correct printf() statement is printf(“%d”,p.t.b);

 
Q3) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
  struct hello{
                      unsigned int a:1;
                      unsigned int b:2;
                    };
  struct hello t;
  t.a=1;
  t.b=100;
  printf("%d\n",t.a);
  printf("%d",t.b);
  getch();
}
Ans:-1 0
Discussion:-The size of member b of structure hello is 2 bit. So maximum it can able to contain value 3.But here b is initialize by 100. So it will print 0.

Q4) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
 struct hello{
               int a;
               int b;
             };
 struct hello t;
 t.a=100;
 printf("%d",t.b);
 getch();
}
Ans:-DEV C++ compiler produces error.
         TC++ compiler produces 0.
Discussion:-If structure is initialize partially then the remaining element automatically initialize by 0.



Q5) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
 
  union hello
  {
   int a;
   int b ;
  };
  union hello t;
  t.a=10;
  printf("%d",t.b);

  getch();
}

Ans:-4
Discussion:-In Union data type, all member share a common memory. So if the value of a member is change then it affects other member.

Q6)Output of the program.

#include "stdio.h"
#include "conio.h"
main()
{
 
  union hello
  {
   int a;
   int b ;
  };
  union hello t;
  printf("%d",sizeof(t));
  getch();
}         
Ans:-DEV C++ gives 4
          TC ++ gives 2.
Discussion:-In union data type memory space is created for largest size member.


Q7)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{    
 struct abc
 {
       int a;
       char b;
 };
struct abc p={10,'A'},q;
q=p;
printf("%d%c",q.a,q.b);
getch();
}
Ans:-10 A
Discussion:-One structure variable can initialize by other structure variable of same structure.

Q8)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{    
struct abc{
            int a;
          }p;
struct pqr{
            int a;
          }q;
p.a=100;
q=p;         
getch();
}
Ans:-Compile time error.
Discussion:-One structure variable cannot possible to initialize by other structure variable.


Q9)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{    
struct abc{
            int a;
          }p;
struct pqr{
            int a;
          }q;
p.a=100;
q.a=100;
if(p==q)
printf("Same");
else
printf("Not same");         
getch();
}
Ans:-Compile time error.
Discussion:-It is not allowed to compare two structure variable using == sign.


Q10) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{    
struct abc{
            int a;
          };
struct pqr{
           struct abc p;
          }q;

q.p.a=100;
printf("%d",q.p.a);
getch();
}         
Ans:-100.
Discussion:-Here the structure variable of structure abc is declared within pqr structure .It works like nested structure.



Q11) Output of the program.
#include "stdio.h"
#include "conio.h"
struct abc
  {
         int a;
         int b;
  };

void print(struct abc *p)
{
     printf("%d%d",p->a,p->b);
}

main()
{    
  struct abc t,*p;
  t.a=10;
  t.b=20;
  p=&t;
  print(p);
  getch();
}
Ans:-10 20
Discussion:-Here the print() function takes a pointer of structure. And arrow operator (->) is used to print the member of structure.

Q12)Find error
 struct abc
  {
         int a;
         int b;
         struct abc *p
  };
Ans:-No error .
Dsicussion:-Such type of structure is called self referencing structure.


Q13)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
      struct abc{
                  int a;
                  char b[10];
                };
      struct abc v[2]={{10,"sourav"},{100,"kayal"}};
      printf("%d%s",v.a,v.b);
      getch();
}
Ans:-Compile time error.
Discussion:-Here printf() function should be like printf(“%d%s”,v[0].a,v[0].b);


No comments:

Post a Comment