Compare Complete Strings
Check an incoming label
The desk should release a part only when its received label matches the approved one. You cannot compare two arrays as whole values with ==; use strcmp from <string.h> to compare their terminated character sequences. Zero means they match. A negative result means the first string sorts before the second; a positive result means it sorts after. Test the sign, not a particular value such as -1 or 1.
#include <stdio.h>
#include <string.h>
int main(void) {
char received[] = "Bolt";
char approved[] = "Bolt";
if (strcmp(received, approved) == 0) {
printf("Labels match\n");
} else {
printf("Review label\n");
}
return 0;
}
Run the example and look for Labels match. If received held "Belt" instead, the else branch would print Review label. Both arrays must contain terminated strings. A memory address identifies where an object is stored; two different arrays can hold the same text. The pointers section will explain addresses, but you can already compare the text correctly with strcmp.