printf and escape sequances
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > printf and escape sequances




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    printf and escape sequances  
Schüle Daniel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-23-06 12:19 AM

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;
}





[ Post a follow-up to this message ]



    Re: printf and escape sequances  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-23-06 12:19 AM

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





[ Post a follow-up to this message ]



    Re: printf and escape sequances  
Schüle Daniel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-24-06 12:21 AM

> [...]
> 
>
> 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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:12 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register