What does the "U" in c defines mean (i.e. #define FOO ((uint32_t)1000U))) - c-preprocessor

I think it could mean unsigned. But what exactly is the difference between:
#define FOO ((uint32_t)1000U))
and
#define FOO ((uint32_t)1000))

According to this table, it indicates a constant of type unsigned int. The same explanation can be found here.

Related

Difference between UINT32_C and uint32_t

As far as I know the suffix t in uint32_t denote type name but I wonder to know what is the C in UINT32_C and what is the differences?
UINT32_C is a macro which defines integer constant of type uint_least32_t. For example:
UINT32_C(123) // Might expand to 123UL on system where uint_least32_t is unsigned long
// or just 123U, if uint_least32_t is unsigned int.
7.20.4.1 Macros for minimum-width integer constants
The macro INTN_C(value) shall expand to an integer constant expression
corresponding to the type int_leastN_t. The macro UINTN_C(value) shall expand
to an integer constant expression corresponding to the type uint_leastN_t. For
example, if uint_least64_t is a name for the type unsigned long long int,
then UINT64_C(0x123) might expand to the integer constant 0x123ULL.
It is thus possible that this constant is more than 32 bits on some rare systems.
But if you are on a system where multiple of 8-bits 2's complement types are defined (most modern systems), and uint32_t exists, then this creates 32-bit constant.
They all are defined in stdint.h, and have been part of the C standard since C99.
UINT32_C is a macro for writing a constant of type uint_least32_t. Such a constant is suitable e.g. for initializing an uint32_t variable. I found for example the following definition in avr-libc (this is for the AVR target, just as an example):
#define UINT32_C(value) __CONCAT(value, UL)
So, when you write
UINT32_C(25)
it's expanded to
25UL
UL is the suffix for an unsigned long integer constant. The macro is useful because there is no standard suffix for uint32_t, so you can use it without knowing that on your target, uint32_t is a typedef e.g. for unsigned long. With other targets, it will be defined in a different way.
These constants are defined something like this:
#define UINT32_C(value) (value##UL)
You can only put constant values as macro argument, otherwise it wont compile.
UINT32_C(10); // compiles
uint32_t x = 10;
UINT32_C(x); // does not compile
Don't know about keil, but at least in Linux UINT32_C is a macro to create a uint32_t literal.
And as mentioned by others, uint32_t is a type defined as of C99 in stdint.h.
It is the macro appending the suffix creating the literal for example #define UINT32_C(c) c##UL

Defining constant-variables with a C macro

I am trying to define a 'variable' with a macro which can later be used as a constant value ...I have now run out of ideas and wondering if anyone can tell me if I've missed anything:
This is what I originally coded, but C does not recognise 'name' as a constant
qv. compile error C2099: initializer is not a constant
#define DECL(name,value) static const unsigned long int name = value
DECL(FOO, 0xFFFFFFFF)
My next attempt was to write a macro that would #define the value for me ...But the compiler complains about the embedded # : "error: '#' is not followed by a macro parameter"
qv. Escaping a # symbol in a #define macro?
#define DECL(name,value) #define name (value)UL
DECL(FOO, 0xFFFFFFFF)
At ^-this-^ link, ybungalobill claims v-this-v works ... but it doesn't
#define DECL(hash, name, value) hash define name (value)UL
DECL(#, FOO, 0xFFFFFFFF)
I managed to get my code to compile with this enum trick (inspired by the first link) ...it works on my computer, but as this issue is ultimately about creating portable code [portable between languages as well as platforms!] I am worried about the sizeof and signed'ness of the value. qv. What is the size of an enum in C?
#define DECL(name,value) enum { name = value } ;
DECL(FOO, 0xFFFFFFFF)
Here are some lines of code which may use the above FOO value:
int array[FOO];
static const unsigned long int BAR = FOO + 1 ;
int main (void) {
unsigned long int var1 = FOO;
printf("%d - %d\n", FOO, sizeof(FOO));
}
A macro definition cannot include preprocessor directives, so you can't #define something that expands to another #define.
The const keyword in C doesn't mean "constant"; it means "read-only". A "constant" expression is one that can be evaluated at compile time. A const object is one whose value may not be modified. For example:
const int r = rand();
r is const, meaning you can't modify it after it's been initialized, but it's not "constant" (it can't be, since the value returned by rand() can't be determined until run time).
Using a constant expression as the initializer doesn't change this. (It does in C++.)
The usual way to define a named constant in C is to define it directly as a macro. For example, rather than
#define DECL(name,value) #define name (value)UL
DECL(FOO, 0xFFFFFFFF)
just write:
#define FOO 0xFFFFFFFFUL
For the special case of constants of type int, you can use an enum declaration:
enum { max = 1000 };
This defines an anonymous enumeration type and a constant max of that type. max is a constant of type int. It's defined that way for historical reasons; it would probably make more sense if max were of the enumeration type (as it is in C), but it isn't.
Your issue is not related to macros in any way.
I am trying to define a 'variable' [...] which can later be used as a constant value
You cannot do this in the sense that this variable can be use as an initialiser.
It would stay a variable.
For further reading: Error "initializer element is not constant" when trying to initialize variable with const

When to use #define or constant char/int?

In general, is it better to define some specific parameters (e.g. (char *) UserIPaddr="192.168.0.5" , (int) MAX_BUF=1024) by #define or constant char */ int?
I read some threads say that it is better not to use #define when it is possible. However, I see quite common usage of #define on open source codes one example from a source code:
#define IEEE80211_WLAN_HDR_LEN 24
a_uint8_t *iv = NULL;
a_uint16_t tmp;
a_uint16_t offset = IEEE80211_WLAN_HDR_LEN;
#define could be avoided to use there, but I wonder why it was preferred to use #define on that case for example. How should I decide when to use #define or not?
In C const declarations do not produce constant expressions, So if you need to have a constant expression its not possible using const, the traditional and more commonly used way to do so is using # define.
For example const int cannot be used in:
a case label or
as a bit-field width or
as array size in a non-VLA array declaration (pre C99 days)
There are few reasons to use #define. There is little it accomplishes that a static const or enum cannot.
As Alok Save mentions, static const int cannot produce an integral constant expression in C (I'm not double checking the C standard; it isn't the case in C++ though). But enum can do that. However enum in pure C does not grow to accommodate values larger than INT_MAX. So if you need a long value to use as an array bound or case label, #define is your friend. Or consider switching to using the C subset of C++, which doesn't have such restrictions.
My rule of thumb is to not use #define unless the symbol must be a compile-time constant. With this in mind, I personally would not have used #define in your example.
To take a different example from the same source file:
#define CRYPTO_KEY_TYPE_AES 2
...
switch (keytype) {
case CRYPTO_KEY_TYPE_AES:
Here, CRYPTO_KEY_TYPE_AES must be a constant expression, and thus using a constant variable would not do.

#define SOMETHING of type int16_t

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 typedefs 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.
http://en.wikipedia.org/wiki/Stdint.h
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)

what does the * mean in c

This is a C language question.
Does the * mean multiply or something else in function below? The reason I ask is because the function definition comments says it expects three params. Also do the () [parenthesis] in the #defines mean something different from no parenthesis? See below.
The function call:
nvm_eeprom_write_byte(TEST_ERASE_PAGE * EEPROM_PAGE_SIZE, 42);
The definitions:
#define TEST_ERASE_PAGE 2
#define EEPROM_PAGE_SIZE 32
Comments for the function definition:
param page_addr EEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGE_SIZE.
param byte_addr EEPROM Byte address, between 0 and EEPROM_PAGE_SIZE.
param value Byte value to write to EEPROM.
The function definition:
void nvm_eeprom_write_byte(eeprom_addr_t address, uint8_t value) {}
eeprom_addr_t is a typedef:
typedef uint16_t eeprom_addr_t
#define EEPROM_START (0x0000)
#define EEPROM_SIZE (2048)
#define EEPROM_PAGE_SIZE (32)
#define EEPROM_END (EEPROM_START + EEPROM_SIZE - 1)
Yes, it just means multiply in this context. It's multiplying two #defined constant to make the first argument to the nvm_eeprom_write_byte function.
This code involves a lot of assumptions about memory address manipulation. Gotta be honest with you, if you don't know C, looking at EEPROM driver code probably isn't the simplest or safest way to start.
Yes, * means multiply in C.
Parenthesis in #define is a standard practice in C to prevent unexpected results when using compound statements (where operator precedence matters).
Consider the difference between
#define FOO 1+2
int a = FOO*2
and
#define FOO (1+2)
int a = FOO*2

Resources