Search code examples
cargvstrcmp

Using argv and strcmp at C


Im having some problem to use the strcmp function. Everytime I run this code, it just print the ERROR mensage, even if i use the correct argument at prompt.

if (strcmp(argv[1], "\?") == 0) {
    ajudaPrompt();
}else {
    printf ("ERROR.\n\n");
    system("pause");
}

Can someone help me?


Solution

  • The \ character is used to begin an escape code in C strings. For example, \n denotes a newline.

    Since \? is not a valid escape code, the backslash character is ignored. So the resulting string in the code is actually "?".

    If you want you string to contain a literal backslash, use two backslashes.

    if (strcmp(argv[1], "\\?") == 0) {