C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 2)
2.
What will be the output of the program?
#include<stdio.h>
void fun(int*, int*);
int main()
{
    int i=5, j=2;
    fun(&i, &j);
    printf("%d, %d", i, j);
    return 0;
}
void fun(int *i, int *j)
{
    *i = *i**i;
    *j = *j**j;
}
5, 2
10, 4
2, 5
25, 4
Answer: Option
Explanation:

Step 1: int i=5, j=2; Here variable i and j are declared as an integer type and initialized to 5 and 2 respectively.

Step 2: fun(&i, &j); Here the function fun() is called with two parameters &i and &j (The & denotes call by reference. So the address of the variable i and j are passed. )

Step 3: void fun(int *i, int *j) This function is called by reference, so we have to use * before the parameters.

Step 4: *i = *i**i; Here *i denotes the value of the variable i. We are multiplying 5*5 and storing the result 25 in same variable i.

Step 5: *j = *j**j; Here *j denotes the value of the variable j. We are multiplying 2*2 and storing the result 4 in same variable j.

Step 6: Then the function void fun(int *i, int *j) return back the control back to main() function.

Step 7: printf("%d, %d", i, j); It prints the value of variable i and j.

Hence the output is 25, 4.

Discussion:
24 comments Page 1 of 3.

Vilas said:   8 years ago
@Rohit.

It's a recursive function, so it will call himself again n again .. in your question.

Till condition get satisfied i==10,

by i++ ;

Value of I will get increment in stack downwards. Once i become 10,

If condition will get sastisfty. So return statement comes in picture and loop will get terminated. exit.

As per the stacks, Value will print down to up, In reverse order i.e. 10 9 8 7 6 5 4 3 2....

Hope you get it.

Raj said:   1 decade ago
@Saumya, There is an unforced error in your program. In second printf statement six % of specifiers used for 5 arguments.

The output will be like this:

1229823404 1229823404 1229823404 // address of a.

13.500000 13.500000 13.500000 13.500000 13.500000 // value of a.

& - Takes the address of the variable.

* - Takes the data which is stored in the given address.

Saumya said:   1 decade ago
#include<stdio.h>
int main()
{
float a=13.5;
float *b,*c;
b=&a;
c=b;
printf("\n%u %u %u", &a, b,c);
printf("\n%f %f %f %f %f %f", a,*(&a),*&a, *b,*c);

return 0;
}

In this problem, what would be printed because of *c i.e last argument of second printf statement.
Can anybody help me out in this?

Rohit said:   1 decade ago
Can someone please help me with this

#include <stdio.h>
void temp(int i)
{
if(i==10) return;
i++ ;
temp(i);
printf("%d" , i);

}
int main()
{
temp(1);
}

This program(when executed in GCC compiler) gives the output
1098765432

How is it printing the elements in reverse order?

Dipak said:   5 years ago
Pointer multiplication is not allowed in C.

But here we take multiplication of the value of pointer means *i is storing value of i so we multiply the value of i.

If it's given like i*i then this give an error because we doing multiplication of pointer which is not allowed in C.

Ruby said:   1 decade ago
While declaring the function its is necessary to inform the compiler about that function, so that "void fun(int*, int*);" is written before main() function. Otherwise it will produce a compilation error.

Pullareddy said:   8 years ago
Call by reference is the with in the function we can passed the address to the variable function. That we can call the sub function that's why call the value is called call by reference.

Lokesh said:   1 decade ago
It's pointer of i(multiplied)by pointer of i
here the pointer value is the value that is present in the address of the variable .
so
*i=5,hence *i**i=5*5=25
*j=2,hence *j**j=2*2=4

Suraj said:   1 decade ago
The values at the &i and &j is squared in the called function so no need to use return statement. Printf statement will display updated values at the address.

KinG said:   1 decade ago
void temp(int i)
{
if(i==10) return;
i++ ;

printf("%d" , i);
temp(i);
}


Change Your function body with this. Then Your Problem will be solved. :)


Post your comments here:

Your comments will be displayed after verification.