Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C function printf() limited in length?

Tags:

c

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.

like image 914
Jelco Adamczyk Avatar asked Jun 23 '26 23:06

Jelco Adamczyk


1 Answers

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);
like image 125
Mike Avatar answered Jun 25 '26 14:06

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!