Knowee
Questions
Features
Study Tools

#include <stdio.h> int main() { char *str = "hello, world"; char *str1 = "hello, world"; if (strcmp(str, str1)) printf("equal"); else printf("unequal"); } equalunequalCompilation error Depends on the compiler

Question

#include <stdio.h> int main() { char *str = "hello, world"; char *str1 = "hello, world"; if (strcmp(str, str1)) printf("equal"); else printf("unequal"); } equalunequalCompilation error Depends on the compiler

🧐 Not the exact question you are looking for?Go ask a question

Solution

The code you've posted is written in C language. It's trying to compare two strings using the strcmp function. However, there's a mistake in the code. The strcmp function returns 0 when the strings are equal, and a non-zero value when they are not. So, the condition in the if statement should be !strcmp(str, str1) instead of strcmp(str, str1). Here's the corrected code:

#include <stdio.h>
#include <string.h> // This line is added to include the string.h library where strcmp function is declared

int main()
{
    char *str = "hello, world";
    char *str1 = "hello, world";
    if (!strcmp(str, str1)) // Here ! is added before strcmp
        printf("equal");
    else
        printf("unequal");

    return 0; // This line is added to return 0 at the end of main function
}

With these corrections, the program will print "equal" if the strings are the same, and "unequal" if they are not.

This problem has been solved

Similar Questions

0/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.