Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the program compiles?

Tags:

c

function

kr-c

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
}
like image 753
qqqqq Avatar asked Dec 26 '22 15:12

qqqqq


1 Answers

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

like image 87
Gilles 'SO- stop being evil' Avatar answered Jan 10 '23 22:01

Gilles 'SO- stop being evil'