Hi, I'm having a minor problem with output with printf. I understand you escape % in printf with %% so if I had some string it would be

printf("%%eax");

But if I have

printf("%%%s", registers(C));

I get an an output of %%eax, which is one too many percent signs. I tried it with just %%s but I'm stil getting an error. Is there any way around this? Thanks.

Recommended Answers

All 4 Replies

What compiler are you using?
Seeing as printf("%%%s", registers(C)) isn't working as expected, you may need to resort to a workaround like printf("%s%s", "%", registers(C))

I'm using C90 if that's what you mean.
I tried your solution, but it still gave me a weird answer.

registers(d) returns a char* so it returned %ebp

So the actual answer it gave me was:
%%ebp %ebp

Which should just be
%ebp

EDIT:
Sorry I'm an idiot. It didn't output %%ebp %ebp
But it still returns %%ebp

Can you provide us with the code you have for the registers() function? Perhaps the reason you're getting an extra percent sign is because your function is tacking it on automatically. If it's not, I would have a reason to believe there's something wrong with your compiler because when I do printf("%%%s", "eax"), I'm getting the expected output of "%eax".

Also, C90 isn't a compiler, it's a language standard from around 1990. We're on C11 now.

registers(d) returns a char* so it returned %ebp

It could well be as Tumlee suggested.
If registers(d) is already returning %ebp, then you only need printf("%s", registers(d))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.