Thursday, February 7, 2008

PUZZLES ON 'C' PROGRAMMING:-

1. Predict the output.
int main() {
int i=-1,j=-1,k=0,l=2,m;
m=++i&&j++&&++k||l++;
printf("%d %d %d %d
%d\n",i,j,k,l,m);
}

2. Output ?
int i = 9;
switch ( i ) {
default : printf ( "No");
case 3 : printf (
"Three" );
case 6 : printf ( "Six"
);
case 9 : printf ( "Nine"
);
}

3. Given that sizeof(int) = 4,
predict the output.
int main ( ){
printf ( "%x", -1 << 2 );
}

4. A very simple question. Just
guess the outcome and explain.
int main() {
printf("%d", --2 );
}

5. Write a macro to swap two
variables of any data-type.

6. Predict the output.
void disp ( char a, char b,
char c )
{
printf ( "%c%c%c",c, b,a);
}
int main ( )
{
char p = 'A';
disp ( p, p++, ++p );
}


7. Given char grain[] = "wheat".Write the simplest code to print
"wheat heat eat at t".


8. Output ?
int main ( )
{
int x = 10, y = 2;
x *= y++ + 3;
printf ( "%d %d", x, y );
return 0;
}


9. Print "Hello, World" without
using a single semicolon in your
program file.

10. Write a program which
reports whether your machine
uses Big Endian or
Little Endian
representation.
------------------------------------------------
Solutions
Note : All solutions are with
respect to gcc on the Linux platform

C :

1. 0 -1 0 3 1
Key : In a && b, if a is
evaluated as 0, then b is not
evaluated and the
result is set to 0.
In a || b, if a is
evaluated as non-zero then b is
not evaluated and
the result is set to 1.


2. Nine
Key : Wherever the default
statement be, it is always
evaluated if none of
the labels match.


3. Since sizeof(int) = 4,
-1 is represented as ffffffff
Hence, -1<2 is fffffffc


4. You will get an error saying
invalid lvalue for decrement.
Key : When there is no space
between two '-' signs, the lexer
interprets them
as the decrement
operator and you CANNOT
decrement or increment
constants.


5. #define SWAP(a,b,X) {X
temp;temp = a;a = b;b = temp;}
X is the datatype!


6. Compiler dependent.A function call always involves arguments being pushed
on the stack.The order in which these argument are pushed will vary.
gcc implementation :
when func ( p, p++, ++p ) is
called, the code looks something
like this.
Note - arguments are pushed
on the stack in the reverse
order
INC LOC ; ++p
PUSH LOC ; push p ...'B'
was sent
PUSH LOC ; push p ...'B'
was sent
INC LOC ; p++
PUSH LOC ; push p ...'C'
was sent
Hence the output is BBC.


7. char grain[] = "wheat" ;
char *p = grain;
while ( *p != '\0' )
printf ( "%s ", p++ ); [
Please don't use grain++. Check
out Exploring C]


9. int main ( )
{
if ( printf ( "Hello,
World" ) ) { }
}
This works because gcc returns
from main implicitly[ no return
0 required].
Contact us if you have a
better solution.


10. int main()
{
int a = 1234;
//you dont know how this integer
is stored
char c = a;
//now, access the first byte of
location 'a'
if ( (c&0xff) == (a&0xff))
// check if that byte is same as
the LSB of
the integer
printf ( "Big Endian" );
else
printf ("Little Endian"
);
return 0;
}
Why c&0xff ? You think and
work out!

1. Predict the output.
int main() {
int i=-1,j=-1,k=0,l=2,m;
m=++i&&j++&&++k||l++;
printf("%d %d %d %d
%d\n",i,j,k,l,m);
}

2. Output ?
int i = 9;
switch ( i ) {
default : printf ( "No");
case 3 : printf (
"Three" );
case 6 : printf ( "Six"
);
case 9 : printf ( "Nine"
);
}

3. Given that sizeof(int) = 4,
predict the output.
int main ( ){
printf ( "%x", -1 << 2 );
}

4. A very simple question. Just
guess the outcome and explain.
int main() {
printf("%d", --2 );
}

5. Write a macro to swap two
variables of any data-type.

6. Predict the output.
void disp ( char a, char b,
char c )
{
printf ( "%c%c%c",c, b,a);
}
int main ( )
{
char p = 'A';
disp ( p, p++, ++p );
}


7. Given char grain[] = "wheat".Write the simplest code to print
"wheat heat eat at t".


8. Output ?
int main ( )
{
int x = 10, y = 2;
x *= y++ + 3;
printf ( "%d %d", x, y );
return 0;
}


9. Print "Hello, World" without
using a single semicolon in your
program file.

10. Write a program which
reports whether your machine
uses Big Endian or
Little Endian
representation.
------------------------------------------------
Solutions
Note : All solutions are with
respect to gcc on the Linux platform

C :

1. 0 -1 0 3 1
Key : In a && b, if a is
evaluated as 0, then b is not
evaluated and the
result is set to 0.
In a || b, if a is
evaluated as non-zero then b is
not evaluated and
the result is set to 1.


2. Nine
Key : Wherever the default
statement be, it is always
evaluated if none of
the labels match.


3. Since sizeof(int) = 4,
-1 is represented as ffffffff
Hence, -1<2 is fffffffc


4. You will get an error saying
invalid lvalue for decrement.
Key : When there is no space
between two '-' signs, the lexer
interprets them
as the decrement
operator and you CANNOT
decrement or increment
constants.


5. #define SWAP(a,b,X) {X
temp;temp = a;a = b;b = temp;}
X is the datatype!


6. Compiler dependent.A function call always involves arguments being pushed
on the stack.The order in which these argument are pushed will vary.
gcc implementation :
when func ( p, p++, ++p ) is
called, the code looks something
like this.
Note - arguments are pushed
on the stack in the reverse
order
INC LOC ; ++p
PUSH LOC ; push p ...'B'
was sent
PUSH LOC ; push p ...'B'
was sent
INC LOC ; p++
PUSH LOC ; push p ...'C'
was sent
Hence the output is BBC.


7. char grain[] = "wheat" ;
char *p = grain;
while ( *p != '\0' )
printf ( "%s ", p++ ); [
Please don't use grain++. Check
out Exploring C]


9. int main ( )
{
if ( printf ( "Hello,
World" ) ) { }
}
This works because gcc returns
from main implicitly[ no return
0 required].
Contact us if you have a
better solution.


10. int main()
{
int a = 1234;
//you dont know how this integer
is stored
char c = a;
//now, access the first byte of
location 'a'
if ( (c&0xff) == (a&0xff))
// check if that byte is same as
the LSB of
the integer
printf ( "Big Endian" );
else
printf ("Little Endian"
);
return 0;
}
Why c&0xff ? You think and
work out!

0 comment here::