Follow Me on Twitter

Saturday, October 31, 2009

Solutions to C Puzzles 7

#include
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;
}
If you expect the output of the above program to be NONE, I would request you to check it out!!

Solution:

Nothing gets printed. Bcs check the spelling of "defa1ut".
"The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements.Default is optional" Since none of the value is matched against case and no case has value 10 and compiler doesn't recognize "default" keyword above, nothing gets printed.

Explain the output of the following C program (No, the output is not 20).
#include
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;
}

Solution:
b is 9873931(some random junk value) since b is not initialized. the statement
int b=20; doesn't get executed bcs inside switch the execution starts from
case/default(if no case) block.

What would be the output of the following C program?
(Is it a valid C program?)
#include 
int main()
{
int i=43;
printf("%d\n",printf("%d",printf("%d",i)));
return 0;

Solution:
4321

printf returns the number of digits/characters it printed.So this
printf("%d",i) gets executed first and prints 43 and the function printf returns 2
and similarly other 2 printf's print 2 1


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 

int main()
{
int a = 1,2;
printf("a : %d\n",a);
return 0;
}

Solution:

error in the line int a = 1,2;

Here the declaration and definition is happening at the same time and hence compiler
associates/initializes a to 1 but for 2 it considers as variable and since it
is a not a valid variable(variable names cannot start with digits) it throws
out an error.




Friday, October 30, 2009

Solutions to C Puzzles 6

Given a string s1 and a string s2, write  a snippet to say whether s2 is a
rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true,
given s1 = ABCD, and s2 = ACBD , return false)

#include
#include

int main ()
{
char *pStr1, *pStr2,*ptempString,*ptr;

pStr1 = "ABCD";
pStr2 = "DABC";
printf("Str1 = \"%s\"\n", pStr1);
printf("Str2 = \"%s\"\n\n", pStr2);

if ((ptempString = malloc(2* strlen(pStr2)+2)) == NULL) {
puts("Memory allocation error!");
exit(0);
}
strcpy(ptempString,pStr2);
strcat( ptempString, pStr2);


if((ptr=strstr(ptempString,pStr1)))
{
puts("true");
}
else
{
puts("false");
}


}

Solutions to C Puzzles 5

What's the  "condition" so that the following code
snippet prints both HelloWorld !

if "condition"
printf ("Hello");
else
printf("World");

Solution:


#include
int main()
{
int x=0;
if (5<(printf ("Hello")))
{
printf ("Hello");
else
printf("World");
}
}

Solutions to C Puzzles 4

What is the potential problem with the following C program?
  #include 
int main()
{
char str[80];
printf("Enter the string:");
scanf("%s",str);
printf("You entered:%s\n",str);

return 0;
}

Solution:

Buffer overflow problem. Since C doesn't do bound checking, anyone can fill more
than 80 characters. Hackers might use this to overwrite the return address and
cause malicious activities.

What is the output of the following program?
#include
int main()
{
int i;
i = 1,2,3;
printf("i:%d\n",i);
return 0;
}

Solution:
i: 1


What is the output of the following program?
#include
int main()
{
int cnt = 5, a;

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

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


Solution:

Division by 0 exception.


In the while loop, first the condition is checked and then the value is decremented.
i.e first the existing value of cnt is used to check the truthfulness of while loop
and then the value of cnt is decremented before the execution enters the body of
the loop. Hence when cnt is 1, while return true but the value of cnt at a/=cnt
would be 0 and hence causes run time error.

What is the output of the following program?
#include
int main()
{
int i = 6;
if( ((++i < 7) && ( i++/6)) || (++i <= 9))
;
printf("%d\n",i);
return 0;
}

Solution:
8

Only ((++i < 7) && ( i++/6)) this gets executed. here first the value is tested and
then incremented.
(++i < 7) this is true bcs 6< style="color:#008b8b;">i++/6) is true
bcs 7/6 is 1 and now i=8. Since if includes ||(or) if once condition is true,
the remaining condition won't be checked.

What is the output of the following, if the input provided is:

Life is beautiful
#include
int main()
{
char dummy[80];
printf("Enter a string:\n");
scanf("%[^a]",dummy);
printf("%s\n",dummy);
return 0;
}

Solution:
Life is be


in the above scanf statement, it accepts the input from the stdin(standard input)
till it encounters character "a"( small a)

Monday, October 26, 2009

Solutions to C Puzzles 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;
}

Solution:

1

It prints 1 and quits.
Reason is continue statement brings/takes the execution to the beginning of the loop.
do while is nothing but while loop apart from the first iteration in which the
condition is not checked. Hence when continue is executed, execution to while
statement which returns false and hence quits the while loop.

Sunday, October 25, 2009

Solutions to C Puzzles 2

2)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

#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;
}
Find out what's going wrong.

Solution:
Sizeof Operator's return type is unsigned. When signed variable is compared with unsigned variable/value, the signed variable is promoted to unsigned.

So in the above case d=-1 and which be be promoted to unsigned value which become huge compared to (TOTAL_ELEMENTS-2) hence the condition will result in false and the loop won't be executed.

SO nothing gets printed.

Solutions to C Puzzles

I will be posting solution to the questions posted on http://www.gowrikumar.com/ site.

1)
What is the output of the following program?
#include 
int main()
{
int i;
i = 10;
printf("i : %d\n",i);
printf("sizeof(i++) is: %d\n",sizeof(i++));
printf("i : %d\n",i);
return 0;
}


Solution:

10
4
10

Reason the value of i at the second printf is still 10 is because
operation performed inside the sizeof operator doesn't change the
value of i.

The 'sizeof' operator is used to determine the amount of space any
data-element/datatype occupies in memory