Why “initializer-string for array of chars is too long” compiles fine in C & not in C++?

前端 未结 4 1938
一向
一向 2020-12-06 03:16

Following program compiles fine in C with warnings but fails in compilation in C++. Why? What is the reason?

#include 
int main(void)
{
    c         


        
4条回答
  •  执笔经年
    2020-12-06 03:46

    A valid string in C and C++ should have a \0 terminator and in

    char a[5]="Hello";
    

    There is no space for null terminator and a is not a valid string.

    So this might not be a error in C but you are bound to hit with issues when using in-built string functions like strlen() and family.

提交回复
热议问题