Standard C — Hosted Environment
For a hosted environment (that’s the normal one), the C11 standard (ISO/IEC 9899:2011) says:
5.1.2.2.1 Program startup
The function called at program startup is named
main
. The implementation declares no
prototype for this function. It shall be defined with a return type ofint
and with no
parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be
used, as they are local to the function in which they are declared):int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.
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.- If the value of
argc
is greater than zero, the array membersargv[0]
through
argv[argc-1]
inclusive shall contain pointers to strings, which are given
implementation-defined values by the host environment prior to program startup. The
intent is to supply to the program information determined prior to program startup
from elsewhere in the hosted environment. If the host environment is not capable of
supplying strings with letters in both uppercase and lowercase, the implementation
shall ensure that the strings are received in lowercase.- If the value of
argc
is greater than zero, the string pointed to byargv[0]
represents the program name;argv[0][0]
shall be the null character if the
program name is not available from the host environment. If the value ofargc
is
greater than one, the strings pointed to byargv[1]
throughargv[argc-1]
represent the program parameters.- The parameters
argc
andargv
and the strings pointed to by theargv
array shall
be modifiable by the program, and retain their last-stored values between program
startup and program termination.10) Thus,
int
can be replaced by a typedef name defined asint
, or the type ofargv
can be written as
char **argv
, and so on.
Program termination in C99 or C11
The value returned from main()
is transmitted to the ‘environment’ in an implementation-defined way.
5.1.2.2.3 Program termination
1 If the return type of the
main
function is a type compatible withint
, a return from the
initial call to themain
function is equivalent to calling theexit
function with the value
returned by themain
function as its argument;11) reaching the}
that terminates the
main
function returns a value of 0. If the return type is not compatible withint
, the
termination status returned to the host environment is unspecified.11) In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in
main
will have ended in the former case, even where they would not have in the latter.
Note that 0
is mandated as ‘success’. You can use EXIT_FAILURE
and EXIT_SUCCESS
from <stdlib.h>
if you prefer, but 0 is well established, and so is 1. See also Exit codes greater than 255 — possible?.
In C89 (and hence in Microsoft C), there is no statement about what happens if the main()
function returns but does not specify a return value; it therefore leads to undefined behaviour.
7.22.4.4 The
exit
function¶5 Finally, control is returned to the host environment. If the value of
status
is zero orEXIT_SUCCESS
, an implementation-defined form of the status successful termination is returned. If the value ofstatus
isEXIT_FAILURE
, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
Standard C++ — Hosted Environment
The C++11 standard (ISO/IEC 14882:2011) says:
3.6.1 Main function [basic.start.main]
¶1 A program shall contain a global function called main, which is the designated start of the program. […]
¶2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementation defined.
All implementations
shall allow both of the following definitions of main:int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
In the latter form
argc
shall be the number of arguments passed to the program from the environment
in which the program is run. Ifargc
is nonzero these arguments shall be supplied inargv[0]
throughargv[argc-1]
as pointers to the initial characters of null-terminated multibyte strings (NTMBSs) (17.5.2.1.4.2) andargv[0]
shall be the pointer to the initial character of a NTMBS that represents the
name used to invoke the program or""
. The value ofargc
shall be non-negative. The value ofargv[argc]
shall be 0. [Note: It is recommended that any further (optional) parameters be added afterargv
. —end
note]¶3 The function
main
shall not be used within a program. The linkage (3.5) ofmain
is implementation-defined. […]¶5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic
storage duration) and callingstd::exit
with the return value as the argument. If control reaches the end
of main without encountering a return statement, the effect is that of executingreturn 0;
The C++ standard explicitly says «It [the main function] shall have a return type of type int
, but otherwise its type is implementation defined», and requires the same two signatures as the C standard to be supported as options. So a ‘void main()’ is directly not allowed by the C++ standard, though there’s nothing it can do to stop a non-standard implementation allowing alternatives. Note that C++ forbids the user from calling main
(but the C standard does not).
There’s a paragraph of §18.5 Start and termination in the C++11 standard that is identical to the paragraph from §7.22.4.4 The exit
function in the C11 standard (quoted above), apart from a footnote (which simply documents that EXIT_SUCCESS
and EXIT_FAILURE
are defined in <cstdlib>
).
Standard C — Common Extension
Classically, Unix systems support a third variant:
int main(int argc, char **argv, char **envp) { ... }
The third argument is a null-terminated list of pointers to strings, each of which is an environment variable which has a name, an equals sign, and a value (possibly empty). If you do not use this, you can still get at the environment via ‘extern char **environ;
‘. This global variable is unique among those in POSIX in that it does not have a header that declares it.
This is recognized by the C standard as a common extension, documented in Annex J:
###J.5.1 Environment arguments
¶1 In a hosted environment, the main function receives a third argument,
char *envp[]
,
that points to a null-terminated array of pointers tochar
, each of which points to a string
that provides information about the environment for this execution of the program (5.1.2.2.1).
Microsoft 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 OS) 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 Visual Studio 2005 version of this page does not list void main()
as an alternative. The versions from Microsoft Visual Studio 2008 onwards do.
Standard C — Freestanding Environment
As noted early on, the requirements above apply to hosted environments. If you are working with a freestanding environment (which is the alternative to a hosted environment), then the standard has much less to say. For a freestanding environment, the function called at program startup need not be called main
and there are no constraints on its return type. The standard says:
5.1.2 Execution environments
Two execution environments are defined: freestanding and hosted. In both cases,
program startup occurs when a designated C function is called by the execution
environment. All objects with static storage duration shall be initialized (set to their initial values) before program startup. The manner and timing of such initialization are otherwise unspecified. Program termination returns control to the execution environment.5.1.2.1 Freestanding environment
In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.
The effect of program termination in a freestanding environment is implementation-defined.
The cross-reference to clause 4 Conformance refers to this:
¶5 A strictly conforming program shall use only those features of the language and library specified in this International Standard.3) It shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior, and shall not exceed any minimum implementation limit.
¶6 The two forms of conforming implementation are hosted and freestanding. A conforming hosted implementation shall accept any strictly conforming program. A conforming freestanding implementation shall accept any strictly conforming program in which the use of the features specified in the library clause (clause 7) is confined to the contents of the standard headers
<float.h>
,<iso646.h>
,<limits.h>
,<stdalign.h>
,
<stdarg.h>
,<stdbool.h>
,<stddef.h>
,<stdint.h>
, and
<stdnoreturn.h>
. A conforming implementation may have extensions (including
additional library functions), provided they do not alter the behavior of any strictly conforming program.4)¶7 A conforming program is one that is acceptable to a conforming implementation.5)
3) A strictly conforming program can use conditional features (see 6.10.8.3) provided the use is guarded by an appropriate conditional inclusion preprocessing directive using the related macro. For example:
#ifdef __STDC_IEC_559__ /* FE_UPWARD defined */ /* ... */ fesetround(FE_UPWARD); /* ... */ #endif
4) This implies that a conforming implementation reserves no identifiers other than those explicitly reserved in this International Standard.
5) Strictly conforming programs are intended to be maximally portable among conforming implementations. Conforming programs may depend upon non-portable features of a conforming implementation.
It is noticeable that the only header required of a freestanding environment that actually defines any functions is <stdarg.h>
(and even those may be — and often are — just macros).
Standard C++ — Freestanding Environment
Just as the C standard recognizes both hosted and freestanding environment, so too does the C++ standard. (Quotes from ISO/IEC 14882:2011.)
1.4 Implementation compliance [intro.compliance]
¶7 Two kinds of implementations are defined: a hosted implementation and a freestanding implementation. For a hosted implementation, this International Standard defines the set of available libraries. A freestanding
implementation is one in which execution may take place without the benefit of an operating system, and has an implementation-defined set of libraries that includes certain language-support libraries (17.6.1.3).¶8 A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that
use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.¶9 Each implementation shall include documentation that identifies all conditionally-supported constructs that it does not support and defines all locale-specific characteristics.3
3) This documentation also defines implementation-defined behavior; see 1.9.
17.6.1.3 Freestanding implementations [compliance]
Two kinds of implementations are defined: hosted and freestanding (1.4). For a hosted implementation, this International Standard describes the set of available headers.
A freestanding implementation has an implementation-defined set of headers. This set shall include at least the headers shown in Table 16.
The supplied version of the header
<cstdlib>
shall declare at least the functionsabort
,atexit
,at_quick_exit
,exit
, andquick_exit
(18.5). The other headers listed in this table shall meet the same requirements as for a hosted implementation.
Table 16 — C++ headers for freestanding implementations
Subclause | Header(s) |
---|---|
<ciso646> |
|
18.2 Types | <cstddef> |
18.3 Implementation properties | <cfloat> <limits> <climits> |
18.4 Integer types | <cstdint> |
18.5 Start and termination | <cstdlib> |
18.6 Dynamic memory management | <new> |
18.7 Type identification | <typeinfo> |
18.8 Exception handling | <exception> |
18.9 Initializer lists | <initializer_list> |
18.10 Other runtime support | <cstdalign> <cstdarg> <cstdbool> |
20.9 Type traits | <type_traits> |
29 Atomics | <atomic> |
What about using int main()
in C?
The standard §5.1.2.2.1 of the C11 standard shows the preferred notation — int main(void)
— but there are also two examples in the standard which show int main()
: §6.5.3.4 ¶8 and §6.7.6.3 ¶20. Now, it is important to note that examples are not ‘normative’; they are only illustrative. If there are bugs in the examples, they do not directly affect the main text of the standard. That said, they are strongly indicative of expected behaviour, so if the standard includes int main()
in an example, it suggests that int main()
is not forbidden, even if it is not the preferred notation.
6.5.3.4 The
sizeof
and_Alignof
operators…
¶8 EXAMPLE 3 In this example, the size of a variable length array is computed and returned from a function:
#include <stddef.h> size_t fsize3(int n) { char b[n+3]; // variable length array return sizeof b; // execution time sizeof } int main() { size_t size; size = fsize3(10); // fsize3 returns 13 return 0; }
A function definition like int main(){ … }
does specify that the function takes no arguments, but does not provide a function prototype, AFAICT. For main()
that is seldom a problem; but it does mean that if you have recursive calls to main()
, the arguments won’t be checked. For other functions, it is more of a problem — you really need a prototype in scope when the function is called to ensure that the arguments are correct.
You don’t normally call main()
recursively, outside of places like IOCCC — and you are explicitly forbidden from doing so in C++. I do have a test program that does it — mainly for novelty. If you have:
int i = 0;
int main()
{
if (i++ < 10)
main(i, i * i);
return 0;
}
and compile with GCC and don’t include -Wstrict-prototypes
, it compiles cleanly under stringent warnings. If it’s main(void)
, it fails to compile because the function definition says «no arguments».
C
According to coding standards, a good return program must exit the main function with 0. Although we are using void main()
in C, In which we have not suppose to write any kind of return statement but that doesn’t mean that C code doesn’t require 0 as exit code. Let’s see one example to clear our thinking about need of return 0 statement in our code.
Example #1 :
#include <stdio.h>
void
main()
{
printf
(
"It works fine"
);
}
Output:
It works fine
Runtime Error:
NZEC
As we can see in the output the compiler throws a runtime error NZEC, Which means that Non Zero Exit Code. That means that our main program exited with non zero exiting code so if we want to be a developer than we make these small things in our mind.
Correct Code for C :
#include <stdio.h>
int
main()
{
printf
(
"This is correct output"
);
return
0;
}
Output:
This is correct output
Note: Returning value other than zero will throw the same runtime error. So make sure our code return only 0.
Example #2 :
#include <stdio.h>
int
main()
{
printf
(
"GeeksforGeeks"
);
return
"gfg"
;
}
Output:
It works fine
Runtime Error:
NZEC
Correct Code for C :
#include <stdio.h>
int
main()
{
printf
(
"GeeksforGeeks"
);
return
0;
}
C++
In case of C++, We are not able to use void keyword with our main()
function according to coding namespace standards that’s why we only intend to use int keyword only with main function in C++. Let’s see some examples to justify these statements.
Example #3 :
#include <iostream>
using
namespace
std;
void
main()
{
cout <<
"GeeksforGeeks"
;
}
Compile Errors:
prog.cpp:4:11: error: '::main' must return 'int' void main() ^
Correct Code for C++ :
#include <iostream>
using
namespace
std;
int
main()
{
cout <<
"GeeksforGeeks"
;
return
0;
}
Example #4 :
#include <iostream>
using
namespace
std;
char
main()
{
cout <<
"GeeksforGeeks"
;
return
"gfg"
;
}
Compile Errors:
prog.cpp:4:11: error: '::main' must return 'int' char main() ^ prog.cpp: In function 'int main()': prog.cpp:7:9: error: invalid conversion from 'const char*' to 'int' [-fpermissive] return "gfg"; ^
Correct Code for C++ :
#include <iostream>
using
namespace
std;
int
main()
{
cout <<
"GeeksforGeeks"
;
return
0;
}
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out — check it out now!
Last Updated :
19 Sep, 2019
Like Article
Save Article
Terminates the current function and returns the specified value (if any) to the caller.
Contents
- 1 Syntax
- 2 Explanation
- 3 Notes
- 3.1 Automatic move from local variables and parameters
- 3.2 Guaranteed copy elision
- 4 Keywords
- 5 Example
- 6 Defect reports
- 7 See also
[edit] Syntax
attr (optional) return expression (optional) ;
|
(1) | |
attr (optional) return braced-init-list ;
|
(2) | (since C++11) |
attr (optional) co_return expression (optional) ;
|
(3) | (since C++20) |
attr (optional) co_return braced-init-list ;
|
(4) | (since C++20) |
attr | — | (since C++11) sequence of any number of attributes |
expression | — | expression, convertible to the function return type |
braced-init-list | — | brace-enclosed list of initializers and other braced-init-lists |
[edit] Explanation
1) Evaluates the expression, terminates the current function and returns the result of the expression to the caller, after implicit conversion to the function return type. The expression is optional in functions whose return type is (possibly cv-qualified) void, and disallowed in constructors and in destructors.
3,4) In a coroutine, the keyword co_return must be used instead of return for the final suspension point (see coroutines for details).
There is a sequence point between the copy-initialization of the result of the function call and the destruction of all temporaries at the end of expression. |
(until C++11) |
The copy-initialization of the result of the function call is sequenced-before the destruction of all temporaries at the end of expression, which, in turn, is sequenced-before the destruction of local variables of the block enclosing the return statement. |
(since C++11) |
[edit] Notes
If control reaches the end of
- a function with the return type (possibly cv-qualified) void,
- a constructor,
- a destructor, or
- a function-try-block for a function with the return type (possibly cv-qualified) void
without encountering a return statement, return; is executed.
If control reaches the end of the main function, return 0; is executed.
Flowing off the end of a value-returning function, except main and specific coroutines(since C++20), without a return statement is undefined behavior.
In a function returning (possibly cv-qualified) void, the return statement with expression can be used, if the expression type is (possibly cv-qualified) void.
If the return type of a function is specified as a placeholder type, it will be deduced from the return value. |
(since C++14) |
Returning by value may involve construction and copy/move of a temporary object, unless copy elision is used. Specifically, the conditions for copy/move are as follows:
Automatic move from local variables and parametersThe expression is move-eligible if it is a (possibly parenthesized) id-expression that names a variable of automatic storage duration whose type is
and that variable is declared
|
(since C++11) |
Guaranteed copy elisionIf expression is a prvalue, the result object is initialized directly by that expression. This does not involve a copy or move constructor when the types match (see copy elision). |
(since C++17) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_implicit_move |
202207L | (C++23) | Simpler implicit move |
[edit] Keywords
return,
co_return
[edit] Example
#include <iostream> #include <string> #include <utility> void fa(int i) { if (i == 2) return; std::cout << "fa("<< i << ")\n"; } // implied return; int fb(int i) { if (i > 4) return 4; std::cout << "fb(" << i << ")\n"; return 2; } std::pair<std::string, int> fc(const char* p, int x) { return {p, x}; } void fd() { return fa(10); // fa(10) is a void expression } int main() { fa(1); // prints its argument, then returns fa(2); // does nothing when i == 2, just returns int i = fb(5); // returns 4 i = fb(i); // prints its argument, returns 2 std::cout << "i = " << i << '\n' << "fc(~).second = " << fc("Hello", 7).second << '\n'; fd(); }
Output:
fa(1) fb(4) i = 2 fc(~).second = 7 fa(10)
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 1541 | C++98 | expression could not be omitted if the return type is cv-qualified void | it can be omitted |
CWG 1579 | C++11 | return by converting move constructor was not allowed | converting move constructor lookup enabled |
CWG 1885 | C++98 | sequencing of the destruction of automatic variables was not explicit | sequencing rules added |
[edit] See also
- copy elision
Все программы имеют жизненный цикл. Когда приложение завершает работу, оно должно сообщить об этом системе. Для передачи соответствующей информации используются специальные операторы – операторы возврата.
Далее предстоит разобраться с принципами работы функции return в C++. Эта информация пригодится преимущественно начинающим разработчикам.
Описание функции возврата
Все пользовательские функции после завершения выполнения инструкций способны возвращать значения туда, откуда они были вызваны. Пример – int main. Она в конце работы всегда возвращает значение в операционную систему. Связано это с тем, что ОС изначально вызывала main для работы программного обеспечения.
В C++ и C имеются операторы возврата. Они называются функциями перехода. Используются для возврата значений или остановки выполнения операции. Называется оператор перехода return (ретурном).
При обработке return происходит следующее:
- Выполнение имеющейся функции прекращается.
- Управление переходит обратно в точку вызова обработанной операции.
- Инструкции, написанные после return, никогда не выполняются.
- Если внутри функции отсутствует соответствующий оператор, при достижении закрывающейся фигурной скобки управление передается в точку вызова обработанной операции. Возвращаемое значение не определяется.
Операторы return имеются несколько сценариев использования. Всего их два. Далее они будут рассмотрены более подробно.
Использование внутри основной функции
Это – первый метод применения returning. Он делает следующее:
- Return останавливает исполнение программы. Происходит присваивание значения 0 (zero) или 1 (one). Они указываются на статус выполнения.
- Соответствующие коды использовались в C в виде соглашения. Связано это с отсутствием поддержки объектов, классов и исключений непосредственно в языке.
- Return 0 возвращает функции 0. Данная запись указывает на успешное выполнение заданных операций. Функция обработана так, как было задумано изначально.
- Return 1 возвращает 1. Указывает на наличие ошибки или сбоя при выполнении приложения. Данная запись свидетельствует о том, что функция не работает по задуманному предназначению.
Что делает returned 0, понятно. Стоит обратить внимание на то, что операционные системы могут самостоятельно завершать приложения с определенными кодами статуса выхода. Это возможно тогда, когда выполняется некоторый спектр недопустимых операций. А еще в зависимости от операционной системы на устройстве пользователя могут меняться соглашения о кодах возврата. Это достаточно редкая ситуация.
Внутри пользовательских функций
Второй вариант использования returns в программном коде – это внутри пользовательских функций. В C++ данный подход широко используется на практике. Соответствующий язык программирования рассматривает логическое значение в качестве отдельного типа данных с двумя «исходами» — истина (true) и ложь (false).
Значения 0 и 1 являются целочисленными (тип int). Они не могут быть неявно преобразованы в логические. Это приводит к тому, что:
- return 0 делает возврат false из заданной инструкции;
- return 1 возвращает истину.
Необходимо помнить, что не всегда returns применяются с 0 и 1. Далее предстоит рассмотреть наглядный пример с отличающимся возвращаемым параметром.
Принцип работы – пример
Чтобы лучше разбираться в операторах перехода в C++, рекомендуется рассмотреть принцип их работы на наглядном примере. Необходимо написать приложение, которое будет запрашивать у клиента число, а затем выводить его квадрат. Для уточнения «цифры» используется отдельная инструкция. Ее предстоит создать самостоятельно.
Делается это так:
- Сначала требуется создать функцию, которая запрашивает число на вывод с консоли:.
- Во второй строке создается функция типа int под названием EnterNumer.
- Строки 3-6 используются для описания инструкций, выполняемых после вызова функции. Сначала клиенту на экране предлагается ввод целого числа. Далее программа создает переменную для него и получит заданный параметр.
- Описанная функция вызывается из главной (main): .
- Функция, запрашивающая и получающая число, вызвана. По завершению работы она должна вернуть в main число, которое было указано пользователем. Эта операция прописана в 7 строке кода. После его обработки произойдет возврат переменной int. Поэтому инструкция выглядит как return input: .
- Чтобы сохранить значения, необходимо воспользоваться переменной. Она инициализируется функцией EnterNumber(). Это происходит в строке 11: .
- Соответствующая инструкция указывает на то, что в памяти резервируется место для переменной с именем num типа int. Далее происходит инициализация ее значением, возвращенным из функции EnterNumber. Предварительно запускается выполнение предусматриваемых инструкций.
- Необходимо завершить приложение выводом на дисплей квадрата числа, полученного из функции EnterNumber(). Для этого используется std::cout:.
При обработке соответствующего кода на экране появится такой результат:
Мы сделали программу, которая использует return. Он используется для возврата значений. Но соответствующий оператор может ничего не возвращать.
Тип void
Тип функции void в C++ — это ничего или пустота. Он указывает компилятору на то, что соответствующая команда не возвращает никаких параметров. Если в ее конце устанавливается оператор return, программа просто не пройдет стадию компиляции.
Данная операция вызывается так же, как и команды, возвращающие параметры. Она просто выполняет все необходимые инструкции, написанные в теле, после чего завершается. Вот наглядный пример:
Написанное приложение печатает две строчки текста, хранимых в двух «алгоритмах» типа void. Они ничего не вернут – только выполняют заданные инструкции. После этого происходит после обращения к ним main().
Как быстрее разобраться с темой
Операторы перехода в C++ – это не так трудно, как кажется. Чтобы надписи типа return, include, main и другие не вызывали вопросов, рекомендуется пройти дистанционные компьютерные курсы. На них с нуля пользователей научат разрабатывать приложения на разных языках – от Python до C++.
Пример – от образовательного центра OTUS. Пользователя в срок до 12 месяцев научат основам разработки. Люди смогут освоить любую IT-профессию. В процессе обучения гарантируется постоянное кураторство, много практики, а также интересные домашние задания. Ученику помогут составить портфолио. В конце курса выдается электронный сертификат, который подтверждает приобретенный спектр навыков и знаний.
Хотите освоить современную IT-специальность? Огромный выбор курсов по востребованным IT-направлениям есть в Otus! Ниже – один из них:
В cpp как известно прога начинается в мейне, и если я указываю ему тип int, то обязательно ли в конце писать return 0? Ну я почему спрашиваю. Сижу книгу по плюсам читаю, здесь в каждом примере в конце обязательно return 0. Ну я естественно пробовал без него, все работает. Но возможно я чего-то не знаю. Поэтому хотел бы спросить у знающих людей, важен ли return в конце мейна? Ну или тупо можно void указывать и забить? Как более корректно?
-
Вопрос задан
-
378 просмотров
Формально, единственная функция, в которой можно не делать return — это именно main.
В остальных случаях, если функция что-то возвращает, наличие return обязательно — иначе будет UB.
Почему для main исключение — никто не знает, потому что параграф в стандарте языка, на который ссылается другой параграф — потерялся:
I looked it up. I believe this was introduced with C99, and apparently the C99 rationale is defect. It has comments for 5.1.2.2.1 Program startup, then labels the next chapter 5.1.2.3 Program execution. It should have been 5.1.2.2. As a consequence of this, the rationale for Program termination that should have been in the real chapter 5.1.2.3, has gone missing in action. Thus, main allows no return code in C99 and there exists no rational reason why.
Источник
Пригласить эксперта
The C programming language allows programs exiting or returning from the main function to signal success or failure by returning an integer, or returning the macros EXIT_SUCCESS and EXIT_FAILURE. On Unix-like systems these are equal to 0 and 1 respectively.[3] A C program may also use the exit() function specifying the integer status or exit macro as the first parameter.
The return value from main is passed to the exit function, which for values zero, EXIT_SUCCESS or EXIT_FAILURE may translate it to “an implementation defined form” of successful termination or unsuccessful termination.
Apart from zero and the macros EXIT_SUCCESS and EXIT_FAILURE, the C standard does not define the meaning of return codes. Rules for the use of return codes vary on different platforms (see the platform-specific sections).
Exit status
Если писать код, соответствующий стандарту языка, то корректно, конечно, указывать return.
В большинстве современных ОС любой запущенный процесс возвращает код возврата. Это требование ОС. Кодом возврата процесса обычно является значение возвращаемое из main.
Но, если код возврата не нужен, то можно не указывать return и объявлять void main()
. Тогда считается, что код возврата из программы нулевой. Обычно это нормально отрабатывает. Но это не стандартная фича и ваша программа может не собраться на какой-то платформе и/или компиляторе.
-
Показать ещё
Загружается…
30 нояб. 2023, в 10:04
3000 руб./за проект
30 нояб. 2023, в 08:47
400 руб./в час
30 нояб. 2023, в 08:42
2000 руб./за проект
Минуточку внимания
FREE BOOK -> Problems for the day before your Coding Interview (on Amazon)
Price is set to 0. Click on Buy Now and it will be downloaded in your account.
The return value of main() function shows how the program exited. The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value.
Table of contents:
- main() in C / C++
- The exit() function
Here is the syntax of main()
function in C/C++ language,
int main() {
// Your program
return 0;
}
The valid C/C++ main signatures are:
int main()
int main(int argc, char* argv[])
int main(int argc, char** argv)
In C/C++ language, the main()
function can be left without return value. By default, it will return zero.
It is prohibited to write void main()
by the C++ standard which when written result in this compitation error:
prog.cpp:4:11: error: '::main' must return 'int'
void main()
^
Some compilers allow the usage of void main()
. It ‘works’ because the compiler does its best to generate code for programs. Compilers such as GCC will warn about non-standard forms for main()
, but will process them. The linker isn’t too worried about the return type; it simply needs a symbol main
and when it finds it, links it into the executable. The start-up code assumes that main has been defined in the standard manner. If main() returns to the startup code, it collects the returned value as if the function returned an int, but that value is likely to be garbage. So, it sort of seems to work as long as you don’t look for the exit status of your program.
So what happend if main is defined in the following manners?
auto main() -> int {}
auto main() { return 0; }
auto main() {}
In (1) It is legal and the program will execute successfully.
In (2) and (3), the return type of the main function cannot be deduced since the standard documentation will be reworded as:
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a declared return type of type int, but otherwise its type is implementation-defined.
The exit() function
This function terminates the calling process immediately.
The declaration of this function is void exit(int status)
where status is the status value returned to the parent process.
Example:
#include <stdio.h>
#include <stdlib.h>
int main () {
printf("Start of the program....\n");
printf("Exiting the program....\n");
exit(0);
printf("End of the program....\n");
return(0);
}
The result of this program will be:
Start of the program....
Exiting the program....
Then what is the difference between exit()
and return
?
return
returns from the current function; it’s a language keyword likefor
orbreak
.exit()
terminates the whole program, wherever you call it from.
Here is an example that could demonstrate this point:
Example with return
:
#include <stdio.h>
void f(){
printf("Executing f\n");
return;
}
int main(){
f();
printf("Back from f\n");
}
The output will be:
Executing f
Back from f
Same Example with exit()
:
#include <stdio.h>
#include <stdlib.h>
void f(){
printf("Executing f\n");
exit(0);
}
int main(){
f();
printf("Back from f\n");
}
The output will be:
Executing f
Back from f
With this article at OpenGenus, you must have a strong idea of Return value of main() in C / C++.