27 27 votes What will be the output of the following code? #include <stdio.h> #include <string.h> int main() { char string[] = "Hello"; if(sizeof(string) <= strlen(string)) printf("1"); printf("0"); } ______ Programming in C go-programming-1 programming-in-c programming numerical-answers one-mark + – Arjun 4.7k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 24 24 votes char string[] = "Hello"; //'\0' is added by compiler at end of string literals sizeof(string) = (5+1) = 6 //+1 to store EndOfString char '\0'. strlen(string) = 5 if(sizeof(string) <= strlen(string)) returns False so it wont print 1 and finally it will print 0. Digvijay Pandey answered Oct 20, 2016 • selected Oct 20, 2016 by Arjun Digvijay Pandey comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote Answer will be Zero as strlen() function always gives the actual length of string excluding '\0', but '\0' is always present at the end of string taking space in memory so it is counted in sizeof operator. adeemajain answered Sep 6, 2018 adeemajain comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes 0 because sizeof(string) = strlen(string) Sumant Singh Rajput answered Sep 18, 2017 Sumant Singh Rajput comment Share Follow See 1 comment 1 1 comment reply Arjun commented Sep 18, 2017 reply Follow flag No. sizeof includes null char at the end but strlen ignores it. 2 2 replyShare Please log in or register to add a comment.
0 0 votes it will print 0 as a o/p SHIVAM GARG answered Jul 25, 2018 SHIVAM GARG comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Sizeof(String) = 6 Strlen(String) = 5 Since if condition fails answer = 0 Bharathchinna0402 answered Nov 16, 2018 Bharathchinna0402 comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes The condition fails and print 0 apptrendz1 answered Jan 1, 2019 apptrendz1 comment Share Follow 0 reply Please log in or register to add a comment.