52

So I'm wondering if command line parameters are always null terminated? Google seems to say yes, and compiling on GCC indicates this is the case, but can I guarantee this to always be true?

int main(int argc, char** argv)
{
    char *p;

    for(int cnt=1; cnt < argc; ++cnt)
    {
        p = argv[cnt];
        printf("%d = [%s]\n", cnt, p);
    }
    return 0;
}

$ MyProgram -arg1 -arg2 -arg3
1 = -arg1
2 = -arg2
3 = -arg3
X. Liu
  • 1,070
  • 11
  • 30
LeviX
  • 3,096
  • 3
  • 28
  • 41
  • 2
    Think for a minute: *what would happen if they weren't NULL terminated?* – user703016 Jun 13 '12 at 17:29
  • 1
    Thinking for a minute... char[] and char* does not automatically imply a C-style string. Obviously the entire command line string is NULL terminated but that does not have to translate to each each individual array of characters passed on the command line being NULL terminated. It would be easy enough for argv to just strip out the whitespace on the command line and and have non null terminated character arrays and not C style strings. – LeviX Jun 14 '12 at 16:12
  • @LeviX - if the character arrays were not NULL terminated how could you detect the end of they character array? – shf301 Jun 14 '12 at 16:20
  • @shf301 Ahh, ok I see your point. char* argv[]. If they aren't null terminated how do you know where one starts and the other begins in the 2d array. Got it, I stand corrected, this makes sense now. Thanks. – LeviX Jun 14 '12 at 17:37
  • @LeviX It's about knowing where each string ends & being able to pass it to every other C func that expects a NUL terminator. It's got nothing to do with the strings being in an array. An array like `char* argv[]` is not a 2D array; it's an array of pointers. (Surely in a real 2D array, each string would have to be the same length, so we wouldn't need a terminator?) The pointed-to objects do not have to be adjacent to each other. That is: here, it is not required that the address pointed to by each successive pointer must be 1 byte after the NUL terminating the previously numbered argument. – underscore_d Sep 27 '17 at 21:00

2 Answers2

87

Yes. The non-null pointers in the argv array point to C strings, which are by definition null terminated.

The C Language Standard simply states that the array members "shall contain pointers to strings" (C99 §5.1.2.2.1/2). A string is "a contiguous sequence of characters terminated by and including the first null character" (C99 §7.1.1/1), that is, they are null terminated by definition.

Further, the array element at argv[argc] is a null pointer, so the array itself is also, in a sense, "null terminated."

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Can you explain the use of argc if argv is null terminated. Can't we detect no. of arguments by traversing the array until '\0' is reached.. ? – Snehasish Aug 30 '15 at 19:53
  • 1
    @Snehasish Yes, you can compute `argc` from `argv`. As for why `main` has the signature it has, I do not know for sure. It's been this way since before I existed. – James McNellis Aug 31 '15 at 01:30
  • 2
    @Snehasish: Just for fun, I asked this question on Retrocomputing S.E. -- it's a holdover from pre-ANSI C: https://retrocomputing.stackexchange.com/questions/5179/why-historically-include-the-number-of-arguments-argc-as-a-parameter-of-main – tonysdg Dec 14 '17 at 16:18
  • 1
    Only the last element of the array is a null pointer, but you can have `argc == 0` (though it is extremely rare and requires careful setup) so that there are no strings in the argument list. If the command name is not available but there are other arguments, the standard stipulates that `argv[0]` shall be an empty string, not a null pointer. – Jonathan Leffler Jul 11 '19 at 22:25
4

Yes, it is always true that the arguments are null terminated strings.

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106