Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

excess elements
error
c++
warning

The "excess elements in scalar initializer" error in C++

Educative Answers Team

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.

svg viewer

​## 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;
}

RELATED TAGS

excess elements
error
c++
warning
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring