Pages

Wednesday 29 August 2012

Some tricky c questions


Some tricky and important c questions for placements

Index:

1.  Data types
2.  Expressions
3.  Storage Classes
4.  Arrays
5.  Pointers
6.  Strings
7.  Input and Output
8. Structures and unions
9. Bitwise tricks

  each topic consists level-1 and level -2

Assume integer takes 4 bytes ,character takes 1 byte, pointer take 4 bytes , float takes 4 bytes and double takes 8bytes
----------------------------------------------------------------------------------------------------------------------------------

DATA TYPES (Level-I)


1)
#include<stdio.h>
int main()
{
   int a=(1<<32)-1;
   a++;
   printf("%d",a);
}

Sol.: 0

2)
#include<stdio.h>
int main()
 {
   char c=255;
   int i=c;
   printf("%d",i);
 }

Sol.: -1

3)
#include<stdio.h>
int main()
 {
   int a;
   char b='A';
   a=b;
   b=a;
   printf("%d",b);
 }

Sol.: 65

4)
#include<stdio.h>
int main()
{
   int a;
   char b;
   float c;
   unsigned int d;
   printf("%d,%d,%d,%d",sizeof(a),sizeof(b),sizeof(c),sizeof(d));
   printf("\n%d,%d,%d",sizeof(1),sizeof('A'),sizeof(0.7));
 }

Sol: 4,1,4,4
     4,1,8

5)
#include<stdio.h>
int main()
{

  printf("\n%d,%d,%d",sizeof(1UL),sizeof(12U),sizeof(0.7f));
}

Sol: 8,4,4 (UL->unsigned long,U->unsigned,f->float)

6)
#include<stdio.h>
int main()
{
   float a,b,c;
   a=4/5;
   b=(float)(4/5);
   c=4.0/5;
   printf("%f,%f,%f",a,b,c);
}

Sol.: 0.0000,0.0000,0.8000

7)
#include<stdio.h>
enum{i=1,j=2};
int main()
{
   i++;
   printf("%d",i);
}

Sol.: error: lvalue required as increment operand

8)
#include<stdio.h>
enum days{SUN=3,MON,TUE=-1,WED};
int main()
{
   printf("%d,%d",MON,WED);
}

Sol: 4,0

9)
#include<stdio.h>
int main()
{
   signed char c=-64;
   unsigned char p=c;
   printf("%d",p);
}

Sol: 192 (i.e.256-64)

10)
#include<stdio.h>
int main()
{
   unsigned char c=-64;
   int b=-32;
   if(c>b)
    printf("pass");
   else
    printf("fail");
}

Sol.: pass (Here value of c is 192)

11)
#include<stdio.h>
int main()
{
    int a=5,b=12;
    float c=a;
    double d=b;
    printf("%f,%lf",c,d);
}

Sol: 5.0000,12.0000



DATA TYPES(LEVEL-II)

1)
#include<stdio.h>
int main()
{
   unsigned short a=10;
   signed short b=10;
   if(a>-1 && b>-1)
    printf("no surprise");
   else
   printf("surprise");
}

Sol: no surprise

2)
#include<stdio.h>
int main()
{
   unsigned int a=10;
   signed int b=10;
   if(a>-1 && b>-1)
    printf("no surprise");
   else
    printf("surprise");
}

Sol: suprise(Here a>-1 returns 0)

3)
#include<stdio.h>
int main()
{
   signed char c=-64;
   unsigned char d=64;
   short int p=c,q=d;
   printf("%d,%d",p,q);
}

Sol: -64,64

5)
#include<stdio.h>
int main()
{
   float a=0.7;
   double b=0.7;
   if(a==b)
    printf("Equal");
   else
    printf("Unequal");
}

Sol: Unequal

6)
#include<stdio.h>
int main()
{
   signed char c;
   for(c=0;c<=127;c++)
   printf("%c",c);
}

Sol: Infinitly printing of characters.

7)
#include<stdio.h>
int main()
{
    float f=0.0f;
   int i;
   for(i=0;i<10;i++)
    f=f+0.1f;
   if(f==1.0f)
    printf("Equal");
   else
   printf("Unequal");
}

Sol: Unequal(Value of Float is not exactly stored in variable so f will give round off error)

8)
#include<stdio.h>
int main()
{
  float f=0.0f;
  int i;
  for(i=0;i<5;i++)
   f=f+0.2f;
  if(f==1.0f)
  printf("Equal");
  else
  printf("Unequal");
}

Sol: Equal

9)
#include<stdio.h>
int main()
{
  unsigned i;
  for(i=5;i>=0;i--)
  printf("%d",i);
}

Sol: Infinite printing of no.'s

11)
#include<stdio.h>
int main()
{
  float a=5.6;
  double b=12.345;
  int c=a,d=b;
  printf("%d,%d",c,d);
}

Sol: 5,12

12)
#include<stdio.h>
int main()
{
  unsigned int a=12345;
  unsigned char b=511,c=a;
  printf("%d,%d",b,c);
}

Sol.: 255,57

13)
#include<stdio.h>
int main()
{
  unsigned int a=12345;
  signed char b=511,c=a;
  printf("%d,%d",b,c);
}

Sol: -1,57

14)
#include<stdio.h>
int main()
{
  signed char c=-64;
   int b=-32;
   unsigned int a=-16;
 if(c>b)
 {
    printf("pass1");
   if(c<a)
    printf("pass2");
  else
   printf("fail2");
 }
 else
    printf("fail1");
  if(b<a)
     printf("pass2");
   else
    printf("fail2");
}

Sol.: fail1pass2

------------------------------------------------------------------------------------------------------------------------------


Expression(LEVEL-I)

1)
#include<stdio.h>
int main()
 {
   int a=1,b=2,c=3,d=-2,r;
   r=a+b*c/d;
   printf("%d\n",r);
   return 0;
 }

Sol.: -2

2)
#include<stdio.h>
int main()
 {
   int i,j=3,k=4;
   printf("%d\n",i=++j==k);
   return 0;
 }

Sol.: 1

3)
#include<stdio.h>
int f(void)
{
   printf("f");
   return 1;
}
int g(void)
{
   printf("g");
   return 2;
}
int h(void)
{
   printf("h");
   return 3;
}
int main()
{
   int r;
   r=f()+g()*h();
   printf("%d\n",r);
   return 0;
}

Sol:. fgh7(NOTE:fgh will get printed in any order depending upon compiler)

4)
#include<stdio.h>
int main()
{
   int a=-2,b;
   b=-a--;
  printf("%d,%d\n",a,b);
   return 0;
}

Sol.: -3,2

5)
#include<stdio.h>
int main()
{
   int i=+5,j=+4,k;
   k=i++*j++;
   printf("%d,%d,%d\n",i,j,k);
   return 0;
}

Sol.: 6,5,20(NOTE: Ans. will be compiler dependent)

6)
#include<stdio.h>
int main()
{
   int i=0;
   if(i++)
  printf("%d\n",i);
  printf("%d\n",i);
  return 0;
}

Sol.: 1

7)
#include<stdio.h>
int main()
{
   int i=2,j=1,k=4,r1,r2;
   r1=(i,j,k,--k);
   r2=i,j,k;
   printf("%d,%d\n",r1,r2);
  return 0;
}

Sol.: 3,2(NOTE:r1 will get the value from --k and r2 will get the value from i)

8)
#include<stdio.h>
int x(void)
{
  printf("x");
  return 2;
}
int y(void)
{
  printf("y");
  return 1;
}
int z(void)
{
  printf("z");
  return 0;
}
int main()
{
  int r;
  r=z() && y();
  printf("%d\n",r);
  return 0;
}

Sol.:z0

9)
#include<stdio.h>
int x(void)
{
 printf("x");
  return 2;
}
int y(void)
{
  printf("y");
  return 1;
}
int z(void)
{
  printf("z");
  return 0;
}
int main()
{
  int r;
  r=z() || x();
  printf("%d\n",r);
  return 0;
}

Sol.: zx1

10)
#include<stdio.h>
int main()
{
   int i=1,2;
   printf("%d\n",i);
   return 0;
}

Sol.: Compile time error

11)
#include<stdio.h>
int main()
{
   int i,j=1,k=0;
   i=(j++,++k ||--k);
   printf("%d,%d,%d\n",i,j,k);
   return 0;
}

Sol.: 1,2,1(NOTE:--k will not get evaluated)

12)
#include<stdio.h>
int main()
{
   int i=- -3,j=-(-4);
   printf("%d,%d\n",i,j);
   return 0;
}

Sol.: 3,4

13)
#include<stdio.h>
int main()
{
  printf("abc");
  printf("\b\b\bsi");
  printf("\rha");
  return 0;
}

Sol.: ha

14)
#include<stdio.h>
int main()
{
   int n=0;
   switch(n)
   {
     case 0: n+=1;
     case 1: n+=2;
     default: n+=3;
   }
   printf("%d",n);
   return 0;
}

Sol.: 6

15)
#include<stdio.h>
int main()
{
   int i,j;
   scanf("%d%d",&i,&j);
  i+=2;
  j-=2;
  if((i=5) && (j=-3))
   printf("yes\n");
  else
   printf("no\n");
   return 0;
}
Under what circumstances would the above program prints yes?

Sol.: Under every circumstances.



Expression(LEVEL-II)

1)
#include<stdio.h>
int main()
{
  int i=1,j;
  j=i--&&++i/2||++i%7;
  printf("%d,%d\n",i,j);
  return 0;
}

Sol.: 2,1

2)
#include<stdio.h>
int main()
{
  int a=2,b=3;
  a=a+b-(b=a);
  printf("%d,%d\n",a,b);
  return 0;
}

Sol.: This is compiler dependent.

3)
#include<stdio.h>
int main()
{
  int i=0,j=2,k=0;
  if(i && (j=k--))
  k=1;
  printf("%d,%d,%d",i,j,k);
  return 0;
}

Sol.: 0,2,0(NOTE:j=k-- will not get evaluated)


4)#include<stdio.h>
int main()
{
   int i=3,j=-2,k;
   k=sizeof(1,--i + ++j);
   printf("%d,%d,%d\n",i,j,k);
   return 0;
}

Sol.: 3,-2,4(NOTE:Expressions inside sizeof() will not be evaluated because sizeof() is an operator)


5)
#include<stdio.h>
int main()
{
  int a=1;
  switch(a)
  {
    int b=2;
    case 1:
    printf("b=%d\n",b);
    break;
    default:
    printf("b=%d\n",b);
     break;
 }
 return 0;
}

Sol.: Garbage value(Inside switch the control will immideately go for case and will not read b)

6)
#include<stdio.h>
int main()
{
   int i=-2,r;
   r=++i++;
   printf("%d\n",r);
   return 0;
}

Sol.: error: lvalue required as increment operand

7)
#include<stdio.h>
int main()
{
   int cnt=5,a=1000;
    do
   {
     a/=cnt;
    }while(cnt--);
   printf("%d\n",a);
   return 0;
}

Sol.: Floating point exception(Note:a is divided by 0 in the last iteration)

8)
#include<stdio.h>
int main()
{
   int i=-5,j=2,k=-3,r;
   r=i++&&(--j||k++);
   printf("%d,%d,%d,%d\n",i,j,k,r);
   return 0;
}

Sol.: -4,1,-3,1

9)Which one of the following expressions should be written in the blank so that the following program prints Yes as output?
#include<stdio.h>
int main()
{
   int i=1;
    if(____,i--)
  printf("Yes\n");
   else
   printf("No\n");
  return 0;
 }

Sol.: i++,++i

10)
int add(int i,int j)
{
  return i+j;
}
#include<stdio.h>
int main()
{
  int i=5;
  printf("%d,%d\n",++i,add(i,2));
  return 0;
}

Sol.: 6,7 or 6,8 (compiler dependent)

11)
#include<stdio.h>
int p(void)
{
  printf("p");
  return 20;
}
int q(void)
{
  printf("q");
  return -10;
}
int r(void)
{
  printf("r");
  return 0;
}
What logical operators should be placed in the blanks of the following program such that it produces the output pqr?
int main()
{
  !p()__q()__r();
  return 0;
}

Sol.: !p()||q()&&r()

12)
#include<stdio.h>
int main()
{
   int i=0,j=199;
   while(i<j)
   {
     --j;
     i+=2;
     }
 printf("%d\n",i-j);
 return 0;
 }

Sol.: 2

13)
#include<stdio.h>
int main()
{
  int i=-1,j=-2,k;
  k=-i++ * ++j;
  printf("%d,%d,%d\n",i,j,k);
  return 0;
}

Sol.: 0,-1,-1(Compiler dependent code)

14)
int x=-3;
int f(void)
{
  x=x+2;
  return x;
}
int g(void)
{
  x=x*3;
  return x;
}
#include<stdio.h>
int main()
{
 printf("%d\n",f()+g());
 return 0;
}

Sol.: -4 or -16(compiler dependent)

15)
#include<stdio.h>
int main()
{
  int i=12,j=3,k;
  k=i+++j;
  printf("%d,%d,%d\n",i,j,k);
  return 0;
}

Sol.: 13,3,15

---------------------------------------------------------------------------------------------------------------------------------

STORAGE CLASSES(LEVEL-I)

1)
#include<stdio.h>
int f(void)
{
 int a=5;
 return a;
}
main()
{
 int b=2;
 b=f();
 printf("%d",b);
}

Sol.: 5

2)
#include<stdio.h>
main()
{
 static int a=2;
 if(a)
 {
  printf("%d\n",a);
  a--;
  main();
 }
}

Sol.: 2,1

3)
#include<stdio.h>
static int a;
int b;
main()
{
 a++,b++;
 printf("%d,%d",a,b);
}

Sol.: 1,1

4)
#include<stdio.h>
void f()
{
 a=10;
 printf("%d",a);
}
int a=5;
main()
{
 a++,b++;
 printf("%d,%d",a,b);
 f();
}

Sol.: Compiler Error

5)
#include<stdio.h>
char f()
{
 static char c='A';
 c+=2;
 return c;
}
main()
{
 f();
 printf("%c",f());
}

Sol.: E

6)
#include<stdio.h>
main()
{
 int i=2;
 {
  int i=3;
  i++;
  printf("%d\n",i);
 }
 printf("%d",i);
}

Sol.: 4,2

7)
#include<stdio.h>
int i=0;
void g()
{
 i=100;
 i++;
}
main()
{
 i++;
 g();
 i+=2;
 g();
 printf("%d",i);
}

Sol.: 101

8)
#include<stdio.h>
main()
{
 register int a=2;
 register float c=3.4;
 printf("%d,%f",a,c);
}

Sol.: 2,3.4000

9)
#include<stdio.h>
main()
{
 int a=2;
 if(a)
 {
  printf("%d\n",a);
  a--;
  main();
 }
}

Sol.: infinite printing of 2.

10)
#include<stdio.h>
int i=-3;
void f(int i)
{
 i++;
 printf("%d\n",i);
}
main()
{
 int i=2;
 f(i);
 printf("%d",i);
}

Sol.: 3,2

11)
#include<stdio.h>
int a=1,b=3;
void f()
{
 printf("%d,%d",a,b);
}
main()
{
 int a=-1,b=2;
 printf("%d,%d\n",a,b);
 f();
}

Sol.: -1,2
      1,3

12)
main()
{
 printf("A");
 main();
}

Sol.: Infinite printing of A

13)
Consider the following 'C' code in two files.
        f1.c
#include<stdio.h>
int i=2;
extern void f();
main()
{
 f();
 printf("%d",i);
}
        f2.c
extern int i;
void f()
{
 i++;
}
What would be the output after linking and execution of above two files?

Sol.: 3

14)
Consider the following 'C' code in two files.
        f1.c
#include<stdio.h>
int i=-2;
extern int f();
main()
{
 printf("%d",f());
}
        f2.c
extern int i;
int f()
{
 static int i;
 i++;
 return i;
}
What would be the output after linking and execution of above two files?

Sol: 1

15)
Consider the following 'C' code in two files.
        f1.c
#include<stdio.h>
extern int i;
main()
{
 i++;
 printf("%d\n",f());
 printf("%d",i);
}
        f2.c
static int i;
int f()
{
 i++;
 return i;
}
What would be the output after linking and execution of above two files?

Sol.: Link time error

16)
#include<stdio.h>
main()
{
 extern int p;
 printf("%d\n",p);
}

Sol.: Link time error

17)
#include<stdio.h>
extern int p;
void f(void)
{
 printf("%d",++p);
}
static int p=4;
main()
{
 f();
}

Sol.: Compile time error

18)
#include<stdio.h>
int f(int p)
{
 return (p%2)?2:f(p/2);
}
static int p=4;
main()
{
 printf("%d",f(36));
}

Sol.: 2

19)
int q=2;
void fun()
{
 int *p=(int*)malloc(q*sizeof(int));
 return p;
}
Which part of the memory would be accessed on execution of above program?

Sol.: q->data segment, p->stack segment, malloc()->heap segment.



STORAGE CLASSES(LEVEL-II)

1)
#include<stdio.h>
main()
{
 int i,r=0;
 int f();
 for(i=0;i<4;i++)
  r+=f();
 printf("%d",r);
}
int f(void)
{
 static int x=-2;
 return x++;
}

Sol.: -2

2)
Consider the  'C' code in the following two files:
        f1.c
#include<stdio.h>
extern int i;
extern int f();
main()
{
 i++;
 printf("%d\n",f());
 printf("%d",i);
}
        f2.c
int i;
static int f()
{
 i++;
 return i;
}
What would be the output after linking and execution of above two files?

Sol: Link time error.

3)#include<stdio.h>
int p=65;
main()
{
 char p;
 printf("%d",p);
}

Sol.: garbage value

4)
#include<stdio.h>
int g(int x)
{
 if(x%2)
  return g(x-1);
 return x/2;
}
main()
{
 printf("%d",g(11));
}

Sol.: 5

5)
#include<stdio.h>
int recursionMethod(int x)
{
 int call;
 if(x!=-1)
 {
  call=recursionMethod(x-1)+1;
  printf("%d",call);
 }
 return x;
}
main()
{
 recursionMethod(5);
}

Sol.: 012345

6)
#include<stdio.h>
int f(int k)
{
 if(k>=0)return 1;
 return f(k+1)+f(k+2);
}
main()
{
 f(-2);
}
How many calls to function 'f' are made while executing the above program?
Sol.: 5(NOTE: f(-2) will call f(-1) & f(0). f(-1) will call f(0) & f(1))

7)
#include<stdio.h>
main()
{
 int a=-1,b=2,r;
 int f(int);
 r=f(a+b);
 printf("%d,%d,%d",a,b,r);
}
int b=-4;
int f(int a)
{
 a++;
 b++;
 return a+b;
}

Sol.: -1,2,-1

8)
#include<stdio.h>
int zap(int n)
{
 int s=0;
 if(!n) return 0;
 s+=n%10;
 zap(n/10);
 return s;
}
main()
{
 printf("%d",zap(12345));
}

Sol.: 5

9)
Compare the following 'c' code in two files:
        f1.c
#include<stdio.h>
main()
{
 double x,y,z;
 x=3.5;
 y=4.2;
 z=f(x,y);
}
        f2.c
double f(double a,double b)
{
 return b-a;
}
Does the linking and execution of above two files result in any errors. If yes what changes do you suggest to eliminate them?

Sol.: No error while linking and execution

10)
#include<stdio.h>
int f(int n)
{
 if(n<=1) return n;
 return f(n-1)+f(n-2)+f(n-3);
}
main()
{
 printf("%d",f(5));
}

Sol.: 3

11)
#include<stdio.h>
static int v=5;
void f(int v)
{
 {
  static int v=0;
  for(;v<5;v++)
   printf("%d\n",v);
 }
}
main()
{
 while(v--) f(v);
 printf("%d\n",v);
}

Sol.: 0
      1
      2
      3
      4
      -1

12)
Fill up the blank in the following program such that it prints 15 on the screen.
#include<stdio.h>
int sample(int i)
{
 static int s=0;
 if(i==2) return s;
  s+=5;
 i++;
 return sample(i);


}
main()
{
 printf("%d\n",sample(__));
}

Sol.: -1

----------------------------------------------------------------------------------------------------------------------------------



ARRAYS(LEVEL-I)

1)
main()
{
 int a[]={1,2,3,4};
 printf("%d,%d",*a+2,*(a+1));
}

Sol.: 3,2

2)
main()
{
 int a[]={1,2,3};
 int b[3][4]={1,5,3,6};
 printf("%d,%d\n",sizeof(a),sizeof(b));
}

Sol.: 12,48

3)
main()
{
 float a[2];
 printf("%u,%u,%u,%u\n",a,a+1,&a,&a+1);
}

Sol.: 1000,1004,1000,1008

4)
main()
{
 int a[2][3]={
        {3,2,1},
        {6,5,4}
         };
 int (*ap)[3];
 ap=a;
 printf("%u",ap);
 ap++;
 printf("%u",ap);
 printf("%d,%d",(*ap)[0],(*ap)[1]);
}

Sol.: 1000,1012,6,5

5)
main()
{
 int i,r=0;
 int a[10]={1,2,-3,-4};
 for(i=0;i<10;++i)
  if(a[i]%2) r+=a[i];
 printf("%d",r);
}

Sol.: -2(NOTE: if an array is partially initialize then all its left element becomes 0)

6)
main()
{
 unsigned int a[2][3]={ {3,2,1},
            {6,5,4}
              };
 printf("%u,%u,%u,%u\n",a,a+1,&a,&a+1);
}

Sol.: 1000,1012,1000,1024

7)
main()
{
 int a[]={1,2,3,4,5};
 int *p=a;
 printf("%d,%d",p[1],p[2]);
}

Sol.: 2,3

8)
main()
{
 static int a[2][3][4];
 int (*ap)[3][4];
 ap=a;
 printf("%u\n",ap);
 ap++;
 printf("%u",ap);
}

Sol.: 1000,1048

9)
main()
{
 int a[]={1,-1,2,-2,3,-3,4,-4,5,-5};
 int *p=a+1;
 int *q=a+6;
 printf("%d",q-p);
}

Sol.: 5

10)
main()
{
 int a[]={1,2,3,4,5};
 int p,*s;
 s=a;
 printf("%d\n",*(s++));
 p=*++s;
 printf("%d",p);
}

Sol.: 1,3

11)
int f(int*p)
{
 int i,s=0;
 for(i=0;i<sizeof(p);i++)
  s+=(p+i);
 return s;
}
main()
{
 static int a[5]={1,-2,3,-4,5};
 int r;
 r=f(a);
 printf("%d",r);
}

Sol.: -2

12)
int a[2][3]={{1}};
main()
{
 int b;
 b=a[0][0]+a[0][2]+a[1][3];
 printf("%d",b);
}

Sol.: undefine behaviour

13)
main()
{
 int i,r=0;
 static int a[5]={0x1,0x2,0x10,0x20,0x30};
 for(i=0;i<5;++i)
  if(!(i%2)) r+=a[i];
 printf("%d",r);
}

Sol.: 65

14)
main()
{
 int i,r=0;
 for(i=0;i<10;++i)
  if(!a[i]) r+=(!a[i]);
 printf("%d",r);
}

Sol.: 5

15)
#define rows 2
#define cols 3
void test(int y[][cols]);
main()
{
 static int a[rows][cols]={-1,2,-3,4,-5,6};
 test(a);
}
void test(int x[][cols])
{
 int i,j,k;
 for(j=0;j<cols;++j)
 {
  k=0;
  for(i=0;i<rows;++i)
   if(x[i][j]>k) k=x[i][j];
  printf("%d\n",k);
 }
}

Sol.: 4,2,6


16)
main()
{
 int b=0;
 int a[2][3]={{1,2}};
 b=a[0][1]+a[0][2]+a[1][2];
 printf("%d",b);
}

Sol.: 2

17)
void foo(int n,int a[])
{
 n=0;a[n]=3;
 n=1;a[n]=9;
 n=2;a[n]=27;
 n=3;a[n]=81;
}
main()
{
 int n=1,a[]={2,4,8,16};
 foo(n,a);
 printf("%d",a[n]);
}

Sol.: 9

18)
main()
{
 float a[5];
 printf("%u",&a[3]-3);
}

Sol.: 1000

19)
main()
{
 int a[2][3]={-1,-2,0,1,2,3};
 printf("%d",sizeof((int*)a));
}

Sol.: 4

20)
main()
{
 int a[]={10,20,30,-30,-20,-10};
 int *b=&a[4];
 printf("%d",b[-3]);
}

Sol.: 20




ARRAYS(LEVEL-II)

1)
#include<stdio.h>
int a[2][3]={010,020,030,040,050};
main()
{
 int i,j,min=100;
 for(i=0;i<2;++i)
  for(j=0;j<3;++j)
   if(a[i][j]<min) min=a[i][j];
 printf("%d",min);
}

Sol.: 0(NOTE:last element will be zero)

2)
#include<stdio.h>
int array[]={23,34,12,17,204,99,16};
main()
{
 unsigned d;
 for(d=-1;d<=5;d++)
  printf("%d\n",array[d+1]);
}

Sol.: No output

3)
#include<stdio.h>
main()
{
 int a[]={1,2,3,4,5},b;
 b=(char*)(a+5)-(char*)a;
 printf("%d",b);
}

Sol.: 20(NOTE:this is the no. of bytes between 5th element of a and 0th element)

4)
Fill up the blank in the following program such that it prints the address of element 5 without subscription operator([])
#include<stdio.h>
main()
{
 unsigned int a[3][3]={
            {1,2,3},
            {2,3,4},
            {3,4,5}
              };
 printf("%d",_____);
}

Sol.: (*a)+4

5)
#include<stdio.h>
int a[]={1,2,3};
int* f(void)
{
 int i;
 for(i=0;i<3;i++)
  return a+1;
}
main()
{
 *f()=5;
 printf("%d,%d,%d",a[0],a[1],a[2]);
}

Sol.: 1,5,3(Note: f will return address of 2nd array variable and that is made equal to 5 in main function.

6)
#include<stdio.h>
void f(float(*a)[3])
{
 printf("%d",sizeof(a[0]));
}
main()
{
 float a[2][3];
 f(a);
}

Sol.: 12

7)
Fill in the blank in the following program s.t. the contents of cell a[1][1] will become 8
#include<stdio.h>
void f(char *a)
{
 ________________
}
main()
{
 int a[2][3]={
        {1,2,3},
        {4,5,6}
         };
 f((char*)a);
 printf("%d",a[1][1]);
}

Sol.: *(a+4*(1*3+1))=8;

8)
#include<stdio.h>
int arr[2][3]={
        {1,2,3},
        {-1,-2,-3}
          };
main()
{
 int i=2;
 int *a=(int*)&arr;
 printf("%d",(a++)[i++]++);
}

Sol.: 3

9)
#include<stdio.h>
main()
{
 int a[][4]={
        {1,2,3},
        {4,5,6},
        {7,8,9}
        };
 int b;
 b=(char*)a+2-(char*)a;
 printf("%d",b);
}

Sol.: 2

10)
main()
{
 int a[2][2][3]={
         {
          {1,2,3},
          {4,5,6},
         },
         {
          {7,8,9},
          {6,5,4}
         }
        };
 printf("%d,%d",*(*(*(a+1))),*(a[0][1]+2));
}

Sol.: 7,6

11)What changes do you suggest in the following program so that it displays 5,10,15 on the screen. Try to make required changes as minimal as possible.
#include<stdio.h>
int a[]={1,2,3};
int* f(void)
{
 int i;
 return a + i++;
}
main()
{
 *f()=5;
 *f()=10;
 *f()=15;
 printf("%d,%d,%d",a[0],a[1],a[2]);
}

Sol.: in function f we will declayer int i as static int i.

12)
#include<stdio.h>
main()
{
 int a[2][3]={
        {2,3,4},
        {5,6,7}
         };
 int *pa[2]={a[0],a[1]};
 printf("%d,%d",pa[0],*pa[1]);
}

Sol.: 1000,5

13)
#include<stdio.h>
main()
{
 int a[][3]={
        {-1,2,3},
        {-4,5,6}
         };
 printf("%d",*(*(a+1)-1)+2);
}

Sol.: 5(3+2)

14)
#include<stdio.h>
main()
{
 int a[2][3]={
        {2,3,4},
        {5,6,7}
         };
 int *p=a[0];
 printf("%d",p[2]);
}

Sol.: 4

15)
#include<stdio.h>
void f(int a[][4])
{
 printf("%d",*a[0]);
}
main()
{
 int a[2][4]={
        {1,2,3,4},
        {5,6,7,8}
         };
 f(a);
}

Sol.: 1

-------------------------------------------------------------------------------------------------------------------------------

POINTERS(Level-I)


1)
#include<stdio.h>
main()
{
 int a=-10,b=-2;
 int c,*pa,*pb;
 pa=&a;
 *pa=2*a;
 pb=&b;
 c=3*(*pb-*pa);
 printf("%d,%d",a,c);
}

Sol.: -20,54

2)
#include<stdio.h>
main()
{
 char x='A',y;
 char *px=&x,*py;
 *px=x+1;
 y=*px+1;
 printf("%c",y);
}

Sol.: C

3)
#include<stdio.h>
main()
{
 int r,*p=NULL;
 r=*p++;
 printf("%d",r);
}

Sol.: Segmentation fault(Run time error).

4)
#include<stdio.h>
char f(char s,char t)
{
 s='B';
 t='A';
 return s<t?s:t;
}
main()
{
 char a='A',b='B',r;
 r=f(a,b);
 printf("%d,%d,%c",a,b,r);
}

Sol.: 65,66,A

5)
#include<stdio.h>
char f(char *s,char *t)
{
 *s='A';
 *t='B';
 return *s==*t?*s:*t;
}
main()
{
 char a='x',b='y',r;
 r=f(&a,&b);
 printf("%c,%c,%d",a,b,r);
}

Sol.: A,B,66

6)
#include<stdio.h>
#include<malloc.h>
int f(int *p)
{
 int i,s=0;
 for(i=0;i<sizeof(p);i++)
  s+=*(p+i);
 return s;
}
main()
{
 int i,r;
 static int *a;
 a=malloc(5*sizeof(int));
 for(i=0;i<5;i++)
  *(a+i)=i+1;
 r=f(a);
 free(a);
 printf("%d",r);
}

Sol.: 10

7)
#include<stdio.h>
main()
{
 int i=65;
 int *p;
 p=i;
 p++;
 printf("%d",p);
}

Sol.: 69(NOTE:at p=i warning: assignment makes pointer from integer without a cast [enabled by default]
              at printf("%d",p) warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat] )

8)
#include<stdio.h>
main()
{
 int *x;
 *x=100;
 printf("%d",*x);
}

Sol.: Segmentation fault(Run time error)

9)
#include<stdio.h>
void f1(int *k)
{
 *k+=15;
}
void f2(int *x)
{
 int m=*x,*n=&m;
 *n*=10;
}
main()
{
 int i=20,*j=&i;
 f1(j);
 *j+=10;
 f2(j);
 printf("%d,%d",i,*j);
}

Sol.: 45,45

10)
#include<stdio.h>
main()
{
 short int q=351;
 unsigned char *p=(unsigned char *)&q;
 printf("%d,",*p);
 *p+=5;
 printf("%d",q);
}

Sol.: 95,356(NOTE: 95=351-256)

11)
#include<stdio.h>
main()
{
 float x,*p,*q;
 p=q=&x;
 q++;
 printf("%d",p-q);
}

Sol.: -1

12)
#include<stdio.h>
main()
{
 int *p,q=5;
 p=&q;
 ++*p=6;
 printf("%d",*p);
}

Sol.: Compile time error(lvalue required as left operand of assignment at ++*p=6)

13)
#include<stdio.h>
main()
{
 int a=4,b=5;
 void *p=&a,*q=&b;
 p++;
 printf("%u,%u",p,*q);
}

Sol.: Compile time error

14)
#include<stdio.h>
#include<malloc.h>
main()
{
 unsigned int *p,*q;
 p=malloc(10);
 q=calloc(10,1);
 printf("%d,%d",*p,*q);
}

Sol.:0,0

15)
#include<stdio.h>
#include<malloc.h>
void alloc(int *a)
{
 a=(int*)malloc(sizeof(int));
}
main()
{
 int *a;
 alloc(a);
 *a=6;
 printf("%d",*a);
}

Sol.: Segmentation Fault(Run Time Error)

16)
#include<stdio.h>
main()
{
 int *p;
 {
  int q=2;
  p=&q;
 }
 printf("%d",*p);
}

Sol.: 2



POINTERS(LEVEL-II)

1)What is the difference between following declerations?
int* f()
int (*g)()

Sol.: int* f() is the decleration of a function f that will return integer pointer.
      Whereas int (*g)() is the decleration of a function pointer that will return int.

2)
#include<stdio.h>
void test(void)
{
 printf("Algorithmica\n");
}
main()
{
 void (*f)(void);
 f=test;
 (*f)();
 f();
}

Sol.: Algorithmica
      Algorithmica

3)
Explain the meaning of the following declerations:
a. double *(*a[10])(double)
b. char (*p)(int(*a)[3])
c. float (*x(int*a))[5]

Sol.: a) it is an array of 10 variable which will contain pointer to a function which will receive double and return double pointer
      b) it is a pointer to a function which will receive adress of an int array of 3 size and will return char
      c)

4)
#include<stdio.h>
void g()
{
 printf("hi");
}
void h(void(*f)())
{
 printf("%d\n",sizeof(f));
 (*f)();
}
main()
{
 h(g);
}

Sol.: 4
      hi

5)
#include<stdio.h>
#include<malloc.h>
main()
{
 int *a,i;
 a=(int*)malloc(5*sizeof(int));
 for(i=0;i<5;i++)
  *(a+i)=i*i;
 for(i=0;i<5;i++)
  printf("%d,",*a++);
 free(a);
}

Sol: 0,1,4,9,16,

6)
#include<stdio.h>
int *f()
{
 int a=6,*b=NULL;
 b=&a;
 return b;
}
main()
{
 int *p=NULL;
 p=f();
 printf("%d",*p);
}

Sol.: 6

7)
#include<stdio.h>
void f()
{
 printf("f");
}
void g()
{
 printf("g");
}
void h()
{
 printf("h");
}
main()
{
 int i;
 void f(),g(),h();
 _________________
 for(i=0;i<=2;i++)
  (*t[i])();
}
What statements should be added to the above code so that the output will be fgh?

Sol.: void(*t[3])()={f,g,h};

8)
What does following function do?
int test(unsigned int n)
{
 unsigned char* p=(unsigned char*)&n;
 return *p&&*(p+1)&&*(p+2)&&*(p+3);
}

Sol.: this function will do && of all the 4 bytes of n and will return the value.

9)
#include<stdio.h>
int m=6;
void foo(int *p)
{
 ++*p++;
 p=&m;
 ++*p++;
}
main()
{
 int n=5;
 int *ptr=&n;
 foo(ptr);
 printf("%d,%d",m,n);
}

Sol.: 7,6(NOTE:here binding will be ++(*(p++)) )

10)
#include<stdio.h>
void f(int *p,int *q)
{
 int *temp;
 temp=p;
 p=q;
 q=temp;
}
main()
{
 int a=10,b=20;
 int *p,*q;
 p=&a;
 q=&b;
 f(p,q);
 printf("a=%d,b=%d,*p=%d,*q=%d",a,b,*p,*q);
}

Sol: a=10,b=20,*p=10,*q=20(NOTE: Changes done by f will not get reflected on main)

11)
#include<stdio.h>
void test(unsigned char *p)
{
 unsigned char tmp=*p;
 *p=*(p+1);
 *(p+1)=tmp;
}
main()
{
 unsigned short int i=2;
 test(&i);
 printf("%u",i);
}

Sol.:512(NOTE: short int contain 2 bytes. Here test will swap the position of both bytes)

12)How do you declare an array of 10 pointers to functions returning pointers to functions returning pointers to characters?

Sol.:

13)
Fillup the blank in the following program so that contents of x & y are swapped without using temp variable.

Sol.:
#include<stdio.h>
void swap(int *a,int *b)
{
 *a=(*a)^(*b);
 *b=(*a)^(*b);
 *a=(*a)^(*b);
}
main()
{
 int x=4,y=3;
 swap(&x,&y);
 printf("%d,%d",x,y);
}

14)
#include<stdio.h>
#include<malloc.h>
main()
{
 int i,*p;
 for(i=0;i<10;i++)
  p=malloc(1024);
}

Sol.: This program will execute sucessfully but in every itteration of for loop a new chunk of memory will be allocated from heap segment which will go unused.

-------------------------------------------------------------------------------------------------------------------------------

STRINGS(LEVEL-I)


1)
main()
{
 char p[]="java";
 char q[]={'j','a','v','a'};
 char *r="java";
 printf("%d,%d,%d",sizeof(p),sizeof(q),sizeof(r));
}

Sol.: 5,4,4

2)
main()
{
 char s[]="c\0java";
 char p[]="abc\0\0";
 printf("%s,%s",s,p);
}

Sol.: c,abc

3)
main()
{
 char s[]="language\0\0\0";
 printf("%d,%d",sizeof(s),sterlen(s));
}

Sol.: 12,8

4)
main()
{
 char s[]="c language";
 printf("%s\n",s+2);
 printf("programming"+3);
}

Sol.: language
      gramming

5)
main()
{
 char *s="yahoo";
 printf("%d,%d",sizeof("yahoo"),sizeof(*s));
}

Sol.: 6,1

6)
main()
{
 char a[10];
 a="down";
 printf("%s",a);
}

Sol.: Error:Lvalue required

7)
main()
{
 char str1[]="program";
 if(str1=="program")
  printf("Equal");
 else
  printf("Unequal");
}

Sol.: Unequal

8)
main()
{
 char *str1="program";
 if(str1=="program")
  printf("Equal");
 else
  printf("Unequal");
}

Sol.: Equal

9)
main()
{
 char *str1="program";
 char *str2="program";
 if(str1==str2)
  printf("Equal);
 else
  printf("Unequal");
}

Sol.: Equal

10)
main()
{
 char a[3]="xyz";
 printf("%s",a);
}

Sol.: Undefined behaviour

11)
main()
{
 char s[]="Algorithmica";
 printf("%c\n",(*s)++);
 printf("%c",++*s);
}

Sol.: AC

12)
main()
{
 static char s[]="Algorithmica";
 printf("%c",*(s+strlen(s)));
}

Sol.: Not display any thing

13)
main()
{
 char str[10];
 int i;
 for(i=0;i<3;i++)
  i[str]='Z'-i;
 i[str]='\0';
 printf("%s",str);
}

Sol.: ZYX

14)
main()
{
 char str[10];
 int i=0;
 i++[str]='B'+1;
 i++[str]='C';
 i[str]='\0';
 printf("%s",str);
}

Sol.: CC

15)
main()
{
 char s[]="Algorithmica";
 printf("%s",&s[10]-2);
}

Sol.: mica

16)
main()
{
 char *months[4]={ "january",
           "february",
           "march",
           "april"
         };
 printf("%c",months[1][2]);
}

Sol.: b

17)
main()
{
 char *a="down";
 a[0]='D';
 printf("%s",a);
}

Sol.: run time error

18) What will be the o/p of following program, if string liteeral "abcdef" starts at address 1000.
main()
{
 printf("%u",&"abcdef"+1);
}

Sol.: 1007

19)
main()
{
 char a[] ="language";
 char p,*s;
 s=a;
 printf("%c\n",*(s++));
 p=*++s;
 printf("%c",p);
}

Sol.: l
      n

20)
void f(char a[])
{
 a[0]='A';
}
main()
{
 f("ada");
}

Sol.: Run Time error




STRINGS(LEVEL-II)

1)
#include<stdio.h>
main()
{
 char c;
 c="abcdef"[3]-3["abcdef"];
 printf("%c",c);
}

Sol.: No output(c will have 0 value)

2)
#include<stdio.h>
main()
{
 char *days[4]={"sunday",
        "monday",
        "tuesday",
        "wednesday"
           };
 printf("%s,%s",*(days+2),*days+1);
}

Sol.: tuesday,unday

3)a)
#include<stdio.h>
main()
{
 char *days[4]={"sunday",
        "monday",
        "tuesday",
        "wednesday"
           };
 printf("%s,%s",*(days+2),*days+1);
}

Sol.: Segmentation fault(Run time error)

3)b)
#include<stdio.h>
#include<string.h>
main()
{
 char *s="NULL";
 if(strcmp(s,NULL))printf("Yes");
 else printf("No");
}

Sol.: Segmentation fault

4)
#include<stdio.h>
#include<string.h>
#include<malloc.h>
void alloc(char *s1)
{
 s1=malloc(20);
 memset(s1,'\0',20);
}
main()
{
 char *s1,*s2="down";
 alloc(s1);
 strcat(s1,s2);
 printf("%s",s1);
}

Sol.: Seg. fault

5)
#include<stdio.h>
main()
{
 switch("c"=="c")
 {
  case 0:
   printf("alas!");
   break;
  case 1:
   printf("hurray");
   break;
  default:
   printf("superb");
 }
}

Sol.: hurray

6)Fill up the blank in the following program such it outputs b onto screen.
#include<stdio.h>
void f(char*s)
{
 printf("%c",____);
}
main()
{
 char a[]="abcdefgh";
 f(a+5);
}

Sol.: *(s-4)

7)
#include<stdio.h>
#include<string.h>
void f(char s[])
{
 printf("%d,%d\n",sizeof(s),strlen(s));
}
main()
{
 char s[]="abcde";
 f(s);
 f("algorithmica");
}

Sol.: 4,5
      4,12

8)
#include<stdio.h>
#include<string.h>
main()
{
 char *str1="Lang";
 char str2[20];
 char str3[30]="Comp";
 if(strcmp(strcat(str3,strcpy(str2,str1)),"Comp.Lang"))
  printf("Yes");
 else
  printf("No");
}

Sol.: Yes

9)
#include<stdio.h>
#include<string.h>
void f(char *s)
{
 s="New string";
}
main()
{
 char *s="Old string";
 f(s);
 printf("%s",s);
}

Sol.: Old string

10)
#include<stdio.h>
#include<string.h>
main()
{
 char s[][5]={
        {'a','b','c','d'},
        {'w','x','y','z'}
         };
 printf("%d",sizeof(s[0]));
}

Sol.: 5

11)
#include<stdio.h>
#include<string.h>
main()
{
 char s[][5]={
        {'a','b','c'},
        {'x','y','z'}
         };
 printf("%c",**s[1]);
}

Sol.: Compilation error

12)
#include<stdio.h>
#include<string.h>
main()
{
 char s1[]="xyz";
 char *s2="xyz";
 if(s1==s2)
  printf("Equal");
 else
  printf("Unequal");
}

Sol.: Unequal.Change if statement to 'if(strcmp(s1,s2)==0)'

13)
#include<stdio.h>
#include<string.h>
void f(char **days)
{
 printf("%c",*(days[2]+2));
}
main()
{
 char *days[4]={"sunday",
        "monday",
        "tuesday",
        "wednesday"
           };
 f(days);
}

Sol.: e

14)Fill up the blank in the following program such that it will print m as output
#include<stdio.h>
#include<string.h>
void f(char *p[])
{
 printf("%c",____);
}
main()
{
 char *months[4]={"january",
        "february",
        "march",
        "april"
           };
 f(months);
}

Sol.: *(p[2])

15)
#include<stdio.h>
#include<string.h>
main()
{
 char bool[][6]={"true","false"};
 printf("%s",bool[(unsigned)-1 == ~0]);
}

Sol.: false

16)
#include<stdio.h>
#include<string.h>
int f(char *str)
{
 int i,j,len;
 len=strlen(str);
 for(i=0;i<len;i++)
 {
  char tmp;
  str[i]^=str[len-i-1];
  str[len-i-1]^=str[i];
  str[i]^=str[len-i-1];
 }
 printf("%s",str);
}
main()
{
 char s[]="ADA";
 f(s);
}

Sol.: A(Note: D& A(first) will xor with itself and create '\0'and A which is printed is the last A which after final itteration comes to 1st position)

17)
#include<stdio.h>
#include<string.h>
char* f(void)
{
 char *s="c++";
 return s;
}
main()
{
 char *p="java";
 p=f();
 printf("%s",p);
}

Sol.: c++

18)
#include<stdio.h>
#include<string.h>
main()
{
 char *days[]={"sunday","monday","tuesday"};
 printf("%d,%d,%d",sizeof(days),sizeof(days[0]),sizeof(*days[0]));
}

Sol.: 24,4,1(Note:tuesday will determine the size of each array as it is the longest)

19)
#include<stdio.h>
#include<string.h>
char* f(void)
{
 char s[]="c";
 return s;
}
main()
{
 char *p="java";
 p=f();
 printf("%s",p);
}

Sol.: Garbage output

----------------------------------------------------------------------------------------------------------------------------------

INPUT & OUTPUT(LEVEL-I)

1)
#include<stdio.h>
main()
{
 int i=10,b=20
 printf("%d");
}

Sol.: compiler error or unpredictable output(depend upon compiler)

2)
#include<stdio.h>
main()
{
 int a;
 char b;
 scanf("%d%c",&a,&b);
 printf("a=%d,b=%c",a,b);
}

Sol: a will take the first integer entered through keyboard and b will have white space character

3)
What is the o/p of the following program if the i/p provided is 25 a from keyboard
#include<stdio.h>
main()
{
 int a;
 char b;
 scanf("%d\n%c",&a,&b);
 printf("a=%d,b=%c",a,b);
}

Sol.: a=25,b=a

4)
#include<stdio.h>
main()
{
 int i;
 for(i=8;i<12;i++)
  printf("%2d",i);
}

Sol.:  8 91011

5)How do you give input to the program sothat it reads and displays the date properly.
#include<stdio.h>
main()
{
 int d,m,y;
 scanf("%d-%d-%d",&d,&m,&y);
 printf("%d-%d-%d",d,m,y);
}

Sol.: 6-6-1986

6)What does the scanf function in the following program trying to do?
#include<stdio.h>
main()
{
 char buf[50];
 scanf("%[^\n]",buf);
 printf("%s",buf);
}

Sol.: It will scan with all the white character

7)
#include<stdio.h>
main()
{
 printf("%d"+1,245);
}

Sol.: d

8)
#include<stdio.h>
main()
{
 int r;
 r=printf("stupid")+printf("program");
 printf("%d",r);
}

Sol.: stupidprogram13

9)
#include<stdio.h>
main()
{
 int i,j;
 if(printf("%d",scanf("%d%d",&i,&j)))
  printf("Yes");
 else
  printf("No");
}

Sol.: It will take input two int through keyboard and will display 2Yes.

10)What is the difference betwee the o/p of the following code snippets?
a)#include<stdio.h>
main()
{
 printf("%10s","expert");
}

b)
#include<stdio.h>
main()
{
 printf("%10.3s","expert");
}

Sol.: a)    expert, b)       exp

11)
The following program is trying to read an integer and a character from keyboard.Does it correctly reads intented input? If not,why so?
main()
{
 int a;
 char b;
 scanf("%d%c",&a,&b);
}

Sol.: No,after entering the first value, we will press any whitespace character(,i.e.,space,enter,tab). b will contain the value of that character.

12)
How will you print % character using printf statement only?

Sol.: #include<stdio.h>
main()
{
 printf("%%");
}

13)
What wiil be the o/p of the following program if the given i/p is: 12345 67
#include<stdio.h>
main()
{
 int a,b;
 scanf("%3d%4d",&a,&b);
 printf("a=%d,b=%d",a,b);
}

Sol.:123,45

14)
#include<stdio.h>
main()
{
 float a=135.23567,b=233.45;
 printf("%3.2f,%.4f",a,b);
}

Sol.: 135.24,233.4500

15)The following is a sample C program to read and print an integer. But it is not working properly.What is(are) the mistake(s)?
#include<stdio.h>
main()
{
 int n;
 printf("Enter a number:\n");
 scanf("%d\n",&n);
 printf("n=%d",n);
}

Sol.: Program will run correctly only it will read the first whitespace character. In order to correct it remove \n from scanf.

16)
#include<stdio.h>
main()
{
 char q='A';
 printf("%d",printf("%c\n",q));
}

Sol. A
     2

17)
#include<stdio.h>
main()
{
 char *p=NULL;
 printf("%s",p);
}

Sol.:(null)

18)
#include<stdio.h>
#include<string.h>
main()
{
 FILE *in,*out;
 int x='a';
 out=fopen("sample","w");
 putc(toupper(x),out);
 fclose(out);
 in=fopen("sample","r");
 printf("%c",getc(in));
 fclose(in);
}

Sol.: A

19)#include<stdio.h>
main()
{
 FILE *in,*out;
 int count=0;
 out=fopen("sample","w");
 fprintf(out,"%s","one");
 fclose(out);
 in=fopen("sample","r");
 while(!feof(in))
 {
  fgetc(in);
  count++;
 }
 fclose(in);
 printf("%d\n",count);
}

Sol.: 4

INPUT & OUTPUT(LEVEL-II)



1)#include<stdio.h>
main()
{
 char c;
 while((c=getchar())!=EOF)
  putchar(c);
};

Sol.: This program will take acharecter and display it. This process will be continued until ctrl+c is pressed.

2)The following program generates 100 unique filenames i.e.,file0 - file99 and stores them in fname array. Fill up the missing logic.
#include<stdio.h>
#include<string.h>
main()
{
 int i;
 char *c;
 char fname[100][10];
 for(i=0;i<100;i++)
 {
  c[0]=i+0;
  c[1]='\0';
  strcpy(fname[i],strcat("file",c));
 }
 for(i=0;i<100;i++)
  printf("\n%s",fname[i]);
}

3)
#include<stdio.h>
main()
{
 int k,max=4;
 char *s="Algorithmica";
 printf("%.*s",max,s);
}

Sol.: Algo

4)
#include<stdio.h>
main()
{
 int p;
 if(printf("%d"+2,scanf("%d",&p)))
  printf("Yes");
 else
  printf("No");
}

Sol.: No (it will first waight for an i/p through keyboard)

5)What is the o/p of following program for the input: 10
                              xy
#include<stdio.h>
main()
{
 int a;
 char b[15];
 scanf("%d",&a);
 gets(b);
 printf("%d,%s",a,b);
}

Sol.:  After entering 10 when we will press enter key gets(b) will feed enter value in b and will display value 10 insted if we will give i/p like 10xy then it will show 10,xy

6)
#include<stdio.h>
main()
{
 int x=0;
 for(;x<20 && printf("%d ",x);x++)
 {
  switch(x)
  {
   case 0: x++,x*=2;
   case 20: x+=2;
   case 70: x+=6;
   default: x+=3;
            break;
  }
 }
}

Sol.: 0 14 18

7)
#include<stdio.h>
main()
{
 char c;
 while(c=getchar()!='a')
  printf("%d ",c);
}

Sol.: Here if we give any input accept a and press enter it will print 1 1 (1 for the character and 1 for enter) when we will give a as input it will get outside the while loop

8)What is the output of the following, if i/p provided is: Life is beautiful
#include<stdio.h>
main()
{
 char dummy[80];
 printf("Enter a string:\n");
 scanf("%[^a]",dummy);
 printf("%s\n",dummy);
}

Sol.: Life is be

9)Predict the o/p of the code with the following inputs:
a)2c3
b)2 5
#include<stdio.h>
main()
{
 int i=0,j=4;
 scanf("%dc%d",&i,&j);
 printf("%d %d",i,j);
}

Sol.: a)2 3
      b)2 4

10)What is the condition so that the following code snippet prints HelloWorld?
#include<stdio.h>
main()
{
 if(_________)
  printf("Hello");
 else
  printf("World");
}

Sol.: !(printf("Hello"))

11)
#include<stdio.h>
main()
{
 printf("%s%s"+2,"Hello","World",2);
}

Sol.: Hello

12)
#include<stdio.h>
#include<malloc.h>
main()
{
 int i;
 int size=3;
 int total=0;
 int numbers[]={001,010,014};
 for(i=0;i<size;i++)
  total+=numbers[i];
 toString(total);
}
int toString(int i)
{
 char* temp=(char*)malloc(sizeof(int)*3);
 sprintf(temp,"%d",i);
 printf("%s",temp);
 return i;
}

Sol.: 21(NOTE:When the no. starts from 0 then it is octal representation)

13)
Which of the following statements about the display function is/are true? Assume that message is'\0' terminated and memory allocated for it.
int display(const char *message)
{
 int count=0;
 count=printf(message);
 return count;
}

Sol.: I)Returns the numbers of characters that were printed successfully.

14)
#include<stdio.h>
main()
{
 FILE *out1,*out2,*in;
 char tmp[20];
 out1=fopen("sample","w");
 out2=fopen("sample","w");
 fprintf(out1,"%s","one");
 fprintf(out2,"%s","two");
 fclose(out1);
 fclose(out2);
 in=fopen("sample","r");
 while(!feof(in))
 {
  fscanf(in,"%s",tmp);
  printf(tmp);
 }
}

Sol.: two(NOTE:The Sequence depend on the order of fclose)

15)
#include<stdio.h>
#include<string.h>
#define MAX 50
main()
{
 FILE *fp1,*fp2;
 int count=0;
 char *this1,*this2;
 fp1=fopen("ip1.txt","r");
 fp2=fopen("ip2.txt","r");
 while((getline1(this1,fp1)!=0) && (getline1(this2,fp2)!=0))
 {
  if(!strcmp(this1,this2))
  {
   count++;
   continue;
  }
  else
   break;
 }
 printf("%d",count);
 fclose(fp1);
 fclose(fp2);
}
int getline1(char *line,FILE *fp)
{
 if(fgets(line,MAX,fp)==NULL)
  return 0;
 else
  return strlen(line);
}

Sol.: Segmentation Fault in getline1

16)
#include<stdio.h>
main()
{
 FILE *out,*in1,*in2;
 char a,b;
 int count=0;
 out=fopen("sample","w");
 fprintf(out,"%s","one");
 fclose(out);
 in1=fopen("sample","r");
 in2=fopen("sample","r");
 while(!feof(in1) && !feof(in2))
 {
  a=fgetc(in1);
  b=fgetc(in2);
  count++;
 }
 fclose(in1);
 fclose(in2);
 printf("%d\n",count);
}

Sol.: 4 


----------------------------------------------------------------------------------------------------------------------------------

Structures & Union(level-I)

1)
#include<stdio.h>
struct book_s
{
 int bno;
 char bname[20];
};
typedef struct book_s book;
main()
{
 book b1={100,"java"},*pb1;
 pb1=&b1;
 printf("%c,%s",pb1->bname[1],pb1->bname+2);
}

Sol.: a,va

2)
#include<stdio.h>
typedef struct student_s
{
 int sno;
 char sname[20];
 char *cname;
}student;

main()
{
 student s={111,"xyz","stanford"},*ps;
 ps=&s;
 printf("%s\n",(*ps).sname+1);
 ps->cname++;
 printf("%s",ps->cname+3);
}

Sol.: yz
      ford

3)
#include<stdio.h>
#include<malloc.h>
struct node_s
{
 int element;
 struct node_s* next;
};

main()
{
 struct node_s *p,*q;
 p=malloc(sizeof(*p));
 q=malloc(sizeof(*p));
 p->element=10;
 p->next=q;
 q->element=-15;
 q->next=NULL;
 printf("%d\n",p->element);
 p=p->next;
 printf("%d",p->element);
}

Sol.: 10
      -15

4)
#include<stdio.h>
#include<malloc.h>
struct test
{
 int a;int b;
};
void f(struct test t)
{
 t.a=10;
 t.b=15;
}
main()
{
 struct test t={4,5};
 f(t);
 printf("%d,%d",t.a,t.b);
}

Sol.: 4
      5

5)
#include<stdio.h>
struct circle
{
 float r;
};
void f(struct circle *t)
{
 t[0].r=12.3;
 t[1].r=13.5;
}
main()
{
 struct circle t[]={4.5,5.0};
 f(t);
 printf("%f,%f",t[0].r,t[1].r);
}

Sol.: 12.30000,13.50000

6)
#include<stdio.h>
struct test
{
 int a; int b;
};
void f(struct test *t)
{
 t->a=10; t->b=15;
}
main()
{
 struct test t={4,5};
 f(&t);
 printf("%d,%d",t.a,t.b);
}

Sol.: 10,15

7)
#include<stdio.h>
union test{short int a;char b;};
main()
{
 union test u;
 u.a=258;
 u.b=10;
 printf("%hd",u.a);
}

Sol.: 266(this is due to 256+10)

8)#include<stdio.h>
union register1 {short int a;char b[2];};
void f(union register1* pu)
{
 printf("%d,%d",pu->b[1],pu->b[0]);
}
main()
{
 union register1 u;
 u.a=300;
 f(&u);
}

Sol.: 1,44 (due to 300=256+44)

9)
#include<stdio.h>
struct example {int a;char b;};
main()
{
 struct example *s;
 s->a=10;
 s->b='A';
 printf("%d,%c",s->a,s->b);
}

Sol.: Segmentation Fault(Run time error)

10)Does the program generates any error? If yes, what changes do you suggest in the program to remove them? If no,what is the o/p?
#include<stdio.h>
struct student
{
 int sno;
 char sname[20];
 struct date d;
};
struct date { int day;int month;int year;};
main()
{
 struct student s={121,"xyz",15,8,1947};
 printf("%d/%d/%d",s.d.day,s.month,s.year);
}

Sol.: yes,the suggest changes are:
#include<stdio.h>
struct date { int day;int month;int year;};
struct student
{
 int sno;
 char sname[20];
 struct date d;
};
main()
{
 struct student s={121,"xyz",15,8,1947};
 printf("%d/%d/%d",s.d.day,s.d.month,s.d.year);
}

11)Which of the following decleration of structure is legal? why?
a)struct node1
{
 int element;
 struct node1 next;
};
b)struct node2
{
 int element;
 struct node2 *next;
};

Sol.: b) (Note:in a) field ‘next’ has incomplete type,hence it will give compile time error)

12)Is the following code decleration legal? If not, what changes do you suggest to make it legal?
typedef struct node
{
 int element;
 listnode *next;
}listnode;

Sol.: no it is not legal. It will give compile time error.The correct way is:
typedef struct node
{
 int element;
 struct node *next;
}listnode;

13)Does the following program generates any error? If Yes, what changes do you suggest to remove remove them?
#include<stdio.h>
main()
{
 typedef struct point_s point;
 point p={4,5};
 struct point_s{int x;int y;};
 printf("%d,%d",p.x,p.y);
}

Sol.: Yes it will generate error. Changes done are:
#include<stdio.h>
main()
{
 struct point_s{int x;int y;};
 typedef struct point_s point;
 point p={4,5};
 printf("%d,%d",p.x,p.y);
}

14)
#include<stdio.h>
main()
{
 struct stud
 {
  char *name;
  int rollno;
 };
 struct subject
 {
  struct stud s;
  char *sub[3];
 };
 struct subject s1={"xyz",123,"C","DS","C++"};
 printf("%s,%s",s1.s.name+1,s1.sub[1]+1);
}

Sol.: yz,S

15)
#include<stdio.h>
typedef union
{
 struct
 {
  char c1,c2;
 }s;
 unsigned int t;
 float f;
}u;
main()
{
 u sample;
 sample.s.c1='A';
 sample.s.c2='B';
 sample.t=5;
 printf("%d,%d,%d",sample.s.c1,sample.s.c2,sample.t);
}

Sol.: 5,0,5

16)
#include<stdio.h>
struct student_s
{
 char *name;
 int rollno;
 int age;
};
main()
{
 struct student_s s={"xyz"};
 printf("%d,%d",s.rollno,s.age);
}

Sol.: 0,0(NOTE: if a structure is partially initialised then all of its uninitialised value becomes 0)

17)
#include<stdio.h>
struct tag
{
 int i;
 union
 {
  int a;
  int b;
 }c;
}a;
main()
{
 a.c.a=10;
 printf("%d",a.c.b);
}

Sol.: 10

18)
#include<stdio.h>
typedef struct s{ int a[10];}s;
typedef struct t{ int *a;}t;
main()
{
 s s1,s2;
 t t1;
 s1.a[0]=0;
 t1.a=s1.a;
 s1.a[0]++;
 s2=s1;
 s1.a[0]++;
 printf("%d,%d,%d",s1.a[0],s2.a[0],t1.a[0]);
}

Sol.: 2,1,2

19)
#include<stdio.h>
struct sample
{
 int x;
 int y[3];
};
main()
{
 struct sample s={10,12,-8,11};
 printf("%d",s.x+s.y[1]);
}

Sol.: 2(i.e.,10-8)

20)
#include<stdio.h>
struct test
{
 int a;
 int b;
};
struct test f(void)
{
 struct test t;
 t.a=20;
 t.b=25;
 return t;
}
main()
{
 struct test t={4,5};
 t=f();
 printf("%d,%d",t.a,t.b);
}

Sol.: 20,25




Structures & Union(level-II)

1)
#include<stdio.h>
struct test
{
 int a;
 int b[3];
 int *c;
};
main()
{
 struct test p={0,1,2,3},*q;
 q=&p;
 q->c=&q->b[2];
 printf("%d",++*q->c);
}

Sol.: 4

2)
#include<stdio.h>
struct book
{
 int n;
 char* bname;
};
main()
{
 struct book b={100,"java"};
 struct book *p=&b;
 printf("%c\n",*p->bname++);
 printf("%c\n",(*p->bname)++);
}

Sol.: j
      Segmentation fault(Run Time error as ++ operator can't be applied to constants)

3)
#include<stdio.h>
struct s1
{
 int a;
 char b;
 float c;
 char d;
};
main()
{
 printf("%d",sizeof(struct s1));
}

Sol.: 16(i.e.,4*4)

4) Suggest an ordering for members of structure s such that the wastage of storage space will be minimal.

Sol.: Ordering should be done in the increasing order of the storage space of the particular element

5)
#include<stdio.h>

main()
{
 struct s2
 {
  int a;
  char b;
  double c;
 };
 struct s2 *ps1;
 printf("%d",sizeof(*ps1));
}

Sol.: 16(4+4+8)

6)
#include<stdio.h>
void f(char **s)
{
 *s="java";
}
main()
{
 struct s
 {
  int s;
  char *t;
 }s1={200,"c++"},*ps1=&s1;
 f(&s1.t);
 printf("%c",ps1->t[1]);
}

Sol.: a

7)
#include<stdio.h>
struct node
{
 int value;
 struct node *next;
}
main()
{
 struct node *k,*m;
 k=malloc(sizeof(struct node));
 m=malloc(sizeof(int));
 printf("%d,%d",sizeof(k),sizeof(m));
}

Sol.: 8,8

8)
#include<stdio.h>
struct point
{
 int x;
 int *y;
};
main()
{
 int a=-3,b=-4;
 struct point p[2];
 struct point *ps=p;
 p[0].x=3;p[0].y=&a;
 p[1].x=4,p[1].y=&b;
 printf("%d\n",ps++->x);
 printf("%d",++(*ps->y));
}

Sol.: 3  (Note:this 3 corresponds to p[0].x)
      -3 (Note:this -3 corresponds to b+1  )

9)
#include<stdio.h>
struct test
{
 int x;
 char *y;
}
s[2][2]={1,"sun",2,"jan",3,"sep",4,"moon"};
main()
{
 char *t;
 __________
 printf("%s",t);
}
Fill up the blank in the above program so that it prints only oon as output.

Sol.:  t=s[1][1].y+1;

10) Write a macro to compute the size of any type t without using sizeof operator

Sol.:
#include<stdio.h>
#define object_size(t) ((char *)(&(t) + 1) - (char *)&(t))
main()
{
 int i;
 struct s { int x; char y;}s1;
 printf("%d,%d",object_size(i),object_size(s1));
}


12)Write a macro to compute the offset of any member m of structure type t.

Sol.: #define offsetof(m,t) ((size_t)((char *)&((t *)0)->m-(char *)0))


13)
#include<stdio.h>
void f(void) {printf("f");}
void g(void) {printf("g");}
struct test
{
 int a;
 int b;
 void (*t[2])(void);
};
main()
{
 int i;
 struct test q,*p=&q;
 ___________
 ___________
 for(i=2;i>=1;i--)
  ___________
}
Write C statements in the above program to initialize the array of function pointers in a structure and to call the functions through these function pointers such that the output will be fg.

Sol.:
#include<stdio.h>
void f(void) {printf("f");}
void g(void) {printf("g");}
struct test
{
 int a;
 int b;
 void (*t[2])(void);
};
main()
{
 int i;
 struct test q,*p=&q;
 q.t[1]=f;
 q.t[0]=g;
 for(i=2;i>=1;i--)
  p->t[i-1]();
}


14)In the following program, union u consists of different types. It is trying an approach through which it knows the type of member stored in that union at any time. Do you think it is correct approach? If not, explain the flaw in that approach and suggest a correct way of doing it.
union u
{
 int type;//0 if char is stored, 1 if integer, 2 if float
 char c;
 int a;
 float b;
};

---------------------------------------------------------------------------------------------------------------------------------

Bitwise trick(LEVEL-I)

1) Write an equivalent expression for x%32 using only bitwise operators?

Sol.:
#include<stdio.h>
main()
{
 int i;
 scanf("%d",&i);
 i=i&31;
 printf("%d",i);
}

2)What does the following function do?
unsigned findwhat(unsigned int x,int p,int n)
{
 return((x>>(p+1-n))&~(~0<<n);
}

Sol.: this function will divide x by 2^(p+1-n) and then do % of it with 2^(n+1).

3)
#include<stdio.h>
void fun(int a)
{
 printf("A");
 if(!(a&(a-1))) return;
 fun(a & (a-1));
}
main()
{
 fun(0xA35F);
}

Sol.:AAAAAAAAAA(At every call of fun the least most significant 1 from a will be deleted).

4)
Write a C function that rounds the given integer to next higher multiple of 8 and returns the value.

Sol.:
int HigherMult*(int n)
{
 return (n+7)& -8;
}

5) Write a C function to check whether a given unsigned integer is having zero bytes(,i.e., the value of any byte is zero) or not? Your function should return 1 true, or 0 otherwise.

Sol.:
int HasZeroByte(int n)
{
 while(n)
 {
  if((n&1)==0)
   return(0);
  n>>=1;
 }
 return(1);
}


BITWISE TRICKS(LEVEL-II)

1) Write a C function to set 5th and 7th bits of given integer n and returns the resultant n value? Your function should not assume specific size for integer.

Sol.:
int setBits(int n)
{
 n|=(1<<5);
 n|=(1<<7);
 return n;
}

2)Which one of the following is true about the following function?
unsigned test2(int x)
{
 x|=(x>>1);
 x|=(x>>2);
 x|=(x>>4);
 x|=(x>>8);
 x|=(x>>16);
 return x+1;
}

Sol.: b)returns the lest power of 2 greater than x.

3)Write a C function that returns the position of the rightmost one in a given unsigned integer.

Sol.:
#include<stdio.h>
#include<math.h>
unsigned RightmostOne(unsigned n)
{
 return log2(n&-n)+1;
}