+ 2
What is output?
Int add(){ Int a,b; Scanf("%d%d",&a,&b); Printf("%d",a+b); } Int main(){ add(add()); Return (0); } Thi code is run on my computer without giving any error and warning why?
7 Respostas
+ 4
This is very interesting! I've never seen a code like this. After some research, here is my explanation (could be wrong):
First of all, the code does compile but I got a Wreturn-type warning, because the add() should return int but there isn't return statements. It makes the return value undefined(could be any value).
Second, and the part you are curious about, add() is passed to add() -> add(add()) and doesn't produce error. I found this post: https://stackoverflow.com/questions/51032/is-there-a-difference-between-foovoid-and-foo-in-c-or-c
and found an interesting fact. add(void) is not equivalent to add(). add(void) means function that takes "no argument". And add() means function that takes "unspecified number of arguments with unspecified type" (NOTE: In C++ this is equivalent to add(void)). And yes, if the you change the declaration to add(void), the code will produce error. So I think it's because the declaration is add() which makes it can take any number of arguments with any type.
+ 2
Abhay thats why im asking people why this even works
+ 2
This would work, but am still figuring out why is this working
https://code.sololearn.com/cMZdR8U68haX/?ref=app
+ 1
So you are calling a add function passing a add function call as argument to a function add that doesn't have any parameters ,how does this even works?
+ 1
Abhay first check yourself
+ 1
Gaurav Rawat i am asking you ? I never saw someone passing an argument without specifying a parameter
+ 1
Gaurav Rawat well this shouldn't work at all ,maybe others can tell why it did