Showing results for string equals c
Search instead for string equals c c

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 - Explanation: In this code, strcmp is used to compare two strings, s1 and s2. Since both strings are identical ("g f g"), strcmp returns 0, indicating the strings are equal. The program then prints "Equal".
Discussions

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
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
This is a super noob question, but strcmp returns 0 if the strings are equal and "truthy" if they are not equal, like if(-1) is true? Is there a different way to compare strings or do I just need to wrap my head around this?
strcmp returns 0 => the strings are equal 1 => the first string is "after" the second when sorted -1 => the first string is "before" the second when sorted It's not really a function to tell you if 2 strings are equal, it's to tell you which "order" the 2 strings are in. It just coincidentally is usable to tell you if 2 strings are equal, because if they are it returns 0 Anything non-zero means the strings aren't equal More on reddit.com
🌐 r/C_Programming
28
9
October 16, 2023
Why pointers to strings are equals betweent them, but array of chars are different between them
Pointers to string literals don't have to be equal, "foo" == "foo" can be false. Most compilers will reuse identical string literals so both "foo" have the same address, but the compiler is also free to make two different "foo" strings with different addresses. You have to remember that the == operator never compares the contents of the string, it only compares the address of the string. Your comparison a == b will never be true. a and b refer to different arrays, and those arrays will never have the same address. More on reddit.com
🌐 r/C_Programming
16
5
March 17, 2024
🌐
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; }
🌐
W3Schools
w3schools.com › c › ref_string_strcmp.php
C string strcmp() Function
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 string is greater then the function returns a positive number.
🌐
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 if statement checks the value of result and prints out the corresponding message depending on whether the strings are equal, or which string is less or greater. Here, the two strings become equal after converting them to lowercase.
🌐
Scaler
scaler.com › home › topics › string comparison in c
String Comparison in C - Scaler Topics
January 11, 2024 - If both strings have the same character at the same index till all of the characters have been compared or the pointer reaches the null character '\0' in both strings then we can say that both strings are equal.
Find elsewhere
🌐
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. for example: [code]int x; … // x is assigned some ...
🌐
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

🌐
DigitalOcean
digitalocean.com › community › tutorials › compare-strings-in-c-plus-plus
3 Ways to Compare Strings in C++ | DigitalOcean
April 22, 2024 - OutputString 1: String Match String 2: String Match Both the input strings are equal. strcmp(str_inp1, str_inp2) results in 0. The values of str_inp1 and str_inp2 are the same. C++ has a built-in compare() function to compare two strings.
🌐
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

🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.equals
String.Equals Method (System) | Microsoft Learn
The string to compare to this instance. ... true if obj is a String and its value is the same as this instance; otherwise, false. If obj is null, the method returns false. The following example demonstrates the Equals method.
🌐
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 ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › strcmp in c
strcmp in C | String Comparison Function with Examples
June 3, 2025 - The strcmp in C function is used to compare two strings character by character. It returns 0 if both strings are equal, a positive value if the first string is greater, and a negative value if it’s smaller.
🌐
TutorialsPoint
tutorialspoint.com › home › c_standard_library › c standard library strcmp function
C Standard Library strcmp Function
August 29, 2012 - Following is the syntax of the C library strcmp() function − ... Zero, if the string are equal.
🌐
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.compare
String.Compare Method (System) | Microsoft Learn
To determine whether two strings are equivalent, call the Equals method. The comparison can be further specified by the options parameter, which consists of one or more members of the CompareOptions enumeration.
🌐
Quora
quora.com › How-do-I-check-if-two-strings-are-equal-in-C-1
How to check if two strings are equal in C - Quora
Answer (1 of 3): You can check whether two strings are equal or not by using strcmp() function. This is a built-in string function which is present in string.h header file. strcmp() function takes 2 strings as parameters and returns integer ...
🌐
Vultr
docs.vultr.com › clang › standard-library › string-h › strcmp
C string.h strcmp() - Compare Two Strings | Vultr Docs
September 27, 2024 - In C programming, comparing two strings is a common task that can be performed using the strcmp() function from the string.h library. This function is essential for operations like sorting lexicographically or checking string equality, which ...