C | String | Question 14

Assume that a character takes 1 byte. Output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    char str[20] = "GeeksQuiz";
    printf ("%d", sizeof(str));
    return 0;
}

chevron_right


(A) 9
(B) 10
(C) 20
(D) Garbage Value


Answer: (C)

Explanation: Note that the sizeof() operator would return size of array. To get size of string stored in array, we need to use strlen(). The following program prints 9.

#include <stdio.h>
#include <string.h>
int main()
{
    char str[20] = "GeeksQuiz";
    printf ("%d", strlen(str));
    return 0;
}


Quiz of this Question



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.