I am trying to print an uint16_t and uint32_t value, but it is not giving the desired output.
#include <stdio.h> #include <netinet/in.h> int main() { uint32_t a = 12, a1; uint16_t b = 1, b1; a1 = htonl(a); printf("%d---------%d", a1); b1 = htons(b); printf("\n%d-----%d", b, b1); return 0; }
I also used
printf("%"PRIu32, a);
which is showing an error.
How do I print these values and what will be the desired output?
uint32_t is a numeric type that guarantees 32 bits. The value is unsigned, meaning that the range of values goes from 0 to 232 - 1. This. uint32_t* ptr; declares a pointer of type uint32_t* , but the pointer is uninitialized, that is, the pointer does not point to anywhere in particular.
uint16_t is unsigned 16-bit integer. unsigned short int is unsigned short integer, but the size is implementation dependent. The standard only says it's at least 16-bit (i.e, minimum value of UINT_MAX is 65535 ).
You need to include inttypes.h
if you want all those nifty new format specifiers for the intN_t
types and their brethren, and that is the correct (ie, portable) way to do it, provided your compiler complies with C99. You shouldn't use the standard ones like %d
or %u
in case the sizes are different to what you think.
It includes stdint.h
and extends it with quite a few other things, such as the macros that can be used for the printf/scanf
family of calls. This is covered in section 7.8 of the ISO C99 standard.
For example, the following program:
#include <stdio.h> #include <inttypes.h> int main (void) { uint32_t a=1234; uint16_t b=5678; printf("%" PRIu32 "\n",a); printf("%" PRIu16 "\n",b); return 0; }
outputs:
1234 5678
The macros defined in <inttypes.h>
are the most correct way to print values of types uint32_t
, uint16_t
, and so forth -- but they're not the only way.
Personally, I find those macros difficult to remember and awkward to use. (Given the syntax of a printf
format string, that's probably unavoidable; I'm not claiming I could have come up with a better system.)
An alternative is to cast the values to a predefined type and use the format for that type.
Types int
and unsigned int
are guaranteed by the language to be at least 16 bits wide, and therefore to be able to hold any converted value of type int16_t
or uint16_t
, respectively. Similarly, long
and unsigned long
are at least 32 bits wide, and long long
and unsigned long long
are at least 64 bits wide.
For example, I might write your program like this (with a few additional tweaks):
#include <stdio.h> #include <stdint.h> #include <netinet/in.h> int main(void) { uint32_t a=12, a1; uint16_t b=1, b1; a1 = htonl(a); printf("%lu---------%lu\n", (unsigned long)a, (unsigned long)a1); b1 = htons(b); printf("%u-----%u\n", (unsigned)b, (unsigned)b1); return 0; }
One advantage of this approach is that it can work even with pre-C99 implementations that don't support <inttypes.h>
. Such an implementation most likely wouldn't have <stdint.h>
either, but the technique is useful for other integer types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With