C: Data Type Consistency
idbruce
Posts: 6,197
While working on Teacup, I used most of the following data types, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, char, and int. And then as I go through various examples provided with SimpleIDE and various library files, although equivalent, I see the use of different data types.
such as int, char, unsigned int, etc.... And of course these all show up blue in the IDE. And I would imagine these would be prefered by other forum members...
So where can I find a copy of the keywords and prefered data types for SimpleIDE?
I would prefer that my code conform to the general usage throughout the forum and be consistent with program examples distributed with SimpleIDE, GCC, and by Parallax.
such as int, char, unsigned int, etc.... And of course these all show up blue in the IDE. And I would imagine these would be prefered by other forum members...
So where can I find a copy of the keywords and prefered data types for SimpleIDE?
I would prefer that my code conform to the general usage throughout the forum and be consistent with program examples distributed with SimpleIDE, GCC, and by Parallax.
Comments
Also check ASM output, as sometimes the native size on a MCU codes smaller, than a smaller type if that smaller type is less naturally supported..
The issue is that the size of int is not defined in C. Except that it will be a minimum of 16 bits. Similarly C does not define whether a char is signed or unsigned.
If these things bother you, which they should, us the C99 types you have listed.
On the other hand I find things like "int32_t" to be really ugly sprinkled throughout the code. After all they mix up letters, numbers and "_" in the same way as is recommended for unguessable passwords!
So I will often use int and char in any place where it's not likely to be critical (the "i" on loop counters, general strings for example). And make the assumption my code is never going to make it back to a 16 bit system.
Then again one can always define ones own types with typedef.
For those averse to the _t names, I've seen some programmers take them down to a truly minimal
u8 u32 etc - I can still understand what they used.
I must admit that your responses surprise me.
Being accustomed to the MS way of doing things, it was a little wierd using the uint8_t, int8_t, uint16_t, int16_t, uint32_t, and int32_t data types, but now I am starting to like this method, because as jmg points out, it includes the bit size, and this should avoid misinterpretation.
Okay, then I will keep using these.
In the world of mathematics, you know, where numbers come from (or at least their definitions) we have:
"natural" numbers: 1, 2, 3, 4, 5, 6....
"whole" numbers: 0, 1, 2, 3, 4, 5, 6....
"integer" numbers: ..., 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, ......
"rational", or "fractional, numbers: p/q for integer values of p and q.
"irrationals": Those that cannot be written as p/q
"real" numbers: The rationals an irrationals combined.
So we see that C has it a bit wrong. What C calls an "unsigned int" is actually a "whole". In fact "unsigned int" is a contradiction in terms.
We have float, well not wrong exactly but why not call it a "rational" or "rat"?!
"long" and "double" are just silly.
"char", "signed char", "unsigned char" are dumb. A character is not a number. It is neither signed or unsigned.
What a mess.
It is surprising to me that everyone basically supports the idea of using uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, as compared to "unsigned this and that". And it also surprises me that I have not found any of these data types being used in obvious Parallax documentation and coding examples.
LOL I would have to agree 100%
typedef unsigned char very_small_whole;
typedef unsigned short small_whole;
typedef unsigned whole;
typedef unsigned long big_whole;
typedef unsigned long long very_big_whole;
typedef float rat;
typedef double big_rat;
typedef long double very_big_rat;
int Integer of system specific size, though always greater than 16 bits.
short Usually 16-bits, though that is not for sure.
long At least 32 bits.
char 8 bit integer (usually unsigned, though not always).
float Real number represented in IEEE 32-bit floating point form.
double High precision real number in IEEE 64 bit floating point form.
unsigned modifies any integer data type to be unsigned.
The bit size extensions to the data types that were added with C99 are usually implemented as macros of the above.
This is basically what I have seen in Parallax documentation and coding examples, as well as in the libraries.
In SimpleIDE, the data types that David mentions, int, short, long, char, float, double, and unsigned, all show up as dark blue, whereas uint8_t, int8_t, uint16_t, int16_t, uint32_t, and int32_t, all show up as gold. So I am assuming, from a SimpleIDE viewpoint, that int, short, long, char, float, double, and unsigned are the prefered data types.
EDIT: Now considering the beginners perspective, it could be very confusing to inter-mingle uint8_t, int8_t, uint16_t, int16_t, uint32_t, and int32_t with int, short, long, char, float, double, and unsigned.
EDIT: So we now have uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, int, short, long, char, float, double, and unsigned in two different colors. Oh my The gold colored ones must be more powerful
char - Smallest addressable unit of the machine that can contain basic character set : May be signed or not. That is to say undefined.
short - Signed, at least 16 bits in size. That is to say undefined.
int - Signed, at least 16 bits in size. That is to say undefined.
long - Signed, at least 32 bits in size. That is to say undefined.
long long - Signed, at least 64 bits in size. That is to say undefined.
In summary:
The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits.
i.e. they are all undefined apart from their minimum size.
Of course my saying "undefined" is a bit harsh. The C language standard leaves them as implementation specific. And the implementations do define them.
I like that. Short, clear, and to the point.
The problem with the syntax highlighting is that C language keywords will be one colour. Non C language defined things will be another colour. Variables, typedefs etc.
int32_t and friends are not part of the language syntax exactly. You have a point that an exception should perhaps be made for them when syntax highlighting. I bet there is a config file in SimpleIDE where that can be tweaked.
That seems odd to me. Aren't the terms like "int" defined from "int32_t" in the first place (obviously specific definitions are impl. specific)?
Careful now. You might turn it into a full-blown IDE :P
I would think, considering the name of the program, a simple hardcoded list of values would be fine. No need to scan and support user-defined types. It would be really cool... but you're not doing any sort of source code scanning at the moment are you? It would be a huge undertaking. I would think, if you wanted something like that, you'd be better off starting with a real IDE like QtCreator, Code::Blocks, Eclipse, IntelliJ, etc and then simplifying it down. Oh wait... that's OmniaCreator :P
The uint_8 etc, C99 type, are defined in a header file "stdint.h" in terms of the language defined types. So uint_8 is defined as "unsigned char" just in case your machine has signed chars. For example. That header file may well be different on different compilers.
Oh! I'd been reading the typedefs the wrong way around the whole time :P
If nothing else, I don't think it hurts being verbose when it makes sense to do so.