feos
feos

Reputation: 1116

sprintf() - left-aligning several units within one placeholder

I'm decoding cpu opcodes and their arguments that are separated by a placeholder.

This works perfectly with one left-side unit by providing myop a0

sprintf(dest, "%-16s%s", opcode, arg);

But when I have opcodes that consist of several units like this myop.w.s a0 where the first 3 units need to be on the left side of the placeholder, and the arg goes after it, what is the way to have them behave like here

sprintf(dest, "%s.%s.%s\t\t%s", opcode, param1, param2, arg);

but without the use of tabs?

There's also a way to do sprintf() twice, at first I create a combined string "myop.w.s" and then during the second sprintf() I pad it, but I'm curious if there are other solutions.

Upvotes: 1

Views: 99

Answers (1)

chux
chux

Reputation: 153458

Print the first 3 strings and the spacing as needed

snprintf(dest, 16, "%s.%s.%s%16s", opcode, param1, param2, "");
strcpy(dest + 16, arg);

Upvotes: 2

Related Questions