+ 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?

7th Sep 2020, 9:34 AM
Gaurav Rawat
Gaurav Rawat - avatar
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.
7th Sep 2020, 10:23 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
Abhay thats why im asking people why this even works
7th Sep 2020, 9:44 AM
Gaurav Rawat
Gaurav Rawat - avatar
+ 2
This would work, but am still figuring out why is this working https://code.sololearn.com/cMZdR8U68haX/?ref=app
7th Sep 2020, 10:01 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 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?
7th Sep 2020, 9:39 AM
Abhay
Abhay - avatar
+ 1
Abhay first check yourself
7th Sep 2020, 9:42 AM
Gaurav Rawat
Gaurav Rawat - avatar
+ 1
Gaurav Rawat i am asking you ? I never saw someone passing an argument without specifying a parameter
7th Sep 2020, 9:43 AM
Abhay
Abhay - avatar
+ 1
Gaurav Rawat well this shouldn't work at all ,maybe others can tell why it did
7th Sep 2020, 9:51 AM
Abhay
Abhay - avatar