I want to create a global 22 character (+ NULL) string in C. I am initialising it like this:
#define SVN_REVISION "1143"
#define SERIAL_NUMBER "PST3201109270001"
char general_info[23] = SVN_REVISION SERIAL_NUMBER SAMPLING_FREQUENCY;
So far, this works fine. general_info is initialised with the concatenation of the two strings. Now, for the last two characters, I want them to be a 16-bit integer. But I can’t simply do this:
#define SVN_REVISION "1143"
#define SERIAL_NUMBER "PST3201109270001"
#define SAMPLING_FREQUENCY (UINT16)500
char general_info[23] = SVN_REVISION SERIAL_NUMBER SAMPLING_FREQUENCY SAMPLING_FREQUENCY;
What can I do?
If you mean you want to embed 16-bit integer
500as binary data in the end of the string, you can use raw hex bytes:Beware, though, endianness is architecture dependent, so you might want to add an extra macro to correctly order the bytes depending on the architecture:
EDIT: If you want to have a human-readable number, you can’t have it at compile-time (at least not in C), but you can initialize it at runtime:
This uses more non-standard (but well-supported) constructs, like
#pragma packand writing to one union member and reading from another.