Sooo....em........can someone tell me what these things mean in C programming :
Uvar8
Uvar16
also what does it mean if a number has a capital u after it eg 10U?
Thanks:)
Embarrassing Software Question
- Jack Bauer
- Posts: 3563
- Joined: Wed Dec 12, 2018 5:24 pm
- Location: Ireland
- Has thanked: 1 time
- Been thanked: 87 times
- Contact:
-
- Posts: 656
- Joined: Sat Oct 05, 2019 6:50 pm
- Location: Northern California, USA
- Been thanked: 1 time
- Contact:
Re: Embarrassing Software Question
Context could be helpful if that's possible.
Sounds to me like variable names?
https://gist.github.com/uyjulian/7048f0 ... 143dee1703
So nothing the interpreter cares about.
Uvar8 could be a uint8_t - 8 bit unsigned.
No idea about the 10U. Possibly it tells the compiler to store it as an unsigned value, but the variable type should have handled that already..
Sounds to me like variable names?
https://gist.github.com/uyjulian/7048f0 ... 143dee1703
So nothing the interpreter cares about.
Uvar8 could be a uint8_t - 8 bit unsigned.
No idea about the 10U. Possibly it tells the compiler to store it as an unsigned value, but the variable type should have handled that already..
- mdrobnak
- Posts: 692
- Joined: Thu Mar 05, 2020 5:08 pm
- Location: Colorado, United States
- Has thanked: 1 time
- Been thanked: 5 times
Re: Embarrassing Software Question
This seems to be a good explanation:
https://stackoverflow.com/questions/902 ... r-a-number
And I'm 99% certain u == U. so 10u == 10U.
If this is in the context of a #define, then yes, it's telling the compiler "this should be treated as unsigned", as both of you figured out.
-Matt
https://stackoverflow.com/questions/902 ... r-a-number
And I'm 99% certain u == U. so 10u == 10U.
If this is in the context of a #define, then yes, it's telling the compiler "this should be treated as unsigned", as both of you figured out.
-Matt
Re: Embarrassing Software Question
The reason for these type-defs is that in the c language there is no standardisation and different compilers / platforms have no standard for basic data types such as int and long. On an Arduino int is 16 bits, but most PC's it's 32bits. So to try make c code platform independent they define data types that are ecplicit as to what the data size is such as unit16_t.
The U generally means the number should be treated as unsigned, again some compilers need to be explicitly told this.
The U generally means the number should be treated as unsigned, again some compilers need to be explicitly told this.
- Jack Bauer
- Posts: 3563
- Joined: Wed Dec 12, 2018 5:24 pm
- Location: Ireland
- Has thanked: 1 time
- Been thanked: 87 times
- Contact:
Re: Embarrassing Software Question
unit8 will result in slightly less compiled size. Think ATTiny chips. I think best practice is to use unit8 where possible.
The catch: uint8 can only hold up to 255. So if you need larger integer you need to use uin16
Note: int16 will go negative, uint16 only positive.
Capital U from what I understand is a "long" integer ....usually means it is in thousands. like 10U is 10,000
The catch: uint8 can only hold up to 255. So if you need larger integer you need to use uin16
Note: int16 will go negative, uint16 only positive.
Code: Select all
+---------------+-------------------------------+-------------------------------+-------------------------------+
| Type | Min | Max | Size |
+---------------+-------------------------------+-------------------------------+-------------------------------+
| sbyte | -128 | 127 | Signed 8-bit integer |
| byte | 0 | 255 | Unsigned 8-bit integer |
| char | U+0000 | U+ffff | Unicode 16-bit character |
| short | -32,768 | 32,767 | Signed 16-bit integer |
| ushort | 0 | 65,535 | Unsigned 16-bit integer |
| int | -2,147,483,648 | 2,147,483,647 | Signed 32-bit integer |
| uint | 0 | 4,294,967,295 | Unsigned 32-bit integer |
| long | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | Signed 64-bit integer |
| ulong | 0 | 18,446,744,073,709,551,615 | Unsigned 64-bit integer |
+---------------+-------------------------------+-------------------------------+-------------------------------+
Capital U from what I understand is a "long" integer ....usually means it is in thousands. like 10U is 10,000
- celeron55
- Posts: 776
- Joined: Thu Jul 04, 2019 3:04 pm
- Location: Finland
- Has thanked: 28 times
- Been thanked: 110 times
- Contact:
Re: Embarrassing Software Question
Sorry but that's not correct at all.
---
I might as well answer the original question while I'm here:
Uvar8 and Uvar16 are probably some typedefs made by some library. They're not a part of any C standard. You will find documentation about them in the docs of some of the libraries you're using.
The library probably intends them to be equivalent to the C standard uint8_t and uint16_t, i.e. unsigned 8 bit integer and unsigned 16 bit integer. Those types are defined in stdint.h, if you want to use them. Using them is a good idea in microcontroller programming when you want to have the maximum portability while making the most out of available memory and CPU time, and when you're working with bitmasks or such.
To get those exact same types without library support you'd use something like unsigned char and unsigned short, depending on compiler and target CPU. The specific bit types are defined by libraries and are used in programs in order to get rid of the compiler and CPU dependence of C types otherwise.
If a number literal has u or U after it, that means it's an unsigned int, instead of an int. There are some cases where you want to define that for a literal number, mainly if you're doing calculations near the maximum bit size of "int" of the chosen platform. See https://stackoverflow.com/a/9030338