In C/C++, when a character array is initialized with a double quoted string and array size is not specified, compiler automatically allocates one extra space for string terminator ‘\0’. For example, following program prints 6 as output.
If array size is specified as 5 in the above program then the program works without any warning/error and prints 5 in C, but causes compilation error in C++.
When character array is initialized with comma separated list of characters and array size is not specified, compiler doesn’t create extra space for string terminator ‘\0’. For example, following program prints 5.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- What's difference between char s and char *s in C?
- Arrays in C Language | Set 2 (Properties)
- How arrays are passed to functions in C/C++
- Arrays in C/C++
- What are the data types for which it is not possible to create an array?
- Difference between pointer and array in C?
- Initialization of variables sized arrays in C
- Memory Layout of C Programs
- Are array members deeply copied?
- Initialization of a multidimensional arrays in C/C++
- Write one line functions for strcat() and strcmp()
- Do not use sizeof for array parameters
- gets() is risky to use!
- C function to Swap strings
- Storage for Strings in C