The Gateway to Computer Science Excellence
+13 votes
2.5k views

Consider the C program below. What does it print?

# include <stdio.h>
# define swapl (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);
}
  1. $5, 5$
  2. $5, 4$
  3. $4, 5$
  4. $4, 4$
in Programming by Boss (16.3k points)
edited by | 2.5k views

3 Answers

+24 votes
Best answer

Answer is C.

Only:

if (num1 > = num2) {swap3 (&num1, &num2);}

Statement works, which in turn swaps num1 and num2.

by Boss (33.9k points)
edited by
+2

I got this question but what if it had 

if (num1 > num2) {swap1 (num1, num2);}
if (num1 < num2) {swap2 (num1 + 1, num2);}
if (num1 < = num2) {swap3 (&num1, &num2);}
0
i think then also same thing happen Because call by value have no effect on variable value but call by reffrence has.
0
but in c we dont have call by references ...... we have ony call by value ....

 

in this case only swap3 will run ..... so the answer should be 5,4

 

if the answer is c then swap1 must have run then only the answer would be 4,5

@arjun sir check it
+1
swap1 will execute but answer is due to swap3  ??? right or wrong
+1

https://onlinegdb.com/ry_Ztg1Bf

Same answer 4,5.

And it is due to swapl macro expansion.

0
@Rajarshi

I understood question but I didn't get then what is the use of define used as prepocessor as function swap3 is already defined.

Please suggest

Thanks
+1
# include <stdio.h>
# define swapl (a, b) tmp = a; a = b; b = tmp

int main ()
{
        int num1 = 5, num2 = 4, tmp;
        if (num1 > num2) {swap1 (num1, num2);
        }
        printf ("%d, %d", num1, num2);
}

What would be answer now? Can someone please tell.

0

@Ashish Jha u are right

+1

@jaisyking answer would be 4,5...just expand the macro which would be equal to swapping of num1 and num2 in the same block.

+1

@Ashish Jha you are correct, here are some minor changes that proves it.

https://onlinegdb.com/By4IdEvAr

+3 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

by Junior (579 points)
0

What? please see the question again.

0
yeah, it's right. the swap1( ) function does not create new variables for a and b. 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 values remain as 4 and 5 for a and 5 respectively.
0
This is wrong!

First if-condition is not true. Only last one is.
+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.
by (45 points)
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,391 answers
198,591 comments
105,442 users