Why the program compiles?

303 views Asked by At

I am just trying to understand this C code ( not trying to achieve any functional goal by the program). This compiles using gcc. Is this main in main(int a, char *argv[] ) format? Is it permissible to declare anything beween argument and function body (similar to char *a; here)?

#include <stdio.h>
main(u,_,a)
  char
  *a;
{
  //printf("%s\n",_,a);//just to help debugging
  //printf("%d\n",u); //just to help debugging
}
4

There are 4 answers

0
Gilles 'SO- stop being evil' On BEST ANSWER

This is an old, obsolete way of writing C functions.

In an ancestor language of C, there were no types: all variables contained a machine word. So a function definition would start like this:

main(u, _, a) {
    /* ... some code ... */
}

C as it used to be, known as ā€œK&R Cā€ from the authors of the seminal book about C (Brian Kernighan and Dennis Ritchie) added types in a form that looked like variable declarations, and were between the list of function parameters and the code of the function.

int main(u, _, a)
    int u;
    int _;
    char *a;
{
    /* ... some code ... */
}

In K&R C, if a type is int, then in many places it can be omitted. For a function parameter, you can omit the type declaration line altogether.

int main(u, _, a)
    char *a;
{
    /* ... some code ... */
}

ANSI C was standardized in 1989, and one of its main innovations was function prototypes. In proper ANSI C, you declare all functions before use and you declare the types of all arguments.

int main(int u, int _, char *a)
{
    /* ... some code ... */
}

C compilers still support the old form for legacy code. (If they conform to the 1989 C standard, they have to.) There isn't much legacy code left after more than 20 years, so you won't find such code often.

(Note that this is not the right type for main. I think Gcc will warn you about that but you may have to turn the warning settings up.)

5
ouah On

This is an invalid form of main declaration. This is invalid in C99 / C11 but also invalid in C90. (See 5.1.2.2.1 for valid declarations of main function in C90).

0
Adam D. Ruppe On

That's an old declaration that nobody uses anymore except for obfuscation (see also: trigraphs!). I think it is illegal under the new C standards, but gcc still supports it for backward compatibility.

The way it works is the types are listed under the function, and the return type is left off. No return type means it defaults to int. The typical main function could be written this way:

main(argc, argv)
    int argc;
    char** argv;
{
    printf("%d\n", argc);
    return 0;
}

You cannot declare other variables before the opening brace. Try adding int c; and get this error:

test.c:4: error: declaration for parameter ā€˜cā€™ but no such parameter
0
mcleod_ideafix On

Declarations between argument list and function body were part of the so called K&R C (the first version of C). So yes, they are valid, if your compiler can compile K&R code.

About main() having more than two arguments... yes, in fact, main can have up to three arguments:

int main (int argc, char *argv[], char *envp[]);

The third argument being an array of pointers to strings, containing each one of them a environment variable definition (a string in the form name=value)