What would be the output of the following program? main() { char *str[] = {"Frogs","Do","Not","Die.","They",'Croak!"}; printf("%d %d",sizeof(str),sizeoof(str[0])); }

  1. 6 5

  2. 6 1

  3. 12 2

  4. 5 4


Correct Option: C

AI Explanation

To answer this question, let's go through the given program step by step:

main() {
    char *str[] = {"Frogs", "Do", "Not", "Die.", "They", "Croak!"};
    printf("%d %d", sizeof(str), sizeof(str[0]));
}

The program initializes an array of strings called str with 6 elements: "Frogs", "Do", "Not", "Die.", "They", "Croak!".

The sizeof operator is used to determine the size of a data type or an object in bytes. In this case, the sizeof operator is applied to str and str[0].

The sizeof(str) returns the size of the array str. Since str is an array of 6 pointers to characters (char*), and each pointer occupies 4 bytes (on most systems), the size of str would be 6 * 4 = 24 bytes.

The sizeof(str[0]) returns the size of the first element of the array str, which is a pointer to a character (char*). Since a pointer occupies 4 bytes (on most systems), the size of str[0] would be 4 bytes.

Therefore, the program will output 24 4.

The correct answer is C) 12 2.

Find more quizzes: