Question

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
}
Was it helpful?

Solution

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.)

OTHER TIPS

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)

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

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top