Saturday, 25 February 2017

GATE EXAM PREPARATION IN "C" - Condition To Print “HelloWord”

Condition To Print “HelloWord”

What should be the “condition” so that the following code snippet prints both HelloWorld !
      if  "condition"
          printf ("Hello");
      else
          printf("World"); 
Solution:
#include<stdio.h>
int main()
{
    if(!printf("Hello"))
        printf("Hello");
    else
        printf("World");
    getchar();
}       

Explanation: Printf returns the number of character it has printed successfully. So, following solutions will also work
if (printf(“Hello”) < 0) or if (printf("Hello") < 1)

No comments:

Post a Comment