|
Home > Archive > Unix Programming > October 2006 > printf and escape sequances
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
printf and escape sequances
|
|
| Schüle Daniel 2006-10-22, 7:19 pm |
| Hello NG,
regarding the code below
// not working
printf("wire %s[%10.10s] %scurrent=%li%s\tdelta=%li\n",
makeColor(green, black, bold), self->name,
makeColor(red, black, bold), self->currentVal,
makeColor(normal, black, none), self->deltaVal);
// working
printf("wire %s[%10.10s]", makeColor(green, black, bold), self->name);
printf("\t%scurrent=%li", makeColor(red, black, bold), self->currentVal);
printf("%s\tdelta=%li\n", makeColor(normal, black, none), self->deltaVal);
it's interesting to note that the later works while the first do not
meaning that the first changes the color to green and gets stuck at it
so that all messages are in green
can someone explain this behaviour?
it doesn't seem that printf need "\n" to "evaluate" the escape sequence
I tried to put it between makeColor calls without success
Regards, Daniel
definition of makeColor function
enum MODE {
none = 0, // all attributes off
bold = 1,
faint = 2,
italic = 3,
underlined = 4,
blink = 5,
rapidBlink = 6,
concealed = 8,
notBold = 22,
notBlink = 25,
notUnderlined = 24,
notInvert = 27,
};
enum COLOR {
black = 0,
red = 1,
green = 2,
yellow = 3,
blue = 4,
magenta = 5,
cyan = 6,
white = 7,
normal = 8,
};
const char * makeColor(enum COLOR foreground, enum COLOR background,
enum MODE mode) {
static char buffer[256]; // we have only one thread
snprintf(buffer, sizeof(buffer), "\033[%i;%i;%im", mode, foreground
+ 30, background + 40);
return buffer;
}
| |
| Måns Rullgård 2006-10-22, 7:19 pm |
| Schüle Daniel <uval@rz.uni-karlsruhe.de> writes:
> Hello NG,
>
> regarding the code below
>
> // not working
> printf("wire %s[%10.10s] %scurrent=%li%s\tdelta=%li\n",
> makeColor(green, black, bold), self->name,
> makeColor(red, black, bold), self->currentVal,
> makeColor(normal, black, none), self->deltaVal);
[...]
> const char * makeColor(enum COLOR foreground, enum COLOR background,
> enum MODE mode) {
> static char buffer[256]; // we have only one thread
The same static buffer is used for all the makeColor() calls, so only
the value written by the last call is seen by printf(). The order of
the calls is undefined.
> snprintf(buffer, sizeof(buffer), "\033[%i;%i;%im", mode,
> foreground + 30, background + 40);
> return buffer;
> }
--
Måns Rullgård
mru@inprovide.com
| |
| Schüle Daniel 2006-10-23, 7:21 pm |
| > [...]
>
>
> The same static buffer is used for all the makeColor() calls, so only
> the value written by the last call is seen by printf(). The order of
> the calls is undefined.
thank you
i do know that the evaluation order is undefined
but in this case i never thought of that
Regards, Daniel
|
|
|
|
|