0

I am trying to take Command line input for my program; where I am giving a multicharacter input! The program works well for a single character input ./a.out b , but shows an error message for multi-character input! ./a.out bf .

I am facing problem in dereferencing and comparing that input! May someone kindly help me with this! Thanks

I/O input : ./a.out bf

Code:

int main(int argc, char* argv[]) 

{   char bf = 'N';

    if (argc>=2){
    if( *argv[1] == 'bf') # line with problem
    {
       char bf = 'Y';
       printf(" \n Bellmann-ford is in action! \n");
    }
    }
return 0
}

Error message:

main.c:6:6: warning: multi-character character constant [-Wmultichar]
    if( *argv[1] == 'bf') 
  • 1
    Note that 'bf' is not a string. It is a multi-byte character literal. I've never needed those (note that wide character is different), so unless you want to go down that rabbit hole by researching yourself, just remember you don't need them. – hyde Oct 20 '22 at 05:48
  • @hyde Just want to confirm : This is correct, right? `char check[2] = "bf"; ` `if (argc>=2)`{ ` if(strcmp(argv[1],check) != 0) ` { `char bf = 'Y'; ... and after that`. – Formal_that Oct 20 '22 at 06:02
  • 2
    `check[2]` cannot hold `"bf"` as there is no room for the terminating 0 byte. Should be `check[3]`. You also don't need to provide an array variable for this purpose. You can directly use `strcmp(argv[1], "bf")` – Gerhardh Oct 20 '22 at 07:01
  • 1
    `"bf"` is 3 bytes: 'b', 'f' and 0. So it is indeed not correct. C will silently discard the extra bytes from the string literal, so you have an array of 2 characters, 'b' and 'f', _but it is not a string_. – hyde Oct 20 '22 at 07:07
  • Now, shall I delete this question? Since, it has been marked as duplicate! – Formal_that Oct 20 '22 at 15:24

0 Answers0