Home / C Programming / Strings :: Discussion

Discussion :: Strings

  1. What will be the output of the program ?

    #include 
    
     int main() 
     { 
           char str[] = "Fresher\0GATE\0";   
           printf("%d\n", sizeof(str));  
           return 0;
     } 
    

  2. A.

    10

    B.

    6

    C.

    5

    D.

    11

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    The following examples may help you understand this problem:

    1. sizeof("") returns 1 (1*).

    2. sizeof("Fresher") returns 6 (5 + 1*).

    3. sizeof("GATE") returns 4 (3 + 1*).

    4. sizeof("Fresher\0GATE") returns 10 (5 + 1 + 3 + 1*).
        Here '\0' is considered as 1 char by sizeof() function.

    5. sizeof("Fresher\0GATE\0") returns 11 (5 + 1 + 3 + 1 + 1*).
        Here '\0' is considered as 1 char by sizeof() function.


Be The First To Comment