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.
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.
Ok a few things: gets is unsafe and should be replaced with fgets(input, sizeof(input), stdin) so that you don't get a buffer overflow.
Next, to compare strings, you must use strcmp, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=) compares the address of the two strings, as opposed to the individual chars inside them.
And also note that, while in this example it won't cause a problem, fgets stores the newline character, '\n' in the buffers also; gets() does not. If you compared the user input from fgets() to a string literal such as "abc" it would never match (unless the buffer was too small so that the '\n' wouldn't fit in it).
Videos
How do we compare a substring of a string in C?
What is string comparison in C?
Can we compare strings in C without `strcmp()`?
Because argv[1] (for instance) is actually a pointer to the string. So all you're doing is comparing pointers.
You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.
The compiler sees a comparison with a char* on either side, so it does a pointer comparison (which compares the addresses stored in the pointers)
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
I am an experienced dev, but have no C experience and i'm currently working through reverse engineering one of the flipper zero apps and this really caught me off guard
I'm very sorry if this has been explained before I'm just trying to wrap my head around it