818 views
0 0 votes

when i printing x it is giving 0 but when comparing x with 5 ,it executing else part ,means condition fail ,why ??

#include <stdio.h>

int main(void) {
int x;
     if(x=(printf("Hello")) != 5)
      printf("Hello");
   else
      printf("World \n");
     printf("%d  \n",x);
    return 0;
}

https://ideone.com/NJSj2i

1 Answer

0 0 votes
yes as printf returns number of characters it print so here printf("Hello") will return 5 and the condition 5!=5 become false due to which x will get 0.Change "HELLO" with "HY" 2!=5 true hence x=1
Position:
Show:

Related questions

2 2 votes
3 3 answers
4.7k
4.7k views
Gurdeep Saini asked Jan 14, 2019
4,696 views
#include <stdio.h>int arr[] = { 10, 20, 30, 40, 50 };static int count ;inc() {return ++count;}int main(){arr[count++]=inc();printf("%d ", arr[count]);printf("%d ", arr[0]...
0 0 votes
0 0 answers
792
792 views
utkarshkosta asked Dec 22, 2018
792 views
#include<stdio.h int main() { char A[5][7][6]; char *p[5][7][6]; printf(“%d\t”,(unsigned)(A+1)-(unsigned)A); printf(“%d”,(unsigned)(p+1)-(unsigned)p); } OPTIONS:42 ...
1 1 vote
2 answers 2 answers
831
831 views
rahul sharma 5 asked Dec 14, 2016
831 views
For n=2 the P is coming as 3,but none of the option is satisfying?
0 0 votes
0 0 answers
505
505 views
Mrityudoot asked Feb 26, 2024
505 views
Trying out a code to get all the possible marks that can be obtained in a GATE paper.Assumptions taken : 1) All questions are MCQs (all questions have a chance of negativ...