Author: Dann Corbit
Date: 19:02:25 06/11/04
Go up one level in this thread
On June 11, 2004 at 21:44:35, Dann Corbit wrote:
[snip]
I should mention that this is a difference between C and C++:
>3 The function main shall not be used (3.2) within a program. The linkage (3.5)
>of main is implementation defined.
In C, you can call the main function. In C++ you cannot.
So this is legal C:
#include <stdio.h>
#include <stdlib.h>
void usage(char *s)
{
printf("Usage:\n\t%s <number>\n\tThe value of <number> must be >= 0.\n", s);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
int n;
int result;
char *s[3];
char string[20];
if (argc < 1)
usage("factorial");
if (argc < 2)
usage(argv[0]);
n = atoi(argv[1]);
if (n <= 1)
return 1;
sprintf(string, "%d", n - 1);
s[0] = argv[0];
s[1] = string;
s[3] = NULL;
result = n * main(2, s);
printf("Current result is %d\n", result);
return result;
}
but it is not legal C++ because it calls main() explicitly.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.