+ 2

Null is an character then why not if part is evaluated here??

#include <stdio.h> int main() { char a[] = "\0"; if(printf("%s", a)) printf("the string is empty"); else printf("the string is not empty"); return 0; }

15th Nov 2019, 10:50 AM
Preity
Preity - avatar
1 Réponse
+ 6
Preity printf function returns number of characters *successfully printed* to console screen. (You probably know) '\0' is still a charecter but when it's used as a part of a string it actually terminates string. Try to run: printf("%s","foo\0bar"); You'll get "foo" (not "foo\0bar") as resulting string. Since you don't actually print a null and use it for indicating end of string, printf will not count it as a printed charecter and will return 0 for an empty string ended with '\0' This `0` number returned by printf is implicitly typecasted to boolean `false` , causing `else` part to be executed instead of `if` .
15th Nov 2019, 11:40 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar