Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
printf( format, 123 )
how to print 123 out as 12.3 if I can only change the format part?
Posted
Comments
Ed Nutting 30-Apr-11 8:07am    
This is a poor question. You need to give more explaination/background information as to why you have this problem so that people can solve it for you properly rather than speculating about solutions and explaining some of those speculations (as NuttingCDEF did.) For better responses, please imropve your question (don't post improvements as solutions!)
[no name] 1-May-11 8:49am    
I think it is very advanced problem.
And why not try my code?

This is very interesting problem.

How about this?

You can do that perfectly.
eg:
printf(setformat(),123);

then you can see the result of "12.3"
Please test below code.

int faddr;
char* pempty;
__declspec (naked) char* setformat()
{
_asm mov eax , [esp + 04h];
_asm mov faddr, eax;
_asm pushad;
pempty = (char*)malloc(1);
*pempty = 0;
printf("%d.%d", faddr/10, faddr%10);
_asm popad;
_asm mov eax, pempty;
_asm ret;
}
int main(int argc, char* argv)
{
printf(setformat(),123);
return 0;
}


I think that is perfect answer for your problem.
 
Share this answer
 
v4
Comments
NuttingCDEF 2-May-11 13:46pm    
I haven't tested this, but clearly this (or some variant using assembly lamguage to manipulate the stack / format string / arguments) might well work. The main questions I have are how dependent is the code on the particular platform / hardware the application is being developed for is this code? Is the questioner targeting an Intel processor or something else? How dependent is this on a particular compiler / configuration (e.g. issues of calling conventions, integer sizes etc.)? What chance is there of interactions with things like compiler optimisations causing problems? To get a practical solution, all these need to be at least considered. A subsidiary question is whether, if the questioner can't adapt the printf line of code to implement a solution along the lines I have suggested, what chance is there of being able to insert a function call instead of a string in that code?
There is no format flag to do that.
int x=123; printf("%i.%i",x/10,x%10);
Regards.
 
Share this answer
 
Comments
Nish Nishant 30-Apr-11 15:42pm    
My vote of 5!
Why can you only change the format part? Do you not have any control at all over the code with the printf in it? If not, you are probably stuck unless you can do a subsequent edit on the stream it is printing to. If you can alter the printf statement / code it is in:

1. Convert to double / divide by 10 / print the double, or

2. Leave as integer, divide by 10 to get 12 - print that - take 10 times 12 away from 123 to get 3 - and print that.
 
Share this answer
 
Comments
Nish Nishant 30-Apr-11 15:42pm    
My vote of 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900