On cppreference.com I found the following code example:
#include <iostream>
#include <string_view>
// Print a string surrounded by single quotes, its
// length and whether it is considered empty.
void check_string(std::string_view ref)
{
std::cout << std::boolalpha
<< "'" << ref << "' has " << ref.size()
<< " character(s); emptiness: " << ref.empty() << '\n';
}
int main(int argc, char **argv)
{
// An empty string
check_string("");
// Almost always not empty: argv[0]
if (argc > 0) // <--- is this necessary?
check_string(argv[0]);
}
Is the test argc > 0
necessary if I have a program that runs on Linux, macOS, Windows, Android or any other common operating system? As far as I know, under these environments the first argument is always the name of the program itself.
Is there an environment where argc
can be equal or even less than 0? More precisely, would argc
be 0
in a freestanding environment (i.e. without an operating system), for example?