2

Upon calling the sprintf function to format a string, something unexpected was printed

printf("%d;%s;%s;%d;%d;\n", ptr->programmaType, ptr->titel, ptr->zender, ptr->start, ptr->einde);

prints "0;Stargate;scifi;0;0;" while

printf("%d;", ptr->einde);

prints "42", which is the value of ptr->einde. I've also tried

printf("%d;%s;%s;%d;%d;", 0, "Stargate", "scifi", 0, 42);

which prints correctly, so I'm guessing the problem is related to the variables. Last thing I tried was

int bug = ptr->einde;
printf("%d;%s;%s;%d;%d;\n", ptr->programmaType, ptr->titel, ptr->zender, ptr->start, bug);

which also failed to print correctly... I have no idea what the hell is happening.

Note: ptr->start and ptr->einde are defined als time_t types, but seeing as printf works with a single argument I doubt that's a problem.

3 Answers3

8

You said that ptr->start and ptr->einde are time_t type. This type is not not fully portable and can be any "integer or real-floating type", so it's possible that they are not being handled correctly on your system when you try to print them as int types.

Try casting it to something known and then printing:

printf("%d;%s;%s;%ld;%ld;\n", ptr->programmaType, ptr->titel, 
    ptr->zender, (long)ptr->start, (long)ptr->einde);
Mike
  • 47,263
  • 29
  • 113
  • 177
  • This works! I am still puzzled as to why printf("%d", ptr->einde) does print "42", but at least I've got something working now. – Jelco Adamczyk Mar 05 '13 at 12:41
  • Check out pmg's comments. It's UB. UB is always puzzling. ;) – Mike Mar 05 '13 at 12:45
  • This is what probably happens: If `ptr->start` is a 64 bit value and if `int` is 32 bits, then the last two `%d` specifiers will print the high 32 bits and the low 32 bits of `ptr->start`. – Klas Lindbäck Mar 05 '13 at 13:10
1

I guess your definition of ptr->einde is not of type int.

And you are using "%d" in the printf specifier which expects an int value.
Use the correct specifier for the correct type and printf behaves.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • I initialised an int to 53, and replaced the time_t with this integer. It still prints this int as "0" in the long printf, while it prints correctly if I call printf("%d", int) – Jelco Adamczyk Mar 05 '13 at 12:36
  • @JelcoAdamczyk – that’s because there are 2 `time_t`s in your long call, if you replaced both `ptr->start` and `ptr->einde` to `int` you wouldn't see it acting strangely – Mike Mar 05 '13 at 12:43
  • 1
    Yep, using the wrong match for specifier and value invokes Undefined Behaviour. You don't want **UB** in your programs. Always correctly match the specifier and the values (with casts if needed). – pmg Mar 05 '13 at 12:44
0

No printf is not limited in length, except in embedded implementations where it must be able to print at least 4095 chars.

time_t is probably a 64 bit integer value. The solution is to cast your variable to (uint64_t) and using PRIu64 format specifier.

printf("%d;%s;%s;%"PRIu64";%"PRIu64";\n", 
           ptr->programmaType, 
           ptr->titel, ptr->zender, 
           (uint64_t)ptr->start, 
           (uint64_t)ptr->einde);
Patrick Schlüter
  • 11,394
  • 1
  • 43
  • 48