C Programming Puzzles 2


26)
#include <stdio.h>
main()
{
int i ;
i = 1;
i= i+2*i++;
printf("i is now %d",i);
}

27)
#include <stdio.h>
#define MAX(x,y) (x) >(y)?(x):(y)
main()

{
int i=10,j=5,k=0;
k= MAX(i++,++j);
printf("%d..%d..%d",i,j,k);
}

28)
#include <stdio.h>
main()
{
const int i = 100;
int *p = &i;
*p = 200;
printf("%d\n",i);

}

29)
#include <stdio.h>
static int count;
void f(int n)
{
int i;
for(i =1;i<=n;i++)
f(n-i);
count++;
}
main()
{
extern int count;
f(5);
printf("%d",count);
}

30)
#include <stdio.h>
void test(int , int *);
main()
{
int * iptr, j, k = 2;
iptr = &j;
j = k;
printf( "%d %d ", k, j);
test(j, iptr);
printf("%d %d\n", k, j);
}
void test(int l, int *p)
{
l++;
(*p)++;
}

31)
#include <stdio.h>
main()
{
char a= 'A';
if( (a=='Z')||( (a='L')&&( a=='A')))
a=a;
printf("%c",a);
printf(" Nothing ");
}

32)
#include <stdio.h>
int myfunc(char *str)
{
char *ptr =str;
while(*ptr++);
return ptr-str-1;
}
main()
{
printf("%d", myfunc("DESIS"));
}

33)
#include <stdio.h>
main(int sizeofargv, char *argv[])
{
while(sizeofargv)
printf("%s ",argv[--sizeofargv]);
}

34)
#include <stdio.h>
main()
{
int x,y=1,z;
if(x=z=y); x = 3;
printf("%d %d %d\n",x,y,z);
while (y<4) x+=++y;
printf("%d %d\n",x,y);
}

35)
#include <stdio.h>
main()
{
union {
long l_e;
float f_e;
} u;

long l_v;
float f_v;
l_v = u.l_e = 10;
printf("%f ", (float)l_v);
printf("%f ", u.f_e);
f_v = u.f_e = 3.555;
printf("%d ", (long)f_v);
printf("%d ", u.l_e);
}

36)
#include <stdio.h>
main()
{
char a[5] = "abcd";
int b = 3;

printf("%c\n",a[b]);
printf("%c\n",((char *) b)[(int) a]);
}

37)
#include <stdio.h>
#define PRINTIFLESS(x,y) if((x) < (y)) printf("First is smaller");else
main()
{
int i = 2, k =1;
if(i>0 && k>0) PRINTIFLESS(i,k);
else printf("Numbers not greater than 0\n");

}

38)
#include <stdio.h>
#include<malloc.h>
main()
{
int *iptr,*dptr, i;
dptr = malloc(sizeof(i));
iptr =&i ;
*iptr = 10;
free(iptr);
*dptr = 20;
/*dptr = iptr;*/
free(dptr);
printf("%d,%d,%d",*dptr,*iptr,i);
}

39)
#include <stdio.h>
main()
{
char line[80];
gets(line);
puts(line);

}

40)
#include <stdio.h>
main()
{
char c1;
int i=0;
c1='a';
while(c1>='a' && c1 <='z')
{
c1++;
i++;
}
printf("%d",i);
}

41)
#include <stdio.h>
main()
{
char ch = 'A';
while(ch <='F'){
switch(ch){
case 'A':case 'B':case 'C': case 'D': ch++; continue;
case 'E': case 'F': ch++;
}
putchar(ch);
}
}

42)
#include <stdio.h>

f(int x,int *y)
{
x=*(y)+=2;
}

main()
{
static int a[5] = {2,4,6,8,10};
int i,b=5;
for(i=0; i< 5;i++){
f(a[i],&b);
printf("%d %d\n",a[i],b);
}
}

43)
#include <stdio.h>
main()
{
FILE *fp1,*fp2;
fp1 = fopen("one","w");
fp2 = fopen("one","w");
fputc('A',fp1);
fputc('B',fp2);
fclose(fp1);
fclose(fp2);
}

44)
#include <stdio.h>
main()
{
int a = 0xff;
if(a<<4>>12)
printf("Right");
else
printf("Wrong");
}

45)
#include <stdio.h>
main()
{
enum _tag{ left=10, right, front=100, back};
printf("left is %d, right is %d, front is %d, back is %d",left,right,front,back);
}

46)
#include <stdio.h>
main()
{
char *arr = "This is to test";
printf("\n%c %c ",*(arr), *(arr++));

}

47)
#include <stdio.h>
main()
{
int I =-3, j=2, k = 0,m;
m = ++I && ++j && ++k;
printf("\n%d %d %d %d", I, j, k, m);
}

48)
#include <stdio.h>
#define MAX 20

main()
{
FILE *fp1, *fp2;
char *this1, *this2;
fp1 = fopen("ip1.dat","r");
if(fp1==NULL)printf("file open error\n");

fp2 = fopen("ip2.dat","r");
if(fp2==NULL)printf("file open error\n");

if((getline(this1,fp1)!=0) && (getline(this2,fp2)!=0)){
if(strcmp(this1,this2))
continue;
else { printf("lines do not match\n"); break;}
}
}
int getline(char *line, FILE *fp)
{
if(fgets(line,MAX, fp) == NULL)
return 0;
else
return strlen(line);
}

49)
#include <stdio.h>
main()
{
FILE *fp;
fp = fopen("testbuf.txt", "wt");
fwrite("1. This is fwrite\n",1, 18, fp);
write(fileno(fp), "2.This is write\n", 17);
fclose(fp);
}

50)
#include <stdio.h>
#define PR(a) printf("a = %d\t",(int) (a));
#define PRINT(a) PR(a); putchar('\n');
#define FUDGE(k) k + 3.14

main()
{
int x = 2;
PRINT( x * FUDGE(2));
}

3 comments:

  1. 26)
    i is now 4

    27)
    12..6..11
    28)
    200

    29)
    32

    30)
    2 2 2 3

    31)
    L Nothing

    32)
    5

    33)
    ./a.out

    34)
    3 1 1
    12 4

    35)
    10.000000 0.000000 3 1080263967

    36)
    d
    d

    37)

    38)
    Segmentation fault

    39)
    -->will output the given input

    40)
    26

    41)
    FG

    42)
    2 7
    4 9
    6 11
    8 13
    10 15

    43)
    The letter A goes into the file initially and then when it is reopened it is replaced with B

    44)
    Wrong

    45)
    left is 10, right is 11, front is 100, back is 101

    46)
    h T

    47)
    -2 3 1 1

    48)ERRORS:
    In function main :6: error: continue statement not within a loop
    17: error: break statement not within loop or switch
    In function getline:25: warning: incompatible implicit declaration of built-in function ârlenâOA

    49)The file testbuf.txt finally contains the text:
    2.This is write
    ^@1. This is fwrite

    50)
    a = 7

    ReplyDelete
  2. can you please explain the answers,it will be a great help. Thanks! :)

    ReplyDelete
  3. excellent questions thanks a lot great work..........

    ReplyDelete