-
Notifications
You must be signed in to change notification settings - Fork 1
C programming puzzles/exercises
starnux/c_exercises
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
C Programming Exercises ======================= 1. The expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so. #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int array[] = {23,34,12,17,204,99,16}; int main() { int d; for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); return 0; } 2. I thought the following program was a perfect C program. But on compiling, I found a silly mistake. Can you find it out (without compiling the program :-) ? #include<stdio.h> void OS_Solaris_print() { printf("Solaris - Sun Microsystems\n"); } void OS_Windows_print() { printf("Windows - Microsoft\n"); } void OS_HP-UX_print() { printf("HP-UX - Hewlett Packard\n"); } int main() { int num; printf("Enter the number (1-3):\n"); scanf("%d",&num); switch(num) { case 1: OS_Solaris_print(); break; case 2: OS_Windows_print(); break; case 3: OS_HP-UX_print(); break; default: printf("Hmm! only 1-3 :-)\n"); break; } return 0; } 3. What's the expected output for the following program and why? enum {false,true}; int main() { int i=1; do { printf("%d\n",i); i++; if(i < 15) continue; }while(false); return 0; } 4. The following program doesn't "seem" to print "hello-out". (Try executing it) #include <stdio.h> #include <unistd.h> int main() { while(1) { fprintf(stdout,"hello-out"); fprintf(stderr,"hello-err"); sleep(1); } return 0; } What could be the reason? 5. Just by looking at the program one "might" expect the output to be, the same for both the printf statements. But on running the program you get it as: bash$ ./a.out 12 f(1,2) bash$ Why is it so? #include <stdio.h> #define f(a,b) a##b #define g(a) #a #define h(a) g(a) int main() { printf("%s\n",h(f(1,2))); printf("%s\n",g(f(1,2))); return 0; } 6. If you expect the output of the above program to be NONE, I would request you to check it out!! #include<stdio.h> int main() { int a=10; switch(a) { case '1': printf("ONE\n"); break; case '2': printf("TWO\n"); break; defa1ut: printf("NONE\n"); } return 0; } 7. The following C program segfaults of IA-64, but works fine on IA-32. Why does it happen so? int main() { int* p; p = (int*)malloc(sizeof(int)); *p = 10; return 0; } 8. Here is a small piece of program(again just 14 lines of program) which counts the number of bits set in a number. Input Output 0 0(0000000) 5 2(0000101) 7 3(0000111) Find out the logic used in the above program. int CountBits (unsigned int x ) { static unsigned int mask[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF } ; int i ; int shift ; /* Number of positions to shift to right*/ for ( i =0, shift =1; i < 5; i ++, shift *= 2) x = (x & mask[i ])+ ( ( x >> shift) & mask[i]); return x; } 9. What do you think would be the output of the following program and why? (If you are about to say "f is 1.0", I would say check it out again) #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("f is 1.0 \n"); else printf("f is NOT 1.0\n"); return 0; } 10. I thought the following C program is perfectly valid (after reading about the comma operator in C). But there is a mistake in the following program, can you identify it? #include <stdio.h> int main() { int a = 1,2; printf("a : %d\n",a); return 0; } 11. What would be the output of the following C program? (Is it a valid C program?) #include <stdio.h> int main() { int i=43; printf("%d\n",printf("%d",printf("%d",i))); return 0; } 12. Is the above valid C code? If so, what is it trying to acheive and why would anyone do something like the above? void duff(register char *to, register char *from, register int count) { register int n=(count+7)/8; switch(count%8){ case 0: do{ *to++ = *from++; case 7: *to++ = *from++; case 6: *to++ = *from++; case 5: *to++ = *from++; case 4: *to++ = *from++; case 3: *to++ = *from++; case 2: *to++ = *from++; case 1: *to++ = *from++; }while( --n >0); } } 13. Here is yet another implementation of CountBits. Verify whether it is correct (how do you that???). If so, find out the logic used. int CountBits(unsigned int x) { int count=0; while(x) { count++; x = x&(x-1); } return count; } 14. Are the following two function prototypes same? int foobar(void); int foobar();
About
C programming puzzles/exercises
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published