ThomasWest ThomasWest - 22 days ago 6
C Question

How to Compare 2 Character Arrays

How do I compare these two character arrays to make sure they are identical?

char test[10] = "idrinkcoke"
char test2[10] = "idrinknote"


I'm thinking of using
for loop
, but I read somewhere else that I couldnt do
test[i] == test2[i]
in C.

I would really appreciate if someone could help this. Thank you.

Answer

but I read somewhere else that I couldnt do test[i] == test2[i] in C.

That would be really painful to compare character-by-character like that. As you want to compare two character arrays (strings) here, you should use strcmp instead:

if( strcmp(test, test2) == 0)
{
    printf("equal");
}