Posts

Showing posts from October, 2008

Shell text color control from C program

How to control output text color of a shell? Text color output is not defined in ANSI C/C++. Instead the creators of the language left that to be operating system dependent. In Linux, to change text color you must issue what are known as terminal commands. To print "Hello, world.\!" in red, change the printf statement to read:printf("\033[22;31mHello, world!"); \033[22;31m is the terminal command. Note the numneric escape sequence at the start and on an ASCII based machine, octal 33 is the ESC (escape) code. For further details on numneric escape sequence read http://casual-pavan.blogspot.com/2008/10/intersting-things-about-escape.html To try different colors, just change the numbers after the left bracket. \033[22;30m is black \033[22;31m is red \033[22;32m is green Some more colours: \033[22;33m - brown \033[22;34m - blue \033[22;35m - magenta \033[22;36m - cyan \033[22;37m - gray \033[01;30m - dark gray \033[01;31m - light red \033[01;32m - light green \033[01;33...

Intersting things about escape sequences

Escape sequence provides an general and extensible mechanism for representing hard-to-type or invisible characters. In C language escape sequences always begin with a backslash (\). Charater constants represented by escape sequences like '\n' (newline) look like two characters, but represent only one. There are fundamentally two distinct purposes behind the escape sequences. 1. The pricipal purpose of escape sequences is to represent invisible characters which control the motions of a printing device when they are sent to it. e.g. i. \f - Form feed: Go to the first position on the ‘next page’, whatever that may mean for the output device. The Standard carefully avoids mentioning the physical directions of movement of the output device which are not necessarily the top to bottom, left to right movements commonly followed. ii. Escape sequences also ease the way of representing some special characters that would otherwise be hard to get into a character constant (or hard to read; ...

Diff in strcmp and stricmp

strcmp (s, t) compares the strings and returns a negative, zero or positive if s is lexicographically less than, equal to, or greater than t. The value is obtained by subtracting the characters at the first position where s and t disagree. stricmp performs the same function as strcmp, but alphabetic characters are compared without regard to case. That is, "A" and "a" are considered equal by stricmp, but different by strcmp. Examples: a.out>strcmp("apple", "peach"); -15 a.out> strcmp("peach", "apple"); 15 a.out> strcmp("Apple","Apple"); 0 a.out> strcmp("Apple","Apple pie"); -32 a.out> strcmp("Apple","apple"); -32 a.out> stricmp("Apple","apple"); 0