You can't (usefully) compare strings using != or ==, you need to use strcmp:

while (strcmp(check,input) != 0)

The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.

Answer from Mysticial on Stack Overflow
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strcmp
C strcmp() - C Standard Library
#include <stdio.h> #include <string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; int result; // comparing strings str1 and str2 result = strcmp(str1, str2); printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %d\n", result); return 0; }
🌐
Cplusplus
cplusplus.com › reference › string › string › compare
std::string::compare
Compares the value of the string object (or a substring) to the sequence of characters specified by its arguments. The compared string is the value of the string object or -if the signature used has a pos and a len parameters- the substring that begins at its character in position pos and spans ...
🌐
Scaler
scaler.com › home › topics › string comparison in c
String Comparison in C - Scaler Topics
January 11, 2024 - Comparing two strings or string comparison in C involves finding out if they are the same or not. This is done by using some built-in function or comparing the strings character by character to determine if they are equal.
🌐
freeCodeCamp
freecodecamp.org › news › strcmp-in-c-how-to-compare-strings-in-c
strcmp in C – How to Compare Strings in C
April 27, 2023 - The strcmp() function is part of the standard C library ( string.h ). Its primary purpose is to compare the characters of the two strings in sequence until it finds a mismatch or until the end of the strings is reached (that is, the null character ...
🌐
Quora
quora.com › How-can-we-compare-two-strings-efficiently-in-C-or-C
How can we compare two strings efficiently in C or C++? - Quora
So keeping an array of strings to just compare is far worst than keeping an array of integers. ... In C and C++, you can compare two strings efficiently using the standard library function `strcmp()`.
🌐
W3Schools
w3schools.com › c › ref_string_strcmp.php
C string strcmp() Function
For this comparison characters at the same position from both strings are compared one by one, starting from the left until one of them does not match or the end of a string has been reached.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › stdstringcompare-in-c
std::string::compare() in C++ - GeeksforGeeks
May 6, 2025 - The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file.
Find elsewhere
🌐
Reddit
reddit.com › r/cpp_questions › simple comparison between std::string and c-string
r/cpp_questions on Reddit: simple comparison between std::string and c-string
August 26, 2023 -

Hello,

I am discussing with a person who believes that

std::string msg = "Hello";

if (msg == "Hello") { // UB?
   ...
}

According to the person, this line "if (msg == "Hello") {" is UB.

Have you seen this "operator==" as UB?

I have checked the C++ specification (from c++11), and it's acceptable. I checked the std::string implementation (GNU GCC 5 and the latest) and operator== calls std::string::compare and then "traits". Passing cstring in operator== is fine too...

Toolchain is ARM for 32bit architecture and std is c++11/14.

His arguments are based on Stackoverflow posts which are more than 10y ago...

Thank you

🌐
Codecademy
codecademy.com › docs › c# › strings › .compare()
C# (C Sharp) | Strings | .Compare() | Codecademy
April 16, 2023 - Compares the alphabetical order of two strings and returns an integer that represents their relative ranking.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › how-to › compare-strings
How to compare strings - C# | Microsoft Learn
It compares the binary value of each Char object in two strings. As a result, the default ordinal comparison is also case-sensitive. The test for equality with String.Equals and the == and != operators differs from string comparison using the ...
🌐
Particle
docs.particle.io › reference › device-os › api › string-class › compareto
compareTo() - String class | Reference | Particle
The strings are compared character by character, using the ASCII values of the characters. That means, for example, that 'a' comes before 'b' but after 'A'. Numbers come before letters. String comparison only compares the 8-bit ASCII values. It does not compare UTF-8 encoded strings properly.
🌐
Cppreference
en.cppreference.com › w › cpp › string › basic_string › compare.html
Cppreference
Then compare the sequences by calling Traits::compare(data1, data2, rlen). For standard strings this function performs character-by-character lexicographical comparison.
🌐
DigitalOcean
digitalocean.com › community › tutorials › compare-strings-in-c-plus-plus
3 Ways to Compare Strings in C++ | DigitalOcean
April 22, 2024 - This code directly compared a string with another input string to the compare() function. C++ Relational operators such as == (double equals) and != (not equals) can be helpful in the comparison of strings.
🌐
Cplusplus
cplusplus.com › forum › beginner › 34698
Is there a string compareTo() ? - C++ Forum
If you're not familiar with this, imagine comparing 2 strings · Result will become a negative value since "Hello" comes before "Yo" in an alphabetic order. Comparing str2.compareTo(str1) would generate a positive value. Does this exists in C++? filipe (1165) str1 > str2 would evaluate to false.
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › string_h › strcmp.php
C Language: strcmp function (String Compare)
In the C Language, the required header for the strcmp function is: ... /* Example using strcmp by TechOnTheNet.com */ #include <stdio.h> #include <string.h> int main(int argc, const char * argv[]) { /* Create a place to store our results */ int result; /* Create two arrays to hold our data */ char example1[50]; char example2[50]; /* Copy two strings into our data arrays */ strcpy(example1, "C programming at TechOnTheNet.com"); strcpy(example2, "C programming is fun"); /* Compare the two strings provided */ result = strcmp(example1, example2); /* If the two strings are the same say so */ if (result == 0) printf("Strings are the same\n"); /* If the first string is less than the second say so (This is because the 'a' in the word 'at' is less than the 'i' in the word 'is' */ if (result < 0) printf("Second string is less than the first\n"); return 0; }
🌐
Mkyong
mkyong.com › home › java › java string compareto() examples
Java String compareTo() examples - Mkyong.com
January 24, 2022 - System.out.println("a".compareTo("b")); ... System.out.println("1".compareTo("5")); // -4 · The "c" lexicographically follows the argument string "a"; it returns positive integer 2....
🌐
TestMu AI Community
community.testmuai.com › ask a question
How should I compare strings correctly in C? - TestMu AI Community
June 19, 2025 - I’m building a simple C program that asks the user to input a word or character, stores it, and keeps printing it until the user types the same input again to exit. However, even when the user retypes the original input, the loop doesn’t stop. Am I using the wrong method for string comparison in C? I’m aware that using check != input may not be the correct approach, but I’m unsure how to properly handle this.
🌐
Reddit
reddit.com › r/cprogramming › fast string comparisons in c
r/cprogramming on Reddit: Fast String comparisons in C
October 29, 2023 -

Hi, I'm working just for fun on a fast comparison for strings written in C, with the intent of being faster than the normal strncmp function, which is currently the code bellow

```
int fast_strncmp(const char *str1, const char *str2, int len) {
    const char *final_pos = (str1 + len) - 4;
    while (str1 < final_pos) {
        // if characters differ, or end of the second string is reached
        if (*((uint32_t *)str1) != *((uint32_t *)str2)) {
            break;
        }
        // move to the block of characters
        str1 += 4;
        str2 += 4;
    }
    final_pos += 4;
    while (str1 < final_pos) {
        if (*str1 != *str2 || *str1 == 0 || *str2 == 0) {
            return *str1 - *str2;
        }
        // move to the next pair of characters
        str1++;
        str2++;
    }
    return 0;
}
```

Is there any clear problem with the code that could make it a bad option for fast string comparisons. When I wrote it a couple of weeks ago, I didn't think there could be any problem with it, but this week I was watching a couple of videos about C programming and it was mentioned that casting an array of 4 uint8_t to a uint32_t could be a problem. I'm even using this function at work and haven't had a single problem or warning, but since I'm planning to make a youtube video about it, I want to guarantee that my code won't be a problem for other people.

On top of that I've made a couple of benchmarks on the performance to be sure it really is fast, so I've compared it to strncmp and an implementation by https://github.com/mgronhol, that I found here: https://mgronhol.github.io/fast-strcmp/, which got me the following results:

EDIT: reddit was not cooperating with me posting the results text in a well formatted way, so here's the link to the file:

https://github.com/BenjamimKrug/fast_string_comparison/blob/main/results.txt

As you can see, running on the STM32 and the ESP32, my algorithm runs faster by a little compared to the fast_compare function by mgronhol, but running on my PC, it's performing terribly. Does anyone know why that is?

You can find more info about the code in my github repository where I put everything related to this test: https://github.com/BenjamimKrug/fast_string_comparison

P.S.: Sorry if this is the wrong subreddit for this kind of thing, I was going to post it on r/programming, but after reading the rules, I saw that maybe it was best to post it here.

EDIT: fixed code formatting