Why is there an argc in C main?

Asked 2 years ago, Updated 2 years ago, 144 views

We know that argc is the number of factors that came in, and argv is storing them as strings.

But since argv[argc]==NULL, there is no need to have argc Is it because it might not be argv[argc]==NULL in other compilers?

c++ c main

2022-09-21 19:49

1 Answers

C11 Standard 5.1.2.2.1 shows

If they are declared, the parameters to the main function shall obey the following constraints:

The value of argc shall be nonnegative. argv[argc] shall be a null pointer.

--

C++ n3337 draft 3.6.1 also

2 ...argc shall be the number of arguments passed to the program from the environment in which the program is run. .... The value of argc shall be non-negative. The value of argv[argc] shall be 0.

Because of this, in C/C++, it is always argv[argc]==NULL. As you said, argc is not a necessary function.

But thanks to argc, I don't have to go around the loop Isn't it a useful function in that you can check how many factors came in right away?


2022-09-21 19:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.