How to test uint8_t is not assigned a value?


How do you test if a uint8_t has been assigned a value? I have the following variable defined before the setup() and loop() functions. Like so,

uint8_t varin;

During the execution of the program it may be assigned a byte = 0, 111, etc. In the loop() portion of the program I want to check it has been assigned a value or not. If it has been assigned a value, then I want to remove the value and continue execution. I want the 0 value if it comes around. So how do I do the check? I tried testing for varin!=NULL, it misses the 0s. I tried sizeof(varin)>0, that always returns something. I have tried testing for '\0', that didn't work, because that is equal to 0. I just can't seem to figure this out. How do I clear the value once I test?



In C/C++, there is no way to tell whether a variable has been assigned a value, and no way to specify that it no longer has a value. If there is a value that can't actually occur, you can use that.

This is usually done by using the next bigger integer size, and using "-1" as the "not assigned" value. For example, does this - it returns an "int" (16 bits) that is either -1 if no data is available, or a value from 0..255 as the actual byte received.

uint8_t indicate the type of variable that varin is. I think this means u => unsigned, value will be from 0 on up int8 => 8 bits long, to 0 to 255 _t = just part of format command

I think if you replaced "uint8_t" with "byte" you would have the same results.

Try this: if (varin !=0){ // if varin is not equal to 0, // do something based on varin having been changed elsewhere // varin = 0; // and then reset varin to 0 } else { // varin equals 0, so // do the varin = 0 code // }

Thanks for the comments westfw and Crossroads,

I will give the -1 a try first. If that doesn’t work I will try the if else. This starting me thinking about another question. If 0 is the same as ‘\0’. What happens when you are evaluating a char that has a 0 in it. Is that evaluated as the end of the string?



What your code recognizes as the end of a string is up to you.

I don't think -1 will work as the data type is defined as Unsigned if I read the Uint8_t correctly.

Thus the values will be treated as all positive, 0 to 255 , or binary 00000000 to 11111111 For -1 to work, values would be -127 to -1 to 0 to 1 to 127: negative #S 10000000 to 11111111, then 0 , and positive #s 00000001 to 01111111

How are you using the variable varin?


OK, so -1 didn’t work. Came up as 255 in the code. I should have read your part about the variable limits more carefully. I am still not sure about the if else statement, because in the first condition I am setting varin back to 0, which is one of my conditions. I am using this variable with the wishield to read incoming data over a socket connection. Sometimes data comes in, sometimes it doesn’t. Sometimes it comes in as a 0 byte. I can put this code in the receive function, but it is nicer to have in the main sketch, as opposed to the axillary sketch. I think that I am going to try inserting a boolean as my test condition, rather than evaluating the incoming variable itself.