What will be the output and why include int fint nfloat x ma
What will be the output and why?
#include <stdio.h>
int f(int n,float x);
main( )
{int a=10,n=12,p;
float x=2.2, y=7.4;
p=f(n,y);
printf(\"%d %d\ \",n,p);
for(n=1;n<3;n++)
printf(\"%d %d\ \",n,f(n,x));
if(x++>3)
printf(\"too big x=%f\ \",x);
else
printf(\"too small x=%f\ \",x);
}
int f(int a,float x)
{int n=5;
if(x>3)
{a=a+2;
x=x+2;
return a-x;}
else
{n=n-3;
x=x-3;
return n;
}
}
Solution
The Output :-
Explanation :-
STEP1 :-
Function call :-
p=f(n,y);
Here we are passing the values of n and y as 12 and 7.4.
Finally these values are stored in function definition.
STEP2 :-
Function definition :-
int f(int a,float x) // Now a=12 and x=7.4
{
int n=5;
if(7.4>3) //True so only if block will be executed.
{
a=a+2; //12+2=14
x=x+2; //7.4+2=9.4
return a-x; //14-9.4=4.6
}
Note :- In the main method the returning variable p is declared as int type.
so the return value 4.6 is returned as 4 to calling place and stored in the variable p.
else
{
n=n-3;
x=x-3;
return n;
}
STEP3 :-
printf(\"%d %d\ \",n,p); statement prints the values of n and p as 12 and 4. // Output Line No 1.
STEP4 :-
for(n=1;n<3;n++)
printf(\"%d %d\ \",n,f(n,x));
STEP4.1 :-
for(n=1;1<3;n++) //True
printf(\"%d %d\ \",n,f(n,x)); //Function call
Here we are passing the values of n and x as 12 and 2.2
Finally these values are stored in function definition as a=12 and x=2.2
STEP4.1.1 :-
int f(int a,float x) // Now a=12 and x=2.2
{
int n=5;
if(2.2>3) //False so only else block will be executed.
{
a=a+2;
x=x+2;
return a-x;
}
else
{
n=n-3; //5-3 = 2
x=x-3; //2.2-3 = -0.8
return n; //2 which is returned to f(n,x) and finally stored in p.
}
STEP4.1.2 :-
printf(\"%d %d\ \",n,f(n,x)); prints the values of n,f(n,x) as 1 and 2. // Output Line No 2.
STEP4.2 :-
for(n=1;2<3;n++) //True
printf(\"%d %d\ \",n,f(n,x)); //Function call
Here we are passing the values of n and x as 12 and 2.2
Finally these values are stored in function definition as a=12 and x=2.2
STEP4.2.1 :-
int f(int a,float x) // Now a=12 and x=2.2
{
int n=5;
if(2.2>3) //False so else block will be executed.
{
a=a+2;
x=x+2;
return a-x;
}
else
{
n=n-3; //5-3 = 2
x=x-3; //2.2-3 = -0.8
return n; //2 which is returned to f(n,x) and finally stored in p.
}
STEP4.2.2 :-
printf(\"%d %d\ \",n,f(n,x)); prints the values of n,f(n,x) as 1 and 2. // Output Line No 3.
STEP4.3 :-
for(n=1;3<3;n++) //False so for loop will be terminated.
STEP5 :-
if(x++>3) //(3.2>3) which is False so else block will be executed.
printf(\"too big x=%f\ \",x);
else
printf(\"too small x=%f\ \",x);
STEP6 :-
Finally printf(\"too small x=%f\ \",x); prints as too small x=3.2 // Output Line No 4.


