array in c vs c++

I have doubt regarding the following piece of code :

1
2
3
4
5
6
7

int main()
{
    int array1 = {1,2,3,4,5};
    int array2[] = {1,2,3,4,5};
    int array3[5] = {1,2,3,4,5};
}


This piece of code gives a error on line 4 in c++ but not in c?
I know array1 is actually a int and array2 and array3 are actually arrays

so why does not a c compiler show a error , but just a warning "excess elements in scalar initialization"

Is there a use of such a definition and why is it valid in c?

Thanks!
In C, you can initialize single values with scalar types like so

1
2
3
int x = { 5 };
//same as
int x = 5;


Apparently this is not the case in C++, as you noticed. I'm not exactly sure why they allow it in C, possibly to support using {0} as a generic initializer.
1
2
3
4
5
6
7
8
9
#include <iostream>
int main()
{
	int a = {5};
	int b{6};    
    std::cout << a << b;    // 56
return 0;    
}
Topic archived. No new replies allowed.