What is 'narrowing conversion'?

When I compile the program I am working on I get this warning

remoteWeatherStation_PTX.ino: 261:43: warning: narrowing conversion of '176' from 'int' to 'const char' inside { } [-Wnarrowing]
   const char DEGREE_SYMBOL[] = { 0xB0, \0 }

I guess it's telling me that my declaration of the constant isn't kosher.

How do I write it so that it is kosher?

Thanks

The problem is that writing 176 as a number it is seen as an int. So when you assign it to a char it looks like you are assigning an int to a char and the compiler knows that doesn't always work but doesn't bother to check the number and see that it will fit.

I guess you could write: (char)176.

I'd be tempted to look at the line and verify that it fits in a byte and then say thank-you to the compiler for the warning but ignore it all the same.

1 Like

Delta_G:
I guess you could write: (char)176.

Bingo! :slight_smile:
Thanks

The problem is not quite as Delta_G describes it.
The compiler has detected that you are implicitly converting from a positive integer (0xB0 = 176) to a negative character value (char is a signed 8-bit quantity), where 0xB0 is -90. The warning isn't generated if the value is less than 128.

Pete

Thanks el_supremo ... I think :-\

Oh, I see. You are indeed correct.

Put another way, the largest positive number you can put in a char (or an int8_t) is +127;