189k views
5 votes
What is the output of this code:

char arr2[] = "abc";
cout << sizeof(arr2) << endl;**

A) 3
B) 4
C) 5
D) Depends on the compiler

User Bendalton
by
8.3k points

1 Answer

2 votes

Final answer:

The output of the code is 4, as it includes the implicit null terminator at the end of the character array.

Step-by-step explanation:

The output of the code, given that it is written in C++ and executed on a typical compiler, is:

  • B) 4

When you create a character array with char arr2[] = "abc", it implicitly contains a null terminator ('\0') at the end, making the total size 4 bytes. Thus, the sizeof(arr2) will return the size of the array including the null terminator, which is 4 bytes.

User Tom Desp
by
7.8k points