What will be the output of the following C code? #include <stdio.h> int main() { char *str = "hello, world"; char *str1 = "hello, world"; if (strcmp(str, str1)) printf("equal"); else printf("unequal"); }
Question
What will be the output of the following C code? #include <stdio.h> int main() { char *str = "hello, world"; char *str1 = "hello, world"; if (strcmp(str, str1)) printf("equal"); else printf("unequal"); }
Solution
The output of the given C code will be "unequal".
This is because the strcmp() function in C returns 0 if the two strings are identical. In the if condition, a 0 is considered as false. So, if the strings are identical, the else part will be executed, printing "unequal".
If you want to print "equal" when the strings are identical, you should negate the condition, like this: if (!strcmp(str, str1)).
Similar Questions
what will be the output of the c program?
What will be the output of the following code?
What will be the output of the following C++ code?
What will be the output of the following program?
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.