Andy Thomas Andy Thomas - 2 months ago 8x
C++ Question

"Narrowing conversion from 'int' to 'char' inside { }" for legal values when cross compiling

I have a C++ project that I compile both using

on my machine (compiling to "host") and to an ARM processor using a cross compiler (in my case
). I am in the process of converting to C++0x/11 standart and there is an error I get when compiling initialization list, which I was able to reproduce in the following snippet:

int main(void) {
char c[1] = {-108};

This program is seemingly correct as
is a legal value for a
Compiling this with
yields no error with the following command line:

g++ -std=c++0x

However, when I compile with the cross-compiler, like so:

arm-cortex_a8-linux-gnueabi-g++ -std=c++0x

I get the following error: In function 'int main()': error: narrowing conversion of '-0x0000000000000006c' from 'int' to 'char' inside { } [-fpermissive]

Since the value is legal, this seems like a bug. Can you explain why I get this error and what to do to solve it?

Edit: note that using positive values (e.g.,
) is legal and does not result in an error on both compilers.


When you declare a variable as char, it's implementation-dependent whether it's signed or unsigned. If you need to be able to store negative values, you should declare it signed explicitly, rather than relying on the implementation-defined default.

signed char c[1] = { -108 };