Char SizeOf confusion

Hello, I am trying to figure this:

int tmpT;
char str[10];

tmpT = (int)bmp.readTemperature();
sprintf (str, "%i", tmpT);
Serial.print("tmpT: ");
Serial.println(tmpT);
Serial.println(sizeof(tmpT));

Output #1:
tmpT: 31
4

int tmpT;
char str[10];

tmpT = (int)bmp.readTemperature()*-1;
sprintf (str, "%i", tmpT);
Serial.print("tmpT: ");
Serial.println(tmpT);
Serial.println(sizeof(tmpT));

Output #2:
tmpT: -31
4

I would have thought that the Output for #1 should have been 31 with the sizeof being 2. And for Output #2, -31 with the sizeof being 3.

Can somebody tell me why both show a size of 4?

Thanks
Eric

@EricBrian, sizeof() returns the size in bytes of the type of the referenced item. Since tmpT is of type “int” which is 32bits for 4 bytes on the Core, sizeof(tmpT) will return 4 always. :smiley:

2 Likes

Peekay, thanks a ton. Yes, of course, I guess if should have been able to make that realization myself. Don’t know what I was thinking.

Thanks for answering my stupid question! LOL

1 Like