+24

Return type in C

for example a function return type is int but can still output floats. Considering this code. Does it mean the return type doesnt affects outputs of the code? Can someone please shed some lights. int sum (int, int); int main(){ int x = 5; int y = 10; float result =sum(x + y); Printf("%d + %d = %.2f", x,y, result); } int sum(int x, int y){ return x + y; }

9/18/2019 1:49:56 AM

BlackRose Mike

6 Answers

New Answer

+15

Fuzzy Squid Then how do we use it or make it do something as you said

+15

Maninder $ingh i see, i was expecting it to throw an error or a warning but it didnt.

+6

BlackRose Mike this is your answer man. Actually if you look deep in your code.your function sum actually return a int value,but in main function you are storing your int value returning from function in result variable which is float type that's why result variable converting your int value to float value. But actually sum function always returning int value.

+6

BlackRose Mike try this code actually your code have many errors. int sum(int x, int y){ return x + y; } int main(){ int x = 5; int y = 10; float result = sum(x,y); printf("%d + %d = %.2f", x,y, result); }