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 690 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 srestha commented Jun 10, 2017 reply Follow Share not getting . a[] contains end of string . So, printf should print NULL. And if then return false. Isnot it? 1 votes 1 votes Bikram commented Jun 10, 2017 reply Follow Share a[] ="\0"; means a[] contains a null character . if ( condition ) is true as a[] store a string value, see a[]="\0" as it store within " " so it is a string , that makes condition part true . so it prints GATE 2018 1 votes 1 votes srestha commented Jun 10, 2017 reply Follow Share No, NULL character means nothing to print. So, printf will print nothing. then if will return false. isnot it? 2 votes 2 votes Bikram commented Jun 11, 2017 i edited by Bikram Aug 21, 2017 reply Follow Share @srestha due to '\n' , the if() part returns true. when we write something like this " " it means a string . And the condition for if() returns 1 means true hence GATE 2018 is printed. Now about the output , you can check it yourself https://ideone.com/R2IMA7 1 votes 1 votes Learner_jai commented Jun 11, 2017 reply Follow Share ma'am {} ,{ empty string(fi) } are used in this question. 0 votes 0 votes srestha commented Jun 12, 2017 reply Follow Share ok but @Bikram Sir u have taken a character inside an array and declared in inside double quote. See here NULL no need to declare inside double quote. right? 1 votes 1 votes Bikram commented Jun 12, 2017 reply Follow Share @srestha Here statement is : char a[]="\0"; // That means , array a[] is an array of strings and \0 represents end of the strings. "When, compiler encounter strings, it appends a null character \0 at the end of string." - in this code snippet "\0 " is given that means the array elements are strings. NULL no need to declare inside double quote. right? No , in this example it should be under double quote , otherwise give error , see here https://ideone.com/rzcr1a , it says invalid initializer means when we declare an array of strings , it's elements should be declared under " " . The example you cited here : https://www.programiz.com/c-programming/c-strings name[i] = '\0'; // here i th element is having a null character. It is a single character ( i th element ) but in our code snippet we represent entire array so Double Quote is require. Hope things are clear now !! 2 votes 2 votes 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.