C | String | Question 3

What is the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
void swap(char *str1, char *str2)
{
  char *temp = str1;
  str1 = str2;
  str2 = temp;
}  
    
int main()
{
  char *str1 = "Geeks";
  char *str2 = "Quiz";
  swap(str1, str2);
  printf("str1 is %s, str2 is %s", str1, str2);
  return 0;
}

chevron_right


(A) str1 is Quiz, str2 is Geeks
(B) str1 is Geeks, str2 is Quiz
(C) str1 is Geeks, str2 is Geeks
(D) str1 is Quiz, str2 is Quiz


Answer: (B)

Explanation: The above swap() function doesn’t swap strings. The function just changes local pointer variables and the changes are not reflected outside the function. See following for more details.

https://www.geeksforgeeks.org/swap-strings-in-c/



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.