Home / C Programming / Strings :: Discussion

Discussion :: Strings

  1. What will be the output of the program ?

     #include 
    
     int main()
     {     
           char str[25] = "FresherGate";  
           printf("%s\n", &str+2);    
           return 0; 
     } 
    
    

  2. A.

    Garbage value

    B.

    Error

    C.

    No output

    D.

    diaGATE

    View Answer

    Workspace

    Answer : Option A

    Explanation :

    Step 1: char str[25] = "FresherGATE"; The variable str is declared as an array of characteres and initialized with a string "FresherGATE".

    Step 2: printf("%s\n", &str+2);

    => In the printf statement %s is string format specifier tells the compiler to print the string in the memory of &str+2

    => &str is a location of string "FresherGATE". Therefore &str+2 is another memory location.

    Hence it prints the Garbage value.

     


Be The First To Comment