How to Compare 2 Character Arrays [duplicate]

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");
}

Edit:

  • There is no need to specify the size when you initialise the character arrays. This would be better: char test[] = "idrinkcoke";
    char test2[] = "idrinknote";
  • It’d also be better if you use strncmp – which is safer in general (if a character array happens to be NOT NULL-terminated). if(strncmp(test, test2, sizeof(test)) == 0)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)