William M. answered 12/21/19
STEM Tutor (Ph.D, Northwestern) | Algebra–Calc, Bio, Chem, Physics, CS
// in standard C, you used to write (void) if there were no args
// but most compilers follow the C99 (and the C11) standard
// and no longer require that
#include <stdio.h>
void foo(void) { // you can now say void foo()
printf("Function taking no arguments and returning void\n");
}
// in C++, you don't have to write (void) if there are no args
#include <iostream>
void foo() { // you can always say this in C++
std::cout << "Function taking no arguments and returning void\n";
}
// Note, as an aside, some textbooks (even Kernighan and Ritchie)
// showed calling main as
int main() {
// ...
}
// in most cases, you will want to process command line arguments,
// something like this
int main(int argc, const char* argv[]) {
// argc is the number of command line arguments
// argv[] contains pointers to the command line arguments
// first command line argument is the name of the program
if (argc != 2) { // std::cerr is the error stream
std::cerr << "Usage: ./program_name filename\n";
exit(1);
}
const std::string filename = argv[1];
// ... use the filename to process
// ... more code here
return 0;
}