0
sprintf(xBuffer,"%l",rootCause)

rootCause is unsigned long

bad_printf_format_string:invalid format string conversion

sprintf(buffId,"Ox%lx Ox%lx Ox%lx", canSnifferMsg.nodeId, canSnifferMsg.index, canSnifferMsg.subindex);

what about this? nodeId, index, subindex all are longs

mch
  • 9,424
  • 2
  • 28
  • 42
niketa
  • 37
  • 5

1 Answers1

2

The correct format string for unsigned long is %lu.
A list of format specifiers can be found here.
A similar question has been asked here.

%lx is used to get a hex representation of the number. It is therefore irrelevant if it is unsigned or signed as 2's-complement is used instead of a negative sign.

If you want to print a regular long number you can use either %ld or %li as specified on the first link I sent.

Example:

unsigned long a = (unsigned long)(-25);
char buff[100];

sprintf(buff, "%lu %lx %ld", a, a, a);

printf("%s", buff);

Output:

4294967271 ffffffe7 -25

FalcoGer
  • 2,278
  • 1
  • 12
  • 34
  • sprintf(buffId,"Ox%lx Ox%lx Ox%lx", canSnifferMsg.nodeId, canSnifferMsg.index, canSnifferMsg.subindex); what about this...nodeId ,index,subindex all are long – niketa Apr 12 '19 at 06:29
  • @niketa That is for hexadecimal output. Taking a closer look at `printf` man page would be a good starting point. – Gerhardh Apr 12 '19 at 06:53
  • "It is therefore irrelevant if it is ... signed as 2's-complement is used instead of a negative sign". --> Disagree: Printing a `long` with a negative value using `"%lx"` is undefined behavior UB. Printing an `unsigned long` with a value > `LONG_MAX` using `"%ld"` is also UB. I have never seen this UB to be a problem though. – chux - Reinstate Monica Apr 12 '19 at 13:49
  • @chux i have considered this a cast. – FalcoGer Apr 15 '19 at 10:15