printf with uint32_t gives strange error messages/warnings

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
RaAl_264636
Level 6
Level 6
50 sign-ins 25 sign-ins 10 solutions authored

Hi,

I'm using printf with a variable of type uint32_t. Variables of this type have a fixed with of 32 bits, so this equals to long int in C. Is this true?

So, for printf, the correct format string is:

printf("%lu", Var);     //%lu => long unsigned

Now, it's getting strange: If I use %lu, I get no error in the notice list after a build, but a error indicator in the corresponding line of the source editor window, saying "format specifies type 'unsigned long', but the argument has type 'uint32_t' (aka 'unsigned int')".

If I use %u, the error indicator in the source editor window wents away, but after a build a warning is shown in the notice list: "format '%u' expects argument of type 'unsigned int', but argument 2 has type 'uint32_t {aka long unsigned int}"

This is confusing. I know I can use

printf("%lu", (long int)Var);

which eliminates both error indicators, but I wonder why there are errors different positions (source code window vs. notice list), and why %lu gives an error - %lu should be right IMHO.


Is the behaviour regarding the error messages a bug of PSoC Creator? Should I file a case about it?

Regards,

Ralf

0 Likes
1 Solution

Try using uint32 instead of uint32_t, you should not see any warnings.C_var.png

View solution in original post

0 Likes
7 Replies
lock attach
Attachments are accessible only for community members.
AnkitaS_51
Employee
Employee
100 likes received 50 likes received 25 likes received

I tried what you mentioned by writing this code in main.c-

uint32_t var=0x00023;

    CyGlobalIntEnable; /* Enable global interrupts. */

printf("%lu",var );

But I didn't got any error indicator ( screenshot attached ) in PSoC Creator4.1 update1.

Can you please provide the project in which you are observing the issue.

0 Likes
lock attach
Attachments are accessible only for community members.

Hi Anks,

you missed to include <stdio.h>. Please try it with including this file. I attached screenshots of the behaviour, one with %u and one with %lu, pictures are taken after rebuild.

Regards,

Ralf

0 Likes

Try using uint32 instead of uint32_t, you should not see any warnings.C_var.png

0 Likes

Hi srds,

thank you, I'll try it. So, this issue is because the IDE is not capable of determining the bit width of uint32_t for the printf arguments?

Regards

0 Likes

Note that it is a diagnostic warning in the code editor (since the typedef are different) and not a compiler generated warning. So, both uint32 and uint32_t functionally work the same way.

0 Likes

even with uint32 i am getting warning in editor to change to %u from %lu, but if i do that then compiler gives warning.

0 Likes

If you are using PSoC 6 MCU, try this:

printf("Value of Val: %lu\r\n", (unsigned long)Val);