The "excess elements in scalar initializer" error in C++
The excess elements in scalar initializer is an error thrown when a scalar object is assigned more than one value.
Remember that scalar objects are objects that only hold one value at a time.
## Wrong code
In the code below, the scalar object foo is set equal to more than one character. Therefore, it throws an error.
#include <iostream>using namespace std;int main(){char foo = {'a','b','c'};return 0;}
Correct code
The code below sets foo equal to a single character and, therefore, compiles correctly.
#include <iostream>using namespace std;int main(){char foo = 'a';return 0;}
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved