type your search

Wednesday, January 18, 2012

C Programming Puzzles 6

126)
#include<stdio.h>
main()
{
        char c='6';
        int d=6;
        printf("%d %d %d",d,d+=c>'0'&&c<='7',c++);
}

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

128)
#include<stdio.h>
main()
{
        int i=98765;
        printf("%d\n",printf("%d",printf("%d",i)));
}

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

130)
#include<stdio.h>
main()
{
        int *i=0;
        printf(" %p\n",i);
}

131)
#include<stdio.h>
#include<stdlib.h>
main()
{
        int *ptr=(int*)malloc(sizeof(int));
        *ptr=4;
        printf("%d",(*ptr)+++*ptr++);
}

132)
#include<stdio.h>
unsigned getbits(unsigned a,int i,int  j)
{
        return(a>>(i+1-j)) & ~(~0<<j);
}
main()
{
        unsigned num=128;
        printf("%d\n",getbits(num,7,5));
}

133)
#include<stdio.h>
#define sq(x) x*x
main()
{
        int a=5;
        printf("%d\n",sq(a+5));
}

134)
#include<stdio.h>
#define concatinate(a,b) a##b
#define same1(a)  #a
#define same2(a)  same1(a)
main()
{
        printf("%s\n",same2(concatinate(1,2)));
        printf("%s\n",same1(concatinate(1,2)));
}

135)
#include<stdio.h>
main()
{
        int a=1,b=3,c,d;
        c=(a,b);
        d=a,b,c;
        printf("%d %d",c,d);
}

136)
#include<stdio.h>
#include<stdlib.h>
main()
{
        int*ptr=(int*)malloc(sizeof(int));
        *ptr=4;
        printf("%d",(*ptr)+++*ptr++);
}

137)
#include<stdio.h>
main()
{
        int x;
        x=20;
        printf("x:%d\n",x);
        printf("sizeof(x++) is: %d\n",sizeof(x++));
        printf("x:%d\n",x);
}

138)
#include <stdio.h>
#define f(x,y) x##y
#define g(x)   #x
#define h(x) g(y)

int main()
{
        printf("%s\n",h(f(2,3)));
        printf("%s\n",g(f(2,3)));
        return 0;
}

139)
#include <stdio.h>
int main()
{
        int i;
        i = 10,20,30;
        printf("i:%d\n",i);
}

140)
#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int Shiftfn(int a)
{
        int t;
        t = a<<2 + a;
        return t;
}

int main()
{
        int i = 1, j = 2,k = 3;
        PrintInt(Shiftfn(i));
        PrintInt(Shiftfn(j));
        PrintInt(Shiftfn(k));
}

141)
#include<stdio.h>
enum {false,true};

int main()
{
        int i=1;
        do
        {
                printf("%d\n",i);
                i++;
                if(i < 15)
                        continue;
        }while(false);
}

142)
#include <stdio.h>
int main()
{
        float a = 12.5;
        printf("%d\n",a);
        printf("%d\n", *(int *)&a);
        return 0;
}

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

144)
#include<stdio.h>
#include<string.h>
int s(char*A[20],char*B[20])
{
        char *a,*b;
        a=A,b=B;
        while(*a++!=*b++);
        *a=*b='\0';
        return strlen(A);
}

int main()
{
        char A[20]="somestring",B[20]="debugthecbug";
        printf("%d %s %s\n",s(&A,&B),A,B);
        return 0;
}

145)
#include<stdio.h>
void insert(char a[],int n)
{
        int i,j;
        for(i=j=0;a[i]!='\0';i++)
                if(a[i]!=n)
                        a[j++]=a[i];
        a[j]='\0';
}
main()
{
        char a[]="helloworld";
        insert(a,'l');
        printf("%s",a);
}

146)
#include<stdio.h>
#include<stdlib.h>
void weird(int*a)
{
        a=(int*)malloc(sizeof(int));
}
main()
{
        int*a;
        weird(a);
        *a=6;
        printf("%d\n",*a);
}

147)
#include <stdio.h>
main()
{
        int a=4,b=10;
        printf("%d %d %d %d\n",a,a^=b=b^=a=a^=b,b,printf("%d %d %d\n",
b,a,a^=b=b^=a=a^=b));
}

148)
#include<stdio.h>
int fun1()
{
        static int c=20;
        return --c;
}

int fun2()
{
        static int c=1;
        return fun1()+c--;
}

int main()
{
        int i=0;
        while(i<fun2())
                printf("%d ",i++);
        return 0;
}

149)
#include<stdio.h>
#include<string.h>
main()
{
        char a[]="aaa";
        char *b="bbb";
        strcpy(a,"cc");
        printf("%s",a);
        strcpy(b,"dd");
        printf("%s",b);
}

150)
#include<stdio.h>
int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))

int main()
{
        printf("%d",SIZE);
        if(-1<=SIZE) printf("1");
        else printf("2");
        return 0;
}

No comments:

Post a Comment