I’m trying to do something like
strcmp(argv[3], "stdout")
however, in the command line I don’t want to type
stdout\0
what’s the best way to get rid of the \0 at the end of a string literal?
Thanks!
update:
Thanks guys. I found what’s wrong with my code… I should have used
strcmp(argv[3], "stdout") == 0
Thanks @Nicol Bolas
You don’t have to type “stdout\0” on the command line. Whichever way your system makes command-line arguments available to your process (it differs by operating system) automatically adds the null character.
As you know, a C-style string is terminated by the null character, which is written in code as ‘\0’. If that character weren’t at the end of the string, a function such as
strcmpwould keep going well beyond the end of the string, since such a string flouts convention. Since the terminating null character is the C convention, however, the compiler is smart enough to add the null character to the end of a string literal, and the system is smart enough to add the null character to the command-line arguments stored in the memory of a freshly created process. Ifargcis greater than 3, and the third argument you type on the command-line for your program is “stdout”, the call tostrcmp(argv[3], "stdout")will return 0 to mean that the two strings match.