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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ strcmp-in-c
strcmp() in C %%sep%% %%sitename%% - GeeksforGeeks
December 13, 2025 - The strcmp function in C is used to compare two strings, with syntax: int strcmp(const char *str1, const char *str2);, where str1 and str2 are the strings to compare.
Discussions

How should I compare strings correctly in C? - TestMu AI Community
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. Any advice on how to string compare in C the ... More on community.testmuai.com
๐ŸŒ community.testmuai.com
0
June 20, 2025
Fast String comparisons in C
Two problems I can think of, off the top: Alignment. Some processors might want accesses to be aligned on certain boundaries, so the uint32_t access of an arbitrary string could be problematic. I think you're making an assumption that len is no larger than the size of the largest string. If someone were to pass in a "len" that was much larger than either string (e.g. both strings are in a buffer of a max size, and they pass in the max size), then you could easily walk off the end of the strings in the first loop, since you're not checking for the null terminator. And I just don't know if the semantics of strncmp requires that to not be true (that len can't be arbitrary). Since you'd be potentially comparing beyond the end of the string, you could say two equal strings aren't, since you're looking at characters past the null terminator. More on reddit.com
๐ŸŒ r/cprogramming
14
1
October 29, 2023
simple comparison between std::string and c-string
That person has probably misunderstood a post referring to comparing string literals. In C/C++ doing something like: char string[] = "test"; if (string == "test") { ... } Which isn't technically UB, but it certainly doesn't do what you're probably expecting. C/C++ have no built-in string comparator, what you're comparing here is the pointers, one being the pointer to a stack buffer of character and the other being a pointer to the raw string literal, which will not point to the same thing. What's UB as far as I know is this: const char* string = "test"; if (string == "test") { ... } If I remember correctly the standard doesn't specify that two identical string literals defined in your code can't be placed twice in the binary, so even if this check will pass on basically any compiler, it's still UB std::string does the right thing for you since it implements operator== between it and a null terminated string which does an actual comparison of the characters. Long story short, your friend is wrong. More on reddit.com
๐ŸŒ r/cpp_questions
26
7
August 26, 2023
Compare string with ">", "<", "=" in c
I want to know what is compared when we have s1 > s2 and what is the result (for example when we compare chars their ASCII corresponding codes are compared). I read that with this operation are More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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; }
๐ŸŒ
TestMu AI Community
community.testmuai.com โ€บ ask a question
How should I compare strings correctly in C? - TestMu AI Community
June 20, 2025 - 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. Any advice on how to string compare in C the ...
๐ŸŒ
Oreate AI
oreateai.com โ€บ blog โ€บ beyond-understanding-c-string-comparison-with-strcmp โ€บ cedd23835a81dc95b2196d1385fa4e96
Beyond '==' : Understanding C String Comparison With Strcmp() - Oreate AI Blog
January 27, 2026 - They're both required, as you can't compare something with nothing! Now, the real magic happens with the return value. strcmp() doesn't just give you a 'yes' or 'no'. It provides a nuanced answer: A value less than 0: This tells you that string1 comes before string2 alphabetically.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-compare-two-strings-without-using-strcmp-function
C Program to Compare Two Strings Without Using strcmp() - GeeksforGeeks
December 5, 2024 - Explanation: This method iterates through both strings simultaneously, compares each character and returns the difference between the last compared characters. If the last compared character differs, it returns non-zero value.
๐ŸŒ
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

๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ c โ€บ string โ€บ byte โ€บ strcmp
strcmp - cppreference.com
May 30, 2024 - Zero if lhs and rhs compare equal. Positive value if lhs appears after rhs in lexicographical order. This function is not locale-sensitive, unlike strcoll and strxfrm. ... #include <stdio.h> #include <string.h> void demo(const char* lhs, const char* rhs) { const int rc = strcmp(lhs, rhs); const char* rel = rc < 0 ?
๐ŸŒ
Learnsic
learnsic.com โ€บ blog โ€บ making-comparisons-using-the-c-strcmp-function
Making Comparisons Using the C strcmp Function
March 20, 2024 - This website uses cookies to ensure you get the best experience & by continuing to use our website, you agree to our Privacy and Cookie Policy ยท Got it
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cstring โ€บ strcmp
strcmp - cstring
Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
๐ŸŒ
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 - If the two strings are equal up to the end of the shorter string, strcmp() returns a negative, zero, or positive value depending on whether the longer string has a character with a smaller, equal, or greater ASCII value than the null character. Actually, the C compiler implements this logic of comparing the ASCII values of characters in two strings and returning the result accordingly.
๐ŸŒ
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
Answer (1 of 6): There is no way to compare strings efficiently. because they are strings. Is far better to compare 2 numbers than 2 strings. because when you compare numbers you ony need to compare 1 number. and such number can never be null. ...
๐ŸŒ
Diffchecker
diffchecker.com
Compare text and find differences online or offline - Diffchecker
Compare text, files, and code (e.g. json, xml) to find differences with Diffchecker online for free! Use our desktop app for private, offline diffs.
๐ŸŒ
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

๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ fast string comparisons in c
r/learnprogramming 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

๐ŸŒ
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.