Thursday, October 28, 2010

C Puzzles Part 2

Questions 

L1.Q1 : Write the output of the following program 

#defineABC     20
#define XYZ    10
#define XXX    ABC - XYZ
void main()
{
        int     a;
        a = XXX * 10;
        printf("%d ", a);
}


L1.Q2 : Write the output of this program 

#define calc(a, b)     (a * b) / (a - b)
void main()
{        int a = 20, b = 10;
         printf("%d ", calc(a + 4, b -2));
}

L1.Q3 : What will be output of the following program ? 

void main()
{
         int cnt = 5, a;
         do {
               a /= cnt;
        } while (cnt --);
        printf ("%d ", a);
}

L1.Q4 : Print the output of this program 

void main()
{
        int  a, b, c, abc = 0;
         a = b = c = 40;
         if (c) {
               int abc;
               abc = a*b+c;
        }
        printf ("c = %d, abc = %d ", c, abc);
}

L1.Q5 : Print the output of this program 

main()
{
        int k = 5;
        if (++k < 5 && k++/5 || ++k <= 8);
        printf("%d ", k);
}


L1.Q6 : What is the output of this program ? 

void fn(int, int);
main()
{        int     a = 5;
         printf("Main :  %d %d ", a++, ++a);
         fn(a, a++);
}
void fn(int a, int b)
{
       printf("Fn : a = %d b = %d ", a, b);
}


Answers 

L1.A1 


a = xxx * 10  
which is => a = ABC - XYZ * 10
        => a = 20 - 10 * 10
        => a = 20 - 100
        => a = -80

L1.A2 


Actual substitution is like this :
calc(20+4, 10 -2) is calculated as follows 
(20+4 * 10-2) / (20+4 - 10-2)
(20+40-2) / 12
58 / 12  = 4.8 
since  it is printed in %d the ans is 4

L1.A3 


This  problem  will compile  properly,  but it will give run
time error.  It will give divide-by-zero  error.  Look in to
the do loop portion

        do { a /= cnt; } while (cnt --);

when the 'cnt' value is 1, it is decremented in 'while
( cnt --)' and on next reference of 'cnt' it becomes zero.

        a /= cnt;  /* ie. a /= 0 */
which leads to divide-by-zero error.

L1.A4 


the result will be c = 40 and abc = 0;
because the scope of the variable  'abc' inside if(c) {..  }
is not valid out side that if (.)  { ..  }.

 
L1.A5 


The answer is 7.  The first condition ++k < 5 is checked and
it is false  (Now k = 6).  So, it checks  the 3rd  condition
(or condition ++k <= 8) and (now k = 7) it is true.  At this
point k value is  incremented by twice, hence the value of k
becomes 7.  

L1.A6 

The  solution  depends  on  the   implementation  of  stack.
(Depends on OS) In some  machines the  arguments  are passed
from left to right to the  stack.  In this  case the  result
will be


        Main :  5 7 Fn :  7 7 

Other  machines  the  arguments  may be passed from right to
left to the stack.  In that case the result will be
       Main : 6 6
       Fn   : 8 7

Monday, October 25, 2010

C Puzzles Part 1


C programming is very important topic for freshers, interviewers usually asks questions from C,C++,Data structures when it comes to programming. So i am starting this Topic , i will gather some good puzzles and keep on continuing this topic. I will also post the answers to the puzzles below soon.

The puzzles mostly covers on typo mistakes, basic understanding of some C concepts , i will explain the answers tho these puzzles covering those concepts also, And please use comment section.

solve them and comment the solutions.

1)
#include

main()
{
 auto int i = 0;
 printf("%d\n",i);

 {
  int i = 2;
  printf("%d\n",i);
  {
   i+=1;
   printf("%d\n",i);
  }
  printf("%d\n",i);
 }
 printf("%d\n",i);
 printf("%d\n",reset());
 printf("%d\n",ret10());
 printf("%d\n",reset());
 printf("%d\n",ret10());
}


int reset()
{
 int j = 0;
 return(j);
}

int ret10()
{
 static int i = 10;
 i+=1;
 return(i);
}


2)
#include
#include
main()
{
 struct emp1
 {
  char *name;
  int age;
 };
 struct emp2
 {
  char *cp;
  struct emp1 e1;
 }e2 = {"ghi",{"jkl",123}};

 struct emp1 e3 = {"rwer",2341};
 printf("\n%s %d\n",e3.name,e3.age);
 printf("\n%s %s %d\n",e2.cp,e2.e1.name,e2.e1.age);
}

3)
#include
struct xyz
{
 int xyz ;
}
;

main()
{
 union xyz
 {
  int xyz;
 }
 ;
}

4)
#include
main()
{
 char s[] = "Bouquets and Brickbats";
 printf("\n%c, ",*(&s[2]));
 printf("%s, ",s+5);
 printf("\n%s",s);
 printf("\n%c",*(s+2));
}

5)
#include
#include
struct s
{
 char *st;
 struct s *sptr;
};
main()
{
 int i;
 struct s *p[3];
 static struct s a[]={
  {"abcd",a+1},
  {"pqrs",a+2},
  {"stuv",a}
 };
 for( i=0;i<3;i++ )
  p[i] = a[i].sptr;
 swap(*p,a);
 printf("%s %s %s \n",p[0]->st,(*p)->st, (*p)->sptr->st);
}

swap(p1,p2)
struct s *p1,*p2;
{
 char *temp;
 temp = p1->st;
 p1->st = p2->st;
 p2->st = temp;
}

6)
#include
Swap( int *x , int *y)
{
 int tmp = *x ;
 *x = *y ;
 *y = tmp;
}
main()
{
 int a = 1, b = 2;
 Swap(&a, &b);
 printf("%d %d\n", a, b);
}

7)
#include
main()
{
 int i;
 scanf("%d",&i);
 switch(i) {
  printf("\nHello");
  case 1: printf("\none");
  break;
  case 2: printf("\ntwo");
  break;
 }
}

8)
#include
main()
{
 int x;
 x = 3;
 f(x);
 printf("MAIN");

}

f(int n)
{
 printf("F");
 if (n != 0)
  f(n-1);
}


9)
#include
main()
{
 int ptr[] = {1,2,23,6,5,6};
 char str[] = {'a','b','c','d','e','f','g','h'};

 printf("pointer differences are %ld, %d",&ptr[3], &str[3]-&str[0]);
}

10)
#include
main()
{
 char a,b,c;
 scanf("%c %c %c",&a,&b,&c);
 printf("%c %c %c ", a, b, c);
 }

11)
#include
#define PRINT(int) printf( "int = %d ", int)
main()
{
 int x=03,y=02,z=01;
 PRINT (x | y & ~z);
 PRINT (x & y && z);
 PRINT (x ^ (y & ~z));
}

12)
#include
main()
{
 int a = 10000;
 char b='c';
 int i,j;

 printf("%d,%d",printf("%d\n",a),printf("%c\n",b));
}

13)
#include
#define PR(a)   printf("%d\t",(int) (a));
#define PRINT(a,b,c) PR(a);PR(b);PR(c);
#define MAX(a,b) (a
main(){
 int x = 1,y = 2;
 PRINT(MAX(x++,y),x,y);
 PRINT(MAX(x++,y),x,y);
}

14)
#include
main()
{
 unsigned int i=1;
 for(;i>=0;i--)  printf("hello: %u\n",i);
}

15)
#include
main()
{
 struct ist{
  int x;
  int y;
};

 struct list{
  int x;
  struct ist next;
 }head;
  head.x = 100;
  head.next.x=10;
  printf("%d %d", head.x,head.next.x);
}

16)
#include
main()
{
 typedef union
 {
  struct
  {
   char c1,c2;
  } s;
  long j;
  float x;
 } U;

 U example;
 example.s.c1 = 'a';
 example.s.c2 = 'b';
   example.j = 5;
 printf("%c %c %d", example.s.c1, example.s.c2, example.j);
}

17)
#include
main()
{
 struct s1
 {       char *str;
  struct s1 *ptr;
 };
 static struct s1 arr[] = { {"Hyderabad",arr+1},{"Bangalore",arr+2},
{"Delhi",arr}};
 struct s1 *p[3];
 int i;

 for(i=0;i<=2;i++)
  p[i] = arr[i].ptr;

 printf("%s\n",(*p)->str);
 printf("%s\n",(++*p)->str);
 printf("%s\n",((*p)++)->str);
}

18)
#include
main()
{
 struct s1
 {       char *str;
  struct s1 *ptr;
 };
 static struct s1 arr[] = {{"Hyderabad",arr+1},
  {"Bangalore",arr+2},
  {"Delhi",arr}
 };
 struct s1 *p[3];
 int i;

 for(i=0;i<=2;i++)       p[i] = arr[i].ptr;

 printf("%s  ",(*p)->str);
 printf("%s ",(++*p)->str);
 printf("%s ",((*p)++)->str);
}

19)
#include
main()
{
 char input[] = "SSSWILTECH1\1\1";
 int i, c;
 for ( i=2; (c=input[i])!='\0'; i++){
  switch(c){
   case 'a': putchar ('i'); continue;
   case '1': break;
   case 1: while (( c = input[++i]) != '\1' && c!= '\0');
   case 9: putchar('S');
   case 'E': case 'L': continue;
   default: putchar(c);continue;
  }
  putchar(' ');
 }
 putchar('\n');
}

20)
#include
main()
{
 int i, n, m, b, x[25];
 int f1(int, int, int j[25]);
 for(i=0;i<25;i++) x[i] = i;
 i=0; m = 24;
 b=f1(i, m, x);
 printf("res %d\n",b);
}

int f1( int p, int q, int a[25])
{
int m1,m2;
if (q==0)
 return(a[p]);
 else
 {
 m1 = f1 (p, q/2, a);
 m2 = f1(p+q/2+1,q/2,a);
 if(m1
  return (m2);
 else
  return(m1);
 }
}

21)
#include
main()
{
 int a[3][4] ={1,2,3,4,5,6,7,8,9,10,11,12} ;
 int i,j,k=99 ;
 for(i=0;i<3;i++)
  for(j=0;j<4;j++)
  if(a[i][j] < k) k = a[i][j];
 printf("%d", k);
}

22)
#include
main()
{
 int a,b,c;
 for (b=c=10;a= "Love Your INDIA \
                           TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*
RPn/QPbEWS_JSWQAIJO^NBELP\
                           eHBFHT}TnALVlBLOFAKFCFQHFOQIAIREETMSQGCSQOU
HATFAJKSbEALGSkMCSlOASn^r\
                           ^r\\tZvYxXyT|S~Pn SPm SOn TNn
ULo0ULo#ULo-WHq!WFs XDt!"[b+++6];)
  while(a-->64) putchar (++c=='Z'?c=c/9:33^b&1);
}

23)
#include
main()
{
 char *p = "hello world!";
 *(p+0) = 'H';
 printf("%s",p);
}

24)
#include
main()
{
 unsigned int m[] = { 0x01,0x02, 0x04, 0x08,0x10, 0x20, 0x40, 0x80};
 unsigned int n,i;
 printf("%d",m[7]);
 scanf("%d",&n);
 for(i=0;i<=7;i++)
 {  if (n& m[i])
  printf("\nyes");
  else
   printf("\nno");
 }
}

25)
#include
main()
{
 int a,b=2,c;
 int *pointer;
 c = 3;
 pointer = &c;
 a = c/*pointer*/;
 b = c /* assigning 3 to b*/;
 printf("a = %d; b = %d", a,b);
}

Answers :


1)

0
2
3
3
0
0
11
0
12

2)

rwer 2341

ghi jkl 123

3)

4)

u, ets and Brickbats,
Bouquets and Brickbats
u

5)

abcd abcd stuv

6)

2 1

7)
in:1=>one
in:2=>two

8)

FFFFMAIN

9)

pointer differences are -1081544168, 3

10)
output same as the input

11)

int = 3 int = 1 int = 1

12)
c
10000
6,2

13)
2 2 2 3 4 2

14)

: 1
hello: 0
hello: 4294967295
hello: 4294967294
hello: 4294967293
hello: 4294967292
hello: 4294967291
hello: 4294967290
hello: 4294967289
hello: 4294967288
hello: 4294967287
hello: 4294967286
hello: 4294967285
hello: 4294967284
hello: 4294967283
hello: 4294967282
hello: 4294967281
hello: 4294967280
hello: 4294967279
hello: 4294967278
hello: 4294967277
hello: 4294967276
hello: 4294967275
.
.
.
.
.
.

15)

100 10

16)

 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