57

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
5
  • 2
    Think for a minute: what would happen if they weren't NULL terminated? Commented Jun 13, 2012 at 17:29
  • 2
    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. Commented Jun 14, 2012 at 16:12
  • @LeviX - if the character arrays were not NULL terminated how could you detect the end of they character array? Commented Jun 14, 2012 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. Commented Jun 14, 2012 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. Commented Sep 27, 2017 at 21:00

3 Answers 3

97

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."

Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
@Snehasish: Just for fun, I asked this question on Retrocomputing S.E. -- it's a holdover from pre-ANSI C: retrocomputing.stackexchange.com/questions/5179/…
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.
Here's what I found about it: en.cppreference.com/w/c/language/main_function.html So, yeah, argv is actually argc+1 pointers long, and the last pointer is always NULL. I never knew that about C, and I thought I knew the language pretty well. This really simplifies programs like sudo or chrt, which call execvp on a modified argv.
3

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

Comments

1

Your question can be understood in two different ways:

1) Is each string in argv[] a NUL-terminated string?

As already written in the other answers, this must be the case because otherwise you could not find out the length of each string.

2) Is argv[argc] = NULL?

I found the answer on the Retrocomputing Stackexchange site:

When the C programming language was invented in the late 1970s, argv[argc] did not contain a useful value (it was not necessarily NULL).

However, all C compilers that comply with the ANSI C standard (from 1983) ensure that argv[argc] = NULL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.