Code Spot

Programming is the field which needs lot of practice to become familiar to the particular programming language. We can’t say that we are perfect in particular language. For example, in C language only there are lot of things that we have not yet focused. Thats why someone said, “ C is Sea”. Below I have collected some funny tweaks with C. Just go through them.

1. What is the output of the below C statement?

int main( )
{
printf(“Hello”, “World”);

}

Output:  Hello

***************************************************************************************************************

2.  What is the output of below code snippet?

void main( )
{
printf(5+”Good Day”);
}

Output: Day ( It eats up first 5 characters. )

*****************************************************************************************************************

3. How do you print 20% milk using printf function in C?

Ans: printf(“20%% milk”);

****************************************************************************************************************

4. How do you print “ I\nLove\nComputers\n” using printf in C?
Ans:  printf(“I\\nLove\\nComputers\\n”);

*****************************************************************************************************************

5. What is the output of the following code snippet?
#include<stdio.h>
main( )
{
int k=1;
printf(“%d==1 is %s”,k, k==1?”TRUE”:”FALSE”);
return 0;
}

Ans: 1==1 is TRUE

******************************************************************************************************************
6. What is the output of the following code?
#include<stdio.h>
#include<math.h>
int main()
{
float n=1.54;
printf(“%f%f”,ceil(n), floor(n));
return 0;
}

Output:  2.0000  1.0000 ( floor( ) function returns  integer value less than ‘n’ and ceil( )  function returns integer value greater than ‘n’)

*******************************************************************************************************************

7. Find the output of following snippet.
#include<stdio.h>
#include<math.h>
void main( )
{
printf(“%d,%d,%d”, sieof(3.14f), sieof(3.14), sizeof(3.14l));
}

Output: 4, 8, 12
( 3.14f – floating point variable, 3.14 – double variable, 3.14l – long double variable )

*******************************************************************************************************************
8. Find the output:
#include<stdio.h>
int main( )
{
char p[ ]=”%d”;
p[1]=’c’;
printf(p,65);
return 0;
}

Output:  A

******************************************************************************************************************

9.   Write the code snippet to execute both IF and ELSE conditions in program.

Ans:   #include<stdio.h>
int main( )
{
if(1)
{
printf( “ Hello”);
goto world;
}
else
{
world: printf(“World”);
}
}

*****************************************************************************************************************

10. What is the output?
#include<stdio.h>
void main( )
{
int a=5;
int b=10;
{
int a=2;

a++; b++;
}
printf(“%d %d”, a,b);
}

Output: 5 11

****************************************************************************************************************

11. Find the  output…..
#include<stdio.h>
void main( )
{
int a = sizeof(signed);
int b = sizeof(unsigned);
printf(“%d, %d”, a, b);
}

Output:  4, 4

****************************************************************************************************************

12. Find the output…………
#include<stdio.h>
void main()
{
int a= sizeof(const);
int b= sizeof(volatile);
printf(“%d,%d”, a,b);

Output: 4, 4

*****************************************************************************************************************

13. Find the output……….
#include<stdio.h>
void main()
{
int a= sizeof(signed)+sizeof(unsigned);
int b= sizeof(const)+ sizeof(volatile);
printf(“%d”, a++ + b);
}

Output:   16

*****************************************************************************************************************

14. Find the output……..

#include<stdio.h>

int main()

{

int a=0, b=1, c=3;

*((a)?&b:&a)=a?b:c;

printf(“%d%d%d”, a,b,c);

return 0;

}

Output: 3, 1, 3

****************************************************************************************************************

15. What is the output of this program?

#include<stdio.h>

int main()

{

char not;

not=!2;

printf(“%d”,not);

}

Output:  0

*****************************************************************************************************************

16. What is the ouput of this program?

#include<stdio.h>

int main()

{

extern int i;

i=20;

printf(“%d”,sizeof(i));

}

(A) Compiler Error          (B) 20            (C) 2            (D) Linker Error

Ans: (D) Linker Error ( The integer variable  ‘i’ is allocated space in external hard disk )

********************************************************************************************************************

17. What is the output of this program?

#include<stdio.h>

#define square(x)   x*x

int main()

{

int i;

i=64/square(4);

printf(“%d”,i);

}

(A) 4        (B) 16         (C) 64         (D)   None

Ans:  (C) 64

***********************************************************************************************************************

18. What is the output of this program ?

#include<stdio.h>

main( )

{

int i=1, j=2;

switch(i)

{

case 1: printf(“GOOD”);

break;

case j:  printf(“BAD”);

break;

}

(A) GOOD          (B) BAD              (C)  Syntax Error            (D)  Compiler Error

Ans:  Compiler Error

***********************************************************************************************************************

19. Find the output……

#include<stdio.h>

main( )

{

static int var=5;

printf(“%d”,var–);

if(var)

main( );

}

(A) 1 2 3 4 5            (B)   5 5 5 5 5        (C)  5 4 3 2 1           (D)  None of these

Ans:  (C) 5 4 3 2 1

***********************************************************************************************************************

20. What is the output of this program ………….

main( )

{

int i= _l_abc(10);

printf(“%d\n”,–i);

}

int _l_abc(int i)

{

return(i++);

}

(A) 10         (B)   8          (C)  9    (D) 11

Ans:  (C) 9

*************************************************************************************************************************

21.  Give the output for below…………

#include<stdio.h>

main( )

{

printf(“%x”, -1<<3);

}

( Note: Assuming size of  integer as 2 bytes and %x format specifier for hexadecimal).

Output: fffffff8

*************************************************************************************************************************

Advertisements

Leave a comment

Filed under PROGRAMMING

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s