C의 main () 함수에 대한 유효한 서명은 무엇입니까?
C의 주 기능에 유효한 서명은 실제로 무엇입니까? 알아:
int main(int argc, char *argv[])
다른 유효한 것들이 있습니까?
이 답변 (C11) 당시의 현재 표준은 다음 두 가지를 명시 적으로 언급합니다.
int main(void);
int main(int argc, char* argv[]);
다음 각주와 함께 "또는 동등한"문구를 언급하지만 :
따라서은
int
로typedef
정의 된 이름 으로 바꾸int
거나 유형을argv
로 쓸 수 있습니다char ** argv
.
또한 더 많은 (구현 정의 된) 가능성을 제공합니다.
관련 섹션 (C11 섹션 5.1.2.2.1,이 특정 측면은 C99에서 변경되지 않음)은 다음과 같이 설명합니다.
프로그램 시작시 호출되는 함수의 이름은
main
. 구현은이 함수에 대한 프로토 타입을 선언하지 않습니다. 반환 유형int
과 매개 변수없이 정의되어야합니다 .int main(void) { /* ... */ }
또는 두 개의 매개 변수를 사용합니다 (여기에서
argc
및 로 지칭됩니다.argv
이름이 선언 된 함수에 로컬이기 때문에 모든 이름을 사용할 수 있음).int main(int argc, char *argv[]) { /* ... */ }
또는 동등한 것; 또는 다른 구현 정의 방식으로.
선언 된 경우
main
함수에 대한 매개 변수 는 다음 제약 조건을 준수해야합니다.
의 값은
argc
음수가 아니어야합니다.
argv[argc]
널 포인터 여야합니다.의 값
argc
이 0보다 크면 inclusive를argv[0]
통한 배열 구성원argv[argc-1]
은 프로그램 시작 전에 호스트 환경에서 구현 정의 값을 제공하는 문자열에 대한 포인터를 포함해야합니다. 그 목적은 호스팅 된 환경의 다른 곳에서 프로그램을 시작하기 전에 결정된 정보를 프로그램에 제공하는 것입니다. 호스트 환경이 대문자와 소문자가 모두 포함 된 문자열을 제공 할 수없는 경우 구현시 문자열이 소문자로 수신되는지 확인해야합니다.의 값
argc
이 0보다 크면로 가리키는 문자열argv[0]
은 프로그램 이름 을 나타냅니다.argv[0][0]
호스트 환경에서 프로그램 이름을 사용할 수없는 경우 널 문자가됩니다. 의 값argc
이 1보다 크면argv[1]
through가 가리키는 문자열argv[argc-1]
은 프로그램 매개 변수 를 나타냅니다.배열이 가리키는 매개 변수
argc
및argv
및 문자열은argv
프로그램에 의해 수정 가능해야하며 프로그램 시작과 프로그램 종료 사이에 마지막으로 저장된 값을 유지해야합니다.
이것은 일반적으로 C 프로그램에서 볼 수있는 호스트 된 환경을위한 것입니다. 독립형 환경 (예 : 임베디드 시스템)은 동일한 표준의 5.1.2.1에 명시된 것처럼 훨씬 덜 제한적입니다.
독립 환경 (운영 체제의 이점없이 C 프로그램 실행이 발생할 수 있음)에서는 프로그램 시작시 호출되는 함수의 이름과 유형이 구현에 따라 정의됩니다. 4 절에서 요구하는 최소 세트를 제외하고 독립 프로그램에 사용할 수있는 모든 라이브러리 기능은 구현에서 정의됩니다.
표준 C
호스팅 된 환경 (일반적인 환경)의 경우 C99 표준은 다음과 같이 말합니다.
5.1.2.2.1 프로그램 시작
프로그램 시작시 호출되는 함수의 이름은
main
. 구현은이 함수에 대한 프로토 타입을 선언하지 않습니다. 반환 유형int
과 매개 변수없이 정의되어야합니다 .int main(void) { /* ... */ }
또는 두 개의 매개 변수를 사용합니다 (여기에서
argc
및 로 지칭됩니다.argv
이름이 선언 된 함수에 로컬이기 때문에 모든 이름을 사용할 수 있음).int main(int argc, char *argv[]) { /* ... */ }
또는 동등한 것; 9) 또는 다른 구현 정의 방식으로.
9) 따라서으로
int
정의 된 typedef 이름으로 대체int
하거나의 유형을argv
로 쓸 수 있습니다char **argv
.
C11 및 C18 표준은 기본적으로 C99 표준과 동일합니다.
표준 C ++
C ++ 98 표준은 다음과 같이 말합니다.
3.6.1 주요 기능 [basic.start.main]
1 프로그램은 프로그램의 지정된 시작 인 main이라는 전역 기능을 포함해야합니다. [...]
2 구현시 주요 기능을 미리 정의해서는 안됩니다. 이 기능은 과부하되지 않아야합니다. 반환 유형은 int이지만 그렇지 않으면 해당 유형이 구현 정의됩니다. 모든 구현은 main에 대한 다음 정의를 모두 허용해야합니다.
int main() { /* ... */ }
과
int main(int argc, char* argv[]) { /* ... */ }
C ++ 표준은 "[주 함수]가 int 유형의 반환 유형을 가져야하지만 그렇지 않으면 해당 유형이 구현으로 정의됩니다"라고 명시 적으로 명시하고 C 표준과 동일한 두 개의 서명이 필요합니다. 따라서 'void main ()'은 C ++ 표준에서 직접 허용되지 않지만 비표준 준수 구현이 대안을 허용하지 못하도록 막을 수있는 방법은 없습니다 (표준 준수 구현이 대안을 표준에 대한 확장으로 허용하지 못하도록 함).
C ++ 03, C ++ 11, C ++ 14 및 C ++ 17 표준은 기본적으로 C ++ 98과 동일합니다.
공통 확장
고전적으로 Unix 시스템은 세 번째 변형을 지원합니다.
int main(int argc, char **argv, char **envp) { ... }
세 번째 인수는 문자열에 대한 포인터의 널로 끝나는 목록이며, 각각은 이름, 등호 및 값 (비어있을 수 있음)이있는 환경 변수입니다. 이것을 사용하지 않아도 ' extern char **environ;
' 를 통해 환경에 들어갈 수 있습니다 . 오랫동안이를 선언 한 헤더가 없었지만 POSIX 2008 표준은 이제 <unistd.h>
.
이것은 부록 J에 문서화 된 공통 확장으로 C 표준에 의해 인식됩니다.
J.5.1 환경 인수
¶1 호스트 된 환경에서, main 함수는 세 번째 인자를받습니다.
char *envp[]
이것은 널로 끝나는 포인터 배열char
을 가리키며, 각각은이 프로그램 실행을위한 환경에 대한 정보를 제공하는 문자열을 가리 킵니다. 2.2.1).
마이크로 소프트 C
The Microsoft VS 2010 compiler is interesting. The web site says:
The declaration syntax for main is
int main();
or, optionally,
int main(int argc, char *argv[], char *envp[]);
Alternatively, the
main
andwmain
functions can be declared as returningvoid
(no return value). If you declaremain
orwmain
as returning void, you cannot return an exit code to the parent process or operating system by using a return statement. To return an exit code whenmain
orwmain
is declared asvoid
, you must use theexit
function.
It is not clear to me what happens (what exit code is returned to the parent or o/s) when a program with void main()
does exit — and the MS web site is silent too.
Interestingly, MS does not prescribe the two-argument version of main()
that the C and C++ standards require. It only prescribes a three argument form where the third argument is char **envp
, a pointer to a list of environment variables.
The Microsoft page also lists some other alternatives — wmain()
which takes wide character strings, and some more.
The Microsoft VS 2005 version of this page does not list void main()
as an alternative. The versions from Microsoft VS 2008 onwards do.
Is int main()
the same as int main(void)
?
For a detailed analysis, see the end of my answer to What should main()
return in C and C++. (It seems that I once considered that this question referred to C++, even though it doesn't and never did. In C++, there is no difference between int main()
and int main(void)
and int main()
is idiomatic C++.)
In C, there is a difference between the two notations, but you only notice it in esoteric cases. Specifically, there's a difference if you call the main()
function from your own code, which you'e allowed to do in C and are not allowed to do in C++.
The int main()
notation does not provide a prototype for main()
, but that only matters if you call it recursively. With int main()
, you might later (in the same function, or in another function) write int rc = main("absolute", "twaddle", 2):
and formally the compiler shouldn't complain to the extent of refusing to compile the code, though it might legitimately complain (warn you) about it (and using -Werror
with GCC would convert the warning into an error). If you use int main(void)
, the subsequent call to main()
should generate an error — you said the function takes no arguments but tried to provide three. Of course, you can't legitimately call main()
before you've declared or defined it (unless you are still using C90 semantics) — and the implementation does not declare a prototype for main()
. NB: The C11 standard illustrates both int main()
and int main(void)
in different examples — both are valid in C, even though there's the subtle difference between them.
POSIX supports execve()
, which in turn supports
int main(int argc, char *argv[], char *envp[])
The added argument is the environment, i.e. an array of strings of the form NAME=VALUE.
http://en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B
Besides the usual int main(int argc, char *argv[])
and the POSIX int main(int argc, char **argv, char **envp)
, on Mac OS X also supports
int main(int argc, char* argv[], char* envp[], char* apple[]);
Of course it's Mac-only.
On Windows there's
int wmain(int argc, wchar_t* argv[], wchar_t* envp[]);
as the Unicode (actually, wide-character) variant. Of course there is WinMain
too.
int main(void)
Under some OS (for example, Windows) also such is valid:
int main(int argc, char **argv, char **envp)
where envp
gives an environment, otherwise accessible through getenv()
ReferenceURL : https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function
'developer tip' 카테고리의 다른 글
RequireJS에서 동적 요구 사항, "컨텍스트에 대한 모듈 이름이 아직로드되지 않았습니다"오류가 발생합니까? (0) | 2020.12.31 |
---|---|
abs (double)에 대한 모호한 오버로드 호출 (0) | 2020.12.30 |
C ++에서 클래스 상속 방지 (0) | 2020.12.30 |
JetBrains IntelliJ IDEA에서 Github를 사용하려면 Git이 필요합니까? (0) | 2020.12.30 |
정적 라이브러리가있는 경우 32 비트 또는 64 비트 용인지 알 수있는 간단한 방법이 있습니까? (0) | 2020.12.30 |