Quote from learncpp.com: "std::int8_t and std::uint8_t likely behave like chars instead of integers".
My question is: what difference in behavior is there?
For example:
char myVal = 1;
Serial.print(myVal);
The serial monitor will show: 1
Is the actual value in RAM 0x01 (1 in decimal) or 0x31 (1 in ASCII code)?
Does the Arduino IDE treat char differently than uint8_t? And if not: what is the difference that learncpp.com warns for?
The Arduino Reference doesn't help much either: "It's recommended to only use char for storing characters. For an unsigned, one-byte (8 bit) data type, use the byte data type". (I tried to use byte. It doesn't compile.)
I don’t have that sketch anymore. It was months ago. I did replace it by uint8_t and it compiled. I use uint8_t eversince.
I don’t do much with texts, but the question still bugs me.
It‘s not a program that I like to fix, I fixed by using uint8_t. I’m searching for background information that Google and other sources don’t provide. And possibly, in the future, when I’m going to use texts, it may or may not become important.
The print() method checks the data type and if it is byte, then applies base 10 (default, DEC) to display the value of myVal2 variable.
The following code will show 41 on the Serial Monitor.
Serial.print(myVal2, HEX); //shows: 41
4. Why is uint16_t instead of int?
In 8-bit architecture like UNO, the int allocates 16-bit memory space for a variable.
In 32-bit architecture like DUE/ESP32, the int allocates 32-bit memory space for a variable.
In order to ensure that the compiler allocates 16-bit memory space for a variable regardless of MCU architecture(8-bit/16-bit/32-bit), the recommended data type is uint16_t.
char is an 8 bit signed variable (holds values from -128 to 127), mostly used to store ASCII characters (7 bit).
byte is an 8 bit unsigned variable (holds values 0 to 255), used to store small numbers like pin numbers and such. Not a universally recognized variable name.
I hadn't found arduino.h yet. And I tried to find all #includes for UNO R3 and Nano Every. So these miss out on the arduino.h (meant for 32-bit processors).
Programming Language is mostly a rule-based language. So, it is very important that we note down the rules as we progress in learning the language.
When you multiply 0x41 by 3 (0x03), the result is 0xC3, which will be seen by the print() method as 2's complement number (because MSBit of C3 is 1). The decimal equivalent of C3 = -1 x 27 + 67 = - 61.