C main에 argc는 왜 있는 건가요?
조회수 2208회
발생하는 문제 및 실행환경
argc는 들어온 인자의 갯수를, argv가 그 인자를 스트링으로 저장하고 있는건 압니다.
근데 argv[argc]==NULL
이니까 굳이 argc
가 있을 필요는 없지 않나요?
다른 컴파일러에서는 argv[argc]==NULL
가 아닐 수도 있어서 그런걸까요?
1 답변
-
C11 표준 5.1.2.2.1에 보면
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에서도
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.
이기 때문에 C/C++에서는 항상
argv[argc]==NULL
입니다. 때문에 말씀하신 것처럼 argc는 필수로 있어야 하는 기능은 아닙니다.그래도 argc가 있는 덕분에 loop을 돌지 않고도 바로바로 몇 개 인자가 들어왔는지 확인할 수 있다는 점에서는 쓸모 있는 기능이 아닌가요?
댓글 입력