33 33 votes Consider the C program below. What does it print?# include <stdio.h> # define swap1 (a, b) tmp = a; a = b; b = tmp void swap2 ( int a, int b) { int tmp; tmp = a; a = b; b = tmp; } void swap3 (int*a, int*b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main () { int num1 = 5, num2 = 4, tmp; if (num1 < num2) {swap1 (num1, num2);} if (num1 < num2) {swap2 (num1 + 1, num2);} if (num1 > = num2) {swap3 (&num1, &num2);} printf ("%d, %d", num1, num2); } $5, 5$$5, 4$$4, 5$$4, 4$ Programming in C gateit-2008 programming programming-in-c easy parameter-passing + – Ishrat Jahan 18.4k views answer comment Share Follow Print See all 3 Comments 3 3 Comments reply Prashant_Dubey commented Aug 7, 2024 reply Follow flag if (num1 > num2) {swap1 (num1, num2);} if (num1 < num2) {swap2 (num1 + 1, num2);} if (num1 > = num2) {swap3 (&num1, &num2);} Now ans will be option (b) , First if condition is True so macros will be expanded and it will swap the value of num1 and num2 so num1 becomes 4 and num2 becomes 5 , 2nd if condition is False now come to 3 rd if condition which is true and it's a real swap of num values using the call by reference, num1 becomes 5 and num 2 become 4. 2 2 replyShare Prashant_Dubey commented Aug 7, 2024 reply Follow flag K&R Pg.no. 89 3 3 replyShare spongebob commented Nov 12, 2025 reply Follow flag I think the question was edited multiple times as different asnwers over time are using different code (especially the if conditions).This code is the one I found in gfg too as of the time I'm writing this.Right answer is this accepted answer, where only statement 3 is getting executed. if (num1 < num2) {swap1 (num1, num2);} if (num1 < num2) {swap2 (num1 + 1, num2);} if (num1 > = num2) {swap3 (&num1, &num2);} 0 0 replyShare Please log in or register to add a comment.
Best answer 41 41 votes Answer is C. Only: if (num1 > = num2) {swap3 (&num1, &num2);} Statement works, which in turn swaps num1 and num2. Rajarshi Sarkar answered Apr 15, 2015 • edited Jun 15, 2018 by Milicevic3306 Rajarshi Sarkar comment Share Follow See all 19 Comments 19 19 Comments reply Show 16 previous comments ChayAdhiraj commented Jan 17, 2023 reply Follow flag Hi, could you kindly give a detailed explanation ? I could not understand the solution. Not even from the comments am I clear. 5>4. So 1st condition is true !. Macro swaps so the 2nd cond is also true ! swap2 only changes adress so no change. 3rd cond is true and finally this gives us (5,5)….. I understand my answer is wrong. Could you kindly correct my mistakes? 0 0 replyShare mahendrapatel commented Jan 25, 2023 reply Follow flag Bhai,I am not getting this swapping by macro How it is been performed??? 0 0 replyShare sarkartanmoy commented Oct 20, 2024 reply Follow flag Right answer but wrong approach. swap1 expands and variable gets swapped. swap2 & swap3 won't execute. 0 0 replyShare Please log in or register to add a comment.
10 10 votes In main program num1=5, num2=4 first condition true. num1=4, num2=5 Now, second condition become true but no affect on values still num1=4, num2=5 third condition fail. Ans: 4,5 kavya kamish upadhya answered Aug 4, 2018 1 flag: ✌ Edit necessary (spongebob “wrong answer. 2nd statement says first condition true, but it isn't”) kavya kamish upadhya comment Share Follow See all 8 Comments 8 8 Comments reply Show 5 previous comments mahendrapatel commented Jan 25, 2023 reply Follow flag Is 5<4???? @KunuSwavik 0 0 replyShare sarkartanmoy commented Oct 20, 2024 reply Follow flag Wrong! GCC expands swap1 and only this gets executed. 0 0 replyShare Aditya_np1 commented Feb 18, 2025 reply Follow flag I believe that using macros it cannot be swap. the third condition will run. 0 0 replyShare Please log in or register to add a comment.
1 1 vote the swap1( ) function does not create new variables for a and b and temp. so it changes the same a and b. so they become 4 and 5. swap2( ) function cant change the value of a and b because it is called by value and create different a and b. the third condition does not get satisfied. so swap3( ) is not called and values remain as 4 and 5 for a and 5 respectively. Pabitra Sahoo answered Jan 10, 2019 1 flag: ✌ Low quality (spongebob “Wrong Answer. first if does not execute as num1 is more than num 2”) Pabitra Sahoo comment Share Follow See 1 comment 1 1 comment reply Muquim commented Dec 28, 2020 reply Follow flag Swap 1 is executing before the birth of a,b ... Then how it changes the *same a and b* ? 1 1 replyShare Please log in or register to add a comment.
1 1 vote Here is my approach: gopal_gate answered Sep 1, 2023 gopal_gate comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote The macro function gets subsituted by its defination. So, basically wherever in code swap1(a,b) is used it is replaced with tmp = a; a = b; b = tmp;After substitution the code becomes: int main(){ int num1 = 5, num2 = 4 , tmp; if(num1 > num2){ tmp = num1; num1 = num2; num2 = tmp; } ...So, first if condition (5 > 4) is satisfied . After swapping the values of num1 and num2 become 4, 5.The second if condition(4 < 5 .) is satisfied. But as function call is by value, so num1 and num2 don't change.The third if condition(4 >= 5) fails .So, final answer is num1 = 4 , num2 = 5. (C) Shloka answered Sep 6, 2024 1 flag: ✌ Edit necessary (spongebob “Wrong Answer. first if condition checks num1 less than num2, Not greater than”) Shloka comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote Option C is the answer.num1 = 5num2 = 4First if statement: if(num1>num2)5 > 4 is true, so we execute swap1(num1, num2)swap1 is a macro that expands to:{int tmp = num1;num1 = num2;num2 = tmp;}This successfully swaps the valuesAfter swap1: num1 = 4, num2 = 5 Second if statement: if(num1<num2)4 < 5 is true, so we execute swap2(num1 + 1, num2)swap2 receives copies of the values (5 and 5)This function swaps the copies but doesn't affect the original variablesAfter swap2: num1 = 4, num2 = 5 (unchanged) Third if statement: if(num1>=num2)4 >= 5 is false, so swap3 is not executed Thus, prints: 4, 5 Himanshu Pawar answered Feb 26, 2025 1 flag: ✌ Edit necessary (spongebob “Wrong Answer. first if condition checks num1 less than num2, Not greater than”) Himanshu Pawar comment Share Follow 0 reply Please log in or register to add a comment.