Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we omit the data types of function arguments?

Tags:

c

while studying a quine program in C, I found that, main was passed with just a, there is no datatype. The below runs fine and outputs 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 does this work (not the actual quine program), but what is data-type of a? What value is it getting?

like image 258
mtk Avatar asked Mar 15 '23 02:03

mtk


2 Answers

First, let me tell you, considering a hosted environment, the above code is non-standard and very bad way of coding, too. You should not write code like that.

This code, make use of the "default-to-int" property of legacy C. This property has been removed from the standard since C99. Compilers might be supporting and accepting this for legacy reasons.

Most probably, it used to get the value similar to that of argc.

like image 115
Sourav Ghosh Avatar answered Apr 27 '23 20:04

Sourav Ghosh


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

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

As for the form of main(), there are some special cases, all described in detail with references in this answer.

TL;DR of that answer:

  • The form of main in your code is not allowed for a hosted C90 implementation.
  • It is not (most likely not, the standard is ambiguous) allowed for any other hosted C implementation either.
  • The code could be valid for some obscure freestanding implementation. Yet the code doesn't make sense in such a context.
  • Or most likely: the code is non-standard and will not compile on any conforming compiler at all.
like image 42
Lundin Avatar answered Apr 27 '23 21:04

Lundin