GATE 2015 COMPUTER SCIENCE & INFORMATION TECH. – CS – C Programming | Q. 34 (Session – 1)

💻 GATE 2015 — C Programming
📝 Question Number: 34   |   Question Type: NAT
🔍 The output of the following C program is  
void f1(int a, int b) {
    int c;
    c=a; a=b; b=c;
}

void f2(int *a,
         int *b) {
    int c;
    c=*a; *a=*b; *b=c;
}

int main() {
    int a=4, b=5, c=6;
    f1(a,b);
    f2(&b, &c);
    printf("%d",c-a-b);
}

Call by Value and Call by Reference

A simple way to understand these two concepts is to imagine a classroom and a student’s marks record.

🟢 Call by Value – Give a Copy

Imagine a teacher gives a student a photocopy of the marks record. The student changes the photocopy from 80 to 100.

The teacher’s original record is still 80.

Simple rule:
“I give you a copy. You can change your copy, but my original does not change.”

🔵 Call by Reference – Access the Original

Now imagine that the teacher gives the student access to the original marks record. If the student changes the marks from 80 to 100, the original record also becomes 100.

Simple rule:
“I give you access to the original. If you change it, my original changes too.”

Quick Comparison

Call by Value Call by Reference
A copy is passed. Access to the original is provided.
Changing the copy does not change the original. Changing the original changes the caller’s value.
Remember:
🟢 Call by Value → Copy
🔵 Call by Reference → Original
Let’s solve the program step by step in a very simple way.
🔹 1. Values in main()
int a = 4,
    b = 5,
    c = 6;
So initially:
Variable
Value
a
4
b
5
c
6
💡 Initial state: Before calling any function, a = 4, b = 5, and c = 6.

Variables a, b, c are local to main() function. They are local variables of main() function.

🔹 2. What happens in f1(a, b)?
void f1(int a, int b) {
    int c;
    c = a;
    a = b;
    b = c;
}
The call is:
f1(a, b);
Here a and b are passed by value.
💡 That means f1() gets copies of main()‘s a and b. As they are photocopies, f1() can’t change local variables of main() function.
Now let’s trace the values inside f1().
a = 4, b = 5

c = a;   → c = 4
a = b;   → a = 5
b = c;   → b = 4
So inside f1():
Variable
Value
a
5
b
4
⚠️ Important: These are only the copies inside f1(). Note that f1() can’t change the original variables – a, b of main() because a, b are passed by value.
Therefore, after f1() finishes, main() still has:
a = 4
b = 5
c = 6
✅ Conclusion: f1() has no effect on the variables a and b in main().
🔹 3. What happens in f2(&b, &c)?
The call is:
f2(&b, &c);
📍 Notice the & operator. & is the address of operator. IT RETURNS ADDRESS OF A VARIABLE. &b returns address of b and &c returns address of c.
&b means the address of b, while &c means the address of c.
Therefore, the function receives pointers:
void f2(int *a,
        int *b)
Initially, in main():
a = 4
b = 5
c = 6
Now let’s see what happens inside f2().
c = *a;
*a = *b;
*b = c;
💡 Since a points to b and b points to c,
*a represents the value of b, while *b represents the value of c.
So the statements execute as follows:
c = *a;    → c = 5
*a = *b;   → b = 6
*b = c;    → c = 5
✅ Note: *a means value at pointer a. * is the ‘value at address’ operator. Therefore, *a = 5. See the statement: c = *a. Here = is assignment operator. Asignment is done from right to left. Therefore c becomes 5.
✅ Note: See the statement: *a = *b. Here a, b are pointers in f2() function. a points to local variable b and b b points to local variable c. Therefore *b becomes 6. And this 6 is assigned to *a. Hence *a = 6. As ‘a’ points to main() b, in main() b become 6. Similarly you can understand *b =c statement.
✅ Key Point: Unlike f1(), f2() receives addresses. Therefore, changing *a and *b changes the original variables in main().
🔹 4. Evaluate printf()
The program has:
printf("%d", c-a-b);
Substitute the final values:
c - a - b
= 5 - 4 - 6
💡 Remember: Subtraction is evaluated from left to right.
5 - 4 = 1
1 - 6 = -5
Therefore:
Output = -5
✅ Final Answer
-5
💡 Key Idea to Remember
f1(a,b)
    → Pass by value
    → changes do NOT affect main()

f2(&b,&c)
    → Pass addresses
    → changes DO affect main()
So, the entire question mainly tests the difference between call by value and pointer-based modification .
📌 In introductory C explanations, pointer-based modification is sometimes informally called “call by reference”. Technically, C passes arguments by value; in f2(), the values being passed are addresses.
🔑 Key Takeaways: C Functions and Pointers
Important basics to remember while solving C programming questions
💡 Before solving function-based questions, remember these basic ideas. A C program can contain several functions. One function can call another function, pass information to it, and then continue its execution when the called function finishes.
1️⃣ Calling Function and Called Function

Suppose we have:

void f1() {
    /* statements */
}

int main() {
    f1();
}

Here, main() calls f1().

📌 Calling function: The function that makes the function call.
In the above example, main() is the calling function.

📌 Called function: The function that is called.
Here, f1() is the called function.

A simple way to remember:

main() ── calls ──► f1()
2️⃣ Function Definition

The function definition contains the actual statements that the function executes.

void f1(int a, int b) {
    int c;
    c = a;
    a = b;
    b = c;
}

Here, the complete block beginning with void f1(int a, int b) and ending with } is the function definition.

3️⃣ Function Call
f1(a, b);

This statement tells the program to execute the function f1() by supplying the values of a and b.

💡 When a function is called, control moves from the calling function to the called function. After the called function finishes, control returns to the calling function.
4️⃣ What About f1(a,b) in This Question?

The statement

f1(a, b);

is actually executed. Therefore, we should not say that the function itself is ignored.

However, f1() receives copies of a and b because they are passed by value. Therefore, any changes made to its local a and b do not change main()‘s variables.

✅ For finding the final output: the effect of f1(a,b) on the variables in main() is zero. Therefore, its effect on the final calculation can be ignored.

But remember: the function is still called and executed.

5️⃣ What Happens to Local Variables?

Variables declared inside a function are generally called local variables of that function.

void f1() {
    int x = 10;
    int y = 20;
}

Here, x and y are local variables of f1().

🔄 When f1() finishes, the lifetime of its automatic local variables ends. They are no longer available for use after the function has returned.

So, for beginner-level questions, you can remember:

Function starts → Local variables exist → Function finishes → Their lifetime ends
6️⃣ How Does a Function Return?

Consider a void function:

void f1() {
    printf(“Hello”);
}

When execution reaches the closing brace }, the function reaches the end of its body.

🔙 Control automatically returns to the point immediately after the function call in the calling function.

For example:

main() {
    f1();
    printf(“World”);
}

After f1() reaches its closing }, execution continues with:

printf(“World”);
7️⃣ A void Function

A function declared with void does not return a value to its caller.

It can finish in either of these ways:

void f1() {
    printf(“Hello”);
}

void f2() {
    printf(“Hello”);
    return;
}

In the first function, reaching } ends the function. In the second function, the return; statement ends the function immediately.

💡 Remember: return; in a void function means return control without returning a value.
8️⃣ Functions That Return a Value

If a function is declared with a return type such as int, float, or char, it is expected to return a value of the appropriate type.

int add(int a, int b) {
    return a + b;
}

Here:

int → tells us that the function returns an integer.
return a + b; → sends the calculated integer back to the calling function.

For example:

int result;
result = add(10, 20);

The value returned by add() is stored in result.

✅ Simple rule: A non-void function normally uses return value; to return a value to its caller.
⚠️ Important Point About Non-void Functions

Do not assume that every function can simply reach } without returning a value.

For a non-void function such as an int function, if execution reaches the closing } without producing a required return value, the behavior is generally undefined if the caller uses the function’s result.

📌 Therefore, for beginner-level problem solving, remember:

void function → may end by reaching } or by executing return;.

Non-void function → should return an appropriate value using return value;.
🏠 10️⃣ When You See a Pointer, Think About a HOUSE

A very simple way to understand pointers is to imagine a real-life house.

🏠
HOUSE
Think of a variable as a house.
📍
HOUSE ADDRESS
Think of the address as the variable’s memory address.
📝
ADDRESS WRITTEN DOWN
Think of a pointer as storing that address.

For example:

int b = 5;
int *p = &b;

Think of it like this:

🏠 b = the house
📍 &b = the address of the house
📝 p = a pointer containing that address
🚪 *p = access the value inside that house

Since p contains the address of b, writing:

*p = 10;

means: go to the house whose address is stored in p and change the value inside it to 10.

🧠 Easy memory trick:

Variable = 🏠 House   |   Address = 📍 Location   |   Pointer = 📝 Stores the address   |   *pointer = 🚪 Access the value
🔍 Applying the HOUSE Idea to This Question
f2(&b, &c);

Here, &b and &c are the addresses of the two variables.

Therefore, f2() receives the locations of the original variables in main(), rather than merely receiving copies of their values.

💡 This is why statements such as *a = *b; inside f2() can directly change the variables belonging to main().
⭐ Quick Revision
🔹 Calling function → the function that calls another function.
🔹 Called function → the function being called.
🔹 Function definition → contains the statements executed by the function.
🔹 Function call → tells the program to execute another function.
🔹 void function → does not return a value.
🔹 return; → ends a void function and returns control to its caller.
🔹 Reaching } → normally ends the function and returns control to its caller.
🔹 Non-void function → should return an appropriate value using return value;.
🔹 Local variables → their lifetime ends when their function execution ends (for ordinary automatic local variables).
🔹 Pointer → think of a 🏠 HOUSE; the pointer stores the house’s address.
🔹 &variable → address of the variable.
🔹 *pointer → access the value at the address stored in the pointer.
0

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »