why 2 and 3 are behaving so | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why 2 and 3 are behaving so

1.This prints "it is 10"- int main() {int x,y; x=5; y=5; int result=x+y; printf("it is %d ",result); } int fact(){ int x,y; x=5; y=5; int result=x+y; return result;} 2.This prints nothing- int main() { printf(it is %d",fact()); } int fact(){int x,y; x=5; y=5; int result=x+y; printf(result); } 3.This prints "10 it is 0" - int fact(){int x, y; x=5; y=5; int result= x+y; printf("%d ",result); } int main() { printf("it is %d",fact()); return 0; }

12th Oct 2023, 2:44 PM
We Doru
We Doru - avatar
2 Answers
+ 3
function which has int or float return type should return something. Your 2nd and 3rd function doesn't have return value. in 2nd case function should be declare first and printf should have %d to print value.
12th Oct 2023, 3:33 PM
A͢J
A͢J - avatar
0
2. This code has a syntax error and will not compile. The printf statement in main is missing a closing quotation mark. 3. This code defines a fact function that adds two variables x and y (both set to 5) and prints the result. In main, it calls fact and tries to print the return value, but the function fact doesn't return anything. Therefore, it prints "0" which is the default return value for functions when no explicit return statement is present.
13th Oct 2023, 1:02 AM
Lutreze Hue Jacinto
Lutreze Hue Jacinto - avatar