Hello,
I'm new to C and have a question about input to a function. In the code I came across
Wire.requestFrom (address, (uint8_t)3)
I'm not quite sure what (uint8_t)3) is doing.
Hello,
I'm new to C and have a question about input to a function. In the code I came across
Wire.requestFrom (address, (uint8_t)3)
I'm not quite sure what (uint8_t)3) is doing.
interlol wrote :
Basically, <stdint.h> is a new C/C++ header that defines a bunch of cross-platform types that you can use when you need an exact number of bits, with or without the sign.
It's very useful when you do computations on bits, or on specific range of values (in cryptography, or image processing for example) because you don't have to "detect" the size of a long, or an unsigned int, you just "use" what you need. If you want 8 unsigned bits, use uint8_t like you did.
And I insist on the fact that it's cross-platform, you can compile your program on a 64-bits Linux and a 32-bits Windows, and you won't have to change the types.
(type_name)x is called a cast. It converts from the type of x to type_name.
Thus, (uint8_t)3 creates a function parameter of type uint8_t with value 3 from a literal constant that would otherwise be of type int with value 3.
uint8_t is defined (somewhere) as a type that represents an unsigned integer in 8 bits, value 0 through 255.
int is defined (for 8 bit Arduinos, at least) as a type that represents an integer of 16 bits including sign, value -32768 through +32767.