C Programming Puzzles 3


51)
#include <stdio.h>
main()
{
int i = 3,j;
j = add(++i);
printf("i = %d *p= %d\n", i, j);
}

add(ii)
int ii;
{
ii++;
printf("ii = %d\n", ii);
}

52)
#include <stdio.h>
#define DEBUG(args) (printf("DEBUG: "), printf args)

main()
{
int n = 0,i = 0 ;
printf("%d\n", n);
if(n != 0) DEBUG(("n is %d\n", n));
DEBUG(("%d",i));

}

53)
#include <stdio.h>
main()
{
printf("hello");
fork();
}

54)
#include <stdio.h>
#include<malloc.h>
#include<string.h>
main()
{
char *s2, *s1 ;
// s1 = malloc(sizeof(char) * 1000);
s1 = "Hello, ";
// s2 = malloc(sizeof(char) * 10);
s2 = "world!";
strcat(s1, s2);
printf("%s", s2);
}

55)
#include <stdio.h>
char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";
main()
{
printf(s,34,s,34);
}

56)

#include <stdio.h>
#include<string.h>
main()
{
char *s1 = "alpha", *s2 = "alpha";
if(!strcmp(s1,s2)) printf("yes\n");
}

57)
#include <stdio.h>
#define DEBUG(args) (printf("DEBUG: "), printf args)

main()
{
int n = 10;
if(n != 0) DEBUG(("n is %d\n", n));

}

58)
#include <stdio.h>
main()
{
int i;
struct
{
int left,y;
}a;
printf("%5d\n",a[i].left);
}

59)
#include <stdio.h>
main()
{
char c1,c2,c3;
c1 = getc(stdin);
putc(c1,stdout);
// c2 = getche();
// putc(c2,stdout);
c3 = getchar();
putc(c3,stdout);
}

60)
#include <stdio.h>
#include<malloc.h>
struct test{
int f;
};

struct test* f(struct test * (*fPtr)() )
{
struct test *ptr = (struct test*) malloc(sizeof(struct test));
return ptr;
}
main()
{
f(f)->f;
}

61)
#include <stdio.h>
void print_in_reverse( char *str )
{
if( *str == '\0' )
return;

print_in_reverse(str+1);

printf( "%c" , *str );
}
main()
{
print_in_reverse( "char *str" );
}

62)
#include <stdio.h>
#include<math.h>
//#define sqrt(x) (( x < 0) ? sqrt(-x) : sqrt(x))
main()
{
int y;
y = sqrt(-9);
printf("%d",y);
}

63)
#include <stdio.h>
#define MAXI 100
main(){
int done,i,x=6;
done=i=0;
for(i = 0; (i< MAXI) && (x/=2)>1; i++)
done++;
printf("%d %d\n",i,done);
}

64)
#include <stdio.h>
main()
{

char as[] = "\\0\0";

int i = 0;
do{
switch( as[i++]){
case '\\' : printf("A");
break;
case 0 : printf("B");
break;
default : printf("C");
break;
}
}
while(i<3);
}

65)
#include <stdio.h>
#define MAXI 100
main(){
int done,i,x=6;
done=i=0;
while (i < MAXI && !done){
if ((x/=2)>1){ i++; continue;}
done++;
}
printf("%d %d\n",i,done);
}

66)
#include <stdio.h>
main()
{
struct emp
{ char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf("\n%d %f",e.age,e.sal);
}

67)
#include <stdio.h>
main()
{
char str[] = "Taj is 2 miles away";
int i;
for(i=0;i<19;++i)
if(isalpha(str[i]))printf("%c",toupper(str[i]));
}

68)
#include <stdio.h>
main()
{
int c;

while((c=getchar()) != 0){
printf("%c",c);
}
}

69)
#include <stdio.h>
f( )
{
printf("I am f()");
}
extern f1();
main()
{
int i=10;
f1(i);
}

f1(int i )
{
printf("the i value is %d",i);
f();
}

70)
#include <stdio.h>
#define abs(x) x>0?x:-x
#define mabs(x) (((x)>=0)?(x):-(x))
int kabs(int);
main()
{
printf("\n%d %d",abs(10)+1,abs(-10)+1);
printf("\n%d %d",mabs(10)+1,mabs(-10)+1);
printf("\n%d %d\n",kabs(10)+1,kabs(-10)+1);
}
int kabs(int n)
{
return(n>0? n: -n);

}

71)

#include <stdio.h>
unsigned char
f(unsigned n)
{
static const unsigned char table[64] = {
0, 0, 0, 9, 0, 0, 10, 1, 0, 0, 0, 0, 0, 11, 2, 21, 7,
0,0, 0, 0, 0, 0,15, 0, 0, 12, 0, 17, 3, 22, 27,32, 8,
0, 0,0, 0, 0, 20, 6, 0, 0, 14,0, 0, 16, 26,31, 0, 0,
19, 5, 13,0, 25, 30, 18, 4, 24, 29, 23, 28, 0
};
return table[((n & -n) * 0x1d0d73df) >> 26];
}
main()
{
printf("%c",f(8));
}

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

73)
#include <stdio.h>
struct _tag
{
int i;
union
{
int a;
int b;
}c;
} a;

main()
{
a.c.a=10;
printf("test %d\n",a.c.b);
}

74)
#include <stdio.h>
main()
{
int a=10,b;
b=a>=5?100:200;
printf("%d\n",b);
}

75)
#include <stdio.h>
main()
{
int a;

a = (1,45,012);

printf("%d", a);
}

1 comment:

  1. 51)
    ii = 5
    i = 4 *p= 7

    52)
    0
    DEBUG: 0

    53)
    hellohello

    54)
    Segmentation fault

    55)
    char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}

    56)
    yes

    57)
    DEBUG: n is 10

    58)
    error: subscripted value is neither array nor pointer

    59)
    getchar
    This is a standard function that gets a character from the stdin.

    getch
    This is a nonstandard function that gets a character from keyboard, does not echo to screen.

    getche
    This is a nonstandard function that gets a character from the keyboard, echoes to screen.

    Use getchar if you want it to work on all compilers. Use getch or getche on a system that supports it when you want keyboard input without pressing [Enter].

    And note that the return value of all three is int! You need this to properly check for EOF.

    60)
    "Successfully compiles without any error"
    61)
    rts* rahc

    62)
    (.text+0x32): undefined reference to `sqrt'
    collect2: ld returned 1 exit status

    63)
    1 1

    64)
    ACB

    65)
    1 1

    66)
    0 0.000000

    67)
    TAJISMILESAWAY

    68)

    -->>Output::what even is taken as input untill we terminate it

    69)
    the i value is 10I am f()

    70)

    10 11
    11 11
    11 11

    71)
    "Nothing is printed"

    72)
    length is 5

    73)
    test 10

    74)
    100

    75)
    10

    ReplyDelete