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 - It takes two strings (array of characters) as arguments, compares these two strings lexicographically, and then returns some value as a result. The strcmp function in C is used to compare two strings, with syntax: int strcmp(const char *str1, ...
Discussions

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
How can I compare strings using switch in C?
You can't use a switch statement for this. But that doesn't mean the only alternative is a bunch of if statements. If you are checking against a number of strings, you could put them in an array and loop over each item, or better, you could use a hash table and look up each string as a key. By the way, you also can't use == to compare strings in C. You need to use strcmp() or something similar. More on reddit.com
🌐 r/learnprogramming
14
1
February 20, 2016
Fast String comparisons in C
The problem with the cast is that the pointers might be unaligned, potentially causing slowdowns or even crashes in the worst case when dereferenced. To fix that you need to check the alignment of both pointers at entry and work with the largest possible blocks whose alignment requirements match the least aligned string. Other than that, if I recall correctly the C standard allows casting between any pointer type and char or void pointers, so if you implement the aforementioned fix you should be fine. As for the performance of the code, heavily optimized strncmp implementations use compiler intrinsics that produce SIMD instructions. For example on a modern x86-64 CPU with AVX512 you can check whether two properly aligned 64 8-bit blocks are equal with a single instruction. More on reddit.com
🌐 r/learnprogramming
17
7
October 29, 2023
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
🌐
W3Schools
w3schools.com › c › ref_string_strcmp.php
C string strcmp() Function
For this comparison characters ... 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. There are three possible scenarios: If the end of both strings has been reached without any mismatches then the function returns zero. At the first mismatch, if the ASCII value of the character in the first ...
🌐
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. ...
🌐
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 both words are the same, strcmp() gives the number 0. strcmp() compares the corresponding characters from both strings based on their ASCII values, which are numeric codes that represent each character.
🌐
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; }
Find elsewhere
🌐
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

🌐
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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › compare-strings-in-c-plus-plus
3 Ways to Compare Strings in C++ | DigitalOcean
April 22, 2024 - Results out to be > 0 (greater than zero) when the second string is greater in comparison. ... #include <iostream> #include <string.h> int main() { const char *str_inp1 = "String Match"; const char *str_inp2 = "String Unmatch"; std::cout << "String 1: " << str_inp1 << std::endl; std::cout << "String 2: " << str_inp2 << std::endl; if (strcmp(str_inp1, str_inp2) == 0) std::cout << "\nBoth the input strings are equal."
🌐
TutorialsPoint
tutorialspoint.com › home › c_standard_library › c standard library strcmp function
C Standard Library strcmp Function
August 29, 2012 - The C Library strcmp() function is used to compare two strings. It checks each character in the string one by one until it finds a difference or reaches the end of the one string.
🌐
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

🌐
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 ...
🌐
IBM
ibm.com › docs › en › i › 7.5.0
strcmp() — Compare Strings
We cannot provide a description for this page right now
🌐
YouTube
youtube.com › watch
Programming with C: Functions & Comparing Strings - YouTube
Welcome back to our C programming series! In this lesson, we dive deep into string comparison and function fundamentals – essential skills for any C programm...
Published   June 5, 2025
🌐
Cplusplus
cplusplus.com › reference › cstring › strcmp
strcmp - cstring
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. This function performs a binary comparison of the characters.
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › compare strings with strcmp function in c
Compare Strings with strcmp() Function in C - Shiksha Online
December 21, 2023 - If str1 is less than str2, then a negative integer is returned. The comparison is done character by character, starting from the first character of each string, until a mismatch is found or the end of the strings is reached.
🌐
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.
🌐
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
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.compare
String.Compare Method (System) | Microsoft Learn
Options to use when performing the comparison (such as ignoring case or symbols). ... An integer that indicates the lexical relationship between the two substrings, as shown in the following table. ... Either strA or strB is null, and length is greater than zero. The following example uses the Compare(String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) method to compare the last names of two people.