If you want to compare the first character in tmp
with the character 'x'
, then use:
if (tmp[0] == 'x')
{
...
}
If you want to compare the entire string in tmp
with the string "xyz"
, then use:
if (strcmp(tmp,"xyz") == 0)
{
...
}
Behind the scene:
- When you use a double-quoted string of characters in your code, the compiler replaces it with a pointer to the beginning of the memory space in which the string will reside during runtime.
- So by
tmp[0] == "t"
, you are actually attempting to compare a single character (typically 8 bits of data) with a memory address (typically 32 or 64 bits of data, depending on your system).