How does one define different types of ints?
I have the following
struct movCommand
{
uint8_t type;
uint8_t order;
int16_t height;
uint16_t distance;
int16_t yaw;
};
and need to define these according to the types they are.
What is the correct syntax for #define
when selecting the type for the define?
EDIT :
It looks like my question has been misunderstood.
I want to do this #define LANDING_COMMAND "2"
But I want to set the type of the landing command because it needs to be int16_t
You do not use #define
for this. You #include <stdint.h>
Rather than using the #define
directive, I'd use a typedef
, which is how the standard-library would define them inside of <stdint.h>
(at least on a C99-compatible platform). If you look in that header, you'll see how they're defined specifically on their platform. Typical typedefs
will be:
typedef unsigned char uint8_t;
typedef signed char int8_t;
typedef unsigned short uint16_t;
typedef signed short int16_t;
typedef unsigned int uint32_t;
typedef int int32_t;
//... etc., etc.
There's a lot more typedef
's defined inside the header file, including 64-bit types, etc.
If you are working with C99, you can use the typedef
s from <stdint.h>
or <inttypes.h>
(and <inttypes.h>
might be available even if <stdint.h>
is not - in non-C99 compilers).
If they are available (they usually are), all the types you show will be provided by those headers.
In general, a typedef
is preferable to a #define
.
With regards to your new question, the #define
is replaced literally with the text you provide. So
#define LANDING_COMMAND "2";
Will replace all uses of LANDING_COMMAND
with "2";
in the program text. This is probably not what you want.
First, preprocessing directives are not part of the C language, they're part of the preprocessor. Since they're not part of C, they're not statements, so they don't end with ;
. If you leave that in, it will likely cause problems if you intend to do things like func(LANDING_COMMAND);
.
Second, "2"
is of type char *
, which is not convertible to int16_t
with any safety. You need to use a literal 2
for the numeric value.
Lastly, to make it type int16_t
, you'll need to provide either a cast ( ((int16_t)2)
)
or use the macro
INT16_C(2)
which expands to a literal with the appropriate suffix to make it of size (and type)
int16_t
.
I recommend the latter, but the former should work
. The macro INT16_C(2)
could be used, but it expands to a literal (with the appropriate suffix) of type int_least16_t
, which is close but no cigar. stdint.h
only provides macros to make integer constant literals of the [u]int_leastN_t
types and the [u]intmax_t
types, not more generally for the [u]intN_t
or [u]int_fastN_t
types. Why they don't is beyond me.
include stdint.h gives you 8, 16, 32, and 64 signed and unsigned.
You can't do what you describe. Other answers have indicated workarounds. As for your specific question, from the MSDN site :
Expressions must have integral type and can include only integer constants, character constants, and the defined operator.
The expression cannot use sizeof or a type-cast operator.
#define
doesn't have a type. It's exactly the same as find/replace in your editor. You can do
#define LANDING_COMMAND 2
...
my_movCommand.yaw = LANDING_COMMAND;
The compiler will do the right type conversions for you, but if you insist on a type int16_t
then
#define LANDING_COMMAND ((int16_t)2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.