Follow me on Linkedin

Tricky C Questions



1) what will be the output of the following Printf function.

printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM"))));

Ans-ILOVECPROGRAM1321

The above printf line gives output like this because printf returns the number of character successfully written in the output.So the inner printf("%s","ILOVECPROGRAM") writes 13 characters to the output so the outer printf function will print 13 and as 13 is of 2 characters so the next outer printf function will print 2 and then next outer printf will print 1 as 2 is one character.So is the output 1321.

2) What will be the output of the following conditional operator.

a=0?(3>2?23:(2>5?(7<6?34:48):64)):1
printf("%d",a);

Ans-1

The above code snippet is very simple actually but it is made to look like that it is very tough and most of us start solving the nested part without thinking a bit although we know the concept.So first think for five minutes.The concept behind this is when we use conditional operator then if the condition is true then we select the value before the colon and if it is false then the value after the column.
For example a>b?a:b.If a is greater then b then a otherwise b.So we have 0 before ? operator that means false so no need to see the nested thing.The answer will be 1.

3) if(condition)
printf("I love" );
else
printf("C Language");

What should be the condition inside if statement such that it will print "I Love C Language" ?

Ans- if(!printf("I Love"))

As the printf returns the number of characters successfully written on output it will return 6.And making it not will invert and make it 0 so else statement will print out.

4) Program to identify even or odd number without using any arithmetic operator,conditional statement,logical operators,Relational operator.This program was asked in Microsoft written test.

Ans- int main()
{
scanf("%d",&no);
(no&1)?printf("odd"):printf("even");
}

The above question can be solved using bitwise AND operator as it is not mentioned in question not to use.And also you can use conditional operator as conditional statement cannot be used.Taking bitwise AND of any number with 1 will give value y where y=0 if number is Even and y=1 if number is Odd because even number always ends with 0 so 0001 & ---0 will always give 0.On the other hand odd number always have 1 in last position so 0001 & ---1 will always give 1.

5) Program to find the sum of the digits of a number in single statement.

Ans- int sum(int x)
{
int s;
for(s=0;x>0;s+=x%10,x/=10);
return s;
}

6) Print number from 1-100 without using loop,Recursion and Goto.

Ans - #include <stdio.h>
#include<conio.h>
#define STEP1 step();
#define STEP2 STEP1 STEP1
#define STEP4 STEP2 STEP2
#define STEP8 STEP4 STEP4
#define STEP16 STEP8 STEP8
#define STEP32 STEP16 STEP16
#define STEP64 STEP32 STEP32
#define STEP128 STEP64 STEP64

int n = 0;

int step()
{
if (++n <= 100)
printf("%d\n", n);
}

int main()
{
STEP128;
getch();
}

7) Deallocate memory without using free() in C

Ans- void *realloc(void *ptr,size_t size);
if size=0 then call to realloc is equivalent to free(ptr)

As realloc is used to deallocate previously allocated memory and if the size=0 then it will acts as free().


Tags : Tricky C Question , Tricky C++ Question , Tricky Interview Question , Interview Question with answers , Easy interview question with answers



a