1
#include <iostream>

using namespace std;

int main(){
    return 0;
}

Literals are things the computer can't change. So should the 0 from "return 0;" be a literal too?

Filburt
  • 16,951
  • 12
  • 63
  • 111
SiamRahman
  • 21
  • 1
  • 1
    Is there any reason why `0` wouldn't be a literal? This sounds like a [XY-problem](https://xyproblem.info). – churill Nov 02 '21 at 07:20
  • It IS a literal to start with. Sounds like you heard some version of confusing statement from here https://www.tutorialspoint.com/cplusplus/cpp_constants_literals.htm ; literals are literals, while constants are constants. – Swift - Friday Pie Nov 02 '21 at 07:21
  • @churil a number of primers start calling literals constants. – Swift - Friday Pie Nov 02 '21 at 07:23
  • Any concrete value directly given in program's text is a literal. This is not a strict definition, as C++ defines literals by describing the syntax (see chapter Literals in ISO 14882). – Swift - Friday Pie Nov 02 '21 at 07:31
  • Relevant: [Is 0 a decimal literal or an octal literal?](https://stackoverflow.com/q/6895522/580083) – Daniel Langr Nov 02 '21 at 07:35
  • 2
    @DanielLangr I had one co-worker who was writing "pretty" and he had aligned an initializer list for a matrix some signature-calculating procedure, by putting multiple integers into columns with added leading zeroes. I laughed hard when he came to me after being unable to track the error. – Swift - Friday Pie Nov 02 '21 at 07:49
  • 1
    @Swift-FridayPie: We've all done it! Funny how it carried over to Java too, except that in Java `0` is a decimal literal. – Bathsheba Nov 02 '21 at 07:52
  • @Bathsheba well, both languages were designed by users of UNIX-like OS and octal system was useful there for certain things. – Swift - Friday Pie Nov 02 '21 at 08:04
  • You could use `return EXIT_SUCCESS;` if you prefer ([link](https://en.cppreference.com/w/cpp/utility/program/EXIT_status)). You'd need `#include ` as well. – Eljay Nov 02 '21 at 12:53

1 Answers1

0

0 is an octal literal of type int with the algebraic value zero. (In C it would be referred to as a constant.)

You don't need the statement return 0; in main - it's implicitly added by the compiler.

In another context, when declaring a pure virtual function, the

= 0

is called a pure specifier. That notation is used for historic reasons. Using an indentifier with special meaning (pure, perhaps cf. final) would have been a more modern choice.

Bathsheba
  • 227,678
  • 33
  • 352
  • 470