Can we omit the data types of function arguments?

Studying the Quine program in C, I found that main was passed only with a , there is no data type. The following works fine and displays the program correctly.

 main(a){printf(a="main(a){printf(a=%c%s%c,34,a,34);}",34,a,34);} 

I would like to know how this works (and not the actual program for working with the site), but what is the data type a ? What value does he receive?

+4
source share
3 answers

First, let me tell you, given the hosted environment, the above code is a non-standard and very poor way of coding. You should not write such code.

This code uses the "default-to-int" property of inherited C. This property has been removed from the standard since C99 . Compilers can support and accept this for obsolete reasons.

Most likely, he used a value similar to the argc value.

+3
source

There is a difference between functions in general and main (), which is a special case.

For regular functions, the syntax you use will work in older versions of C, where the types will be treated as an "implicit int", and your function will become int func (int); . This nonsense was removed from the language 16 years ago, and such programs will no longer compile.

Regarding the form main (), there are some special cases described in detail with reference in this answer .

TL DR of this answer:

  • The main code form is not allowed to host the C90.
  • This is not (most likely not, the standard is ambiguous), allowed for any other hosted C implementation.
  • The code may be valid for some obscure self-implementation. However, the code does not make sense in such a context.
  • Or, most likely: the code is non-standard and will not compile in any appropriate compiler.
+3
source

What is a data type?

This is a int . This is a legacy of the old days when C objects were considered int , unless explicitly stated.

What value does he get?

This is a more complicated question. When trying to assign an address to a format string a . In the old days, people often used to point to pointers to int and vice versa. However, there was never a rule to say sizeof(char*) == sizeof(int) and, indeed, on my compiler (clang 7) a char* is 8 bytes, and int is only 4 bytes. This is why your program compiles (with six warnings), but seg faults when I run it.

+1
source

All Articles