Home / C Programming / Strings :: Discussion

Discussion :: Strings

  1. What will be the output of the program ?

     #include
     void swap(char *, char *);  
    
     int main() 
     {   
         char *pstr[2] = {"Hello", "FresherGATE"};  
         swap(pstr[0], pstr[1]);    
         printf("%s\n%s", pstr[0], pstr[1]);    
         return 0;
     } 
     void swap(char *t1, char *t2)
     {   
         char *t;   
         t=t1;     
         t1=t2;   
         t2=t;
     }
    

     

  2. A.

    FresherGATE
    Hello

    B.

    Address of "Hello" and "FresherGATE"

    C.

    Hello
    Fres

    D.

    Iello
    HFresherGATE

    View Answer

    Workspace

    Answer : Option C

    Explanation :

    Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything.

    Step 2: char *pstr[2] = {"Hello", "FresherGATE"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to

    pstr[0] = "Hello", pstr[1] = "FresherGATE"

    Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program.

    If the swap function is "called by reference" it will affect the variable pstr.

    Step 4: printf("%s\n%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].

    Hence the output of the program is

    Hello
    FresherGATE

     


Be The First To Comment