4 votes 4 votes Read this code snippet : void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf(" GATE 2018\n"); else printf("Forget GATE\n"); } The output is : Compiler Error Forget GATE GATE $2018$ Runtime error Programming in C tbb-programming-2 + – Bikram asked May 14, 2017 • edited Aug 19, 2019 by Counsellor Bikram 737 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 6 votes 6 votes Printf returns number of characters printed. Printing NULL will return 0. But there is a new line after NULL. (\n) Because of that new line, 1 will be returned by printf. So output is C. Ahwan answered Aug 20, 2017 • selected Aug 21, 2017 by Bikram Ahwan comment Share Follow See all 0 reply Please log in or register to add a comment.
2 votes 2 votes Explanation: Printf will return how many characters does it print. Hence printing a new line character "\n" returns 1 which makes the if statement true, thus "GATE 2018" is printed. Bikram answered May 14, 2017 • edited Aug 21, 2017 by Bikram Bikram comment Share Follow See all 10 Comments See all 10 10 Comments reply Show 7 previous comments srestha commented Jun 12, 2017 reply Follow Share ok, tnks :) 1 votes 1 votes Ahwan commented Aug 20, 2017 reply Follow Share @Bikram sir, printing NULL returns 0, because of that new line, \n 1 is returned. You can test by removing new line. 3 votes 3 votes Bikram commented Aug 21, 2017 reply Follow Share yes, due to '\n' the condition part of if() becomes true . here i remove '\n' , now it becomes https://ideone.com/y6aibK gives out put Forget GATE 2 votes 2 votes Please log in or register to add a comment.
0 votes 0 votes void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf(" GATE 2018\n"); else printf("Forget GATE\n"); } Because of \n inside printf condition will become true (\n is newline character so it will print nothing but will insert a new line) and output will be GATE 2018. void main() { int i; char a[]="\0"; if(printf("%s",a)) printf(" GATE 2018\n"); else printf("Forget GATE\n"); } Now output will be : Forget GATE Some ref : https://stackoverflow.com/questions/18654465/how-0-is-treated-in-printf#:~:text=So%20empty%20is%20printed%20as%20output.&text=The%20main%20concept%20here%20is,in%20the%20statement%20if%20any%20 KUSHAGRA गुप्ता answered Jul 27, 2020 KUSHAGRA गुप्ता comment Share Follow See all 0 reply Please log in or register to add a comment.