Skip to content
doubtly

2 answers

Answer: strcmp(s1, s2) compares two strings character by character and returns 0 if they are equal, a negative value if s1 comes first, and a positive value if s1 comes later.

It is declared in string.h. Characters are compared one by one using their character codes (ASCII on most computers) until a difference or the null character '\0' is reached.

  • Returns 0 → both strings are identical.
  • Returns < 0 → s1 is less than s2 (e.g. "apple" vs "banana").
  • Returns > 0 → s1 is greater than s2.
if (strcmp(pass, "abc123") == 0)
    printf("Password correct");

It is used because == compares only the addresses of two strings, not their contents.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.