Saturday, May 31, 2008

Unknown C more and more and even more

Write a small C program to determine whether a machine's type is little-endian or big-endian. The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work.

#include <stdio.h>
int main()
{
int i;
int n = 20;
for( i = 0; i < n; i-- )
printf("-");
return 0;
}

Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three. What's the mistake in the following code?

#include <stdio.h>
int main()
{
int* ptr1,ptr2;
ptr1 = (int*)malloc(sizeof(int));
ptr2 = ptr1;
*ptr2 = 10;
return 0;
}

What is the output of the following program?

#include <stdio.h>
int main()
{
int cnt = 5, a;

do {
a /= cnt;
} while (cnt --);

printf ("%d\n", a);
return 0;
}

What is the output of the following program?

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

What is the bug in the following program?

#include <stdlib.h>
#include <stdio.h>
#define SIZE 15
int main()
{
int *a, i;

a = (int*) malloc(SIZE*sizeof(int));

for (i=0; i<SIZE; i++)
*(a + i) = i * i;
for (i=0; i<SIZE; i++)
printf("%d\n", *a++);
free(a);
return 0;
}

Is the following a valid C program? If so, what is the output of it?

#include <stdio.h>
int main()
{
int a=3, b = 5;

printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
return 0;
}

Write a C program which prints Hello World! without using a semicolon!!! What is the output of the following, if the input provided is:
Life is beautiful

#include <stdio.h>
int main()
{
char dummy[80];
printf("Enter a string:\n");
scanf("%[^a]",dummy);
printf("%s\n",dummy);
return 0;
}

Write a C program to find the smallest of three integers, without using any of the comparision operators.

No comments: