Friday, March 18, 2011

\xhh hexadecimal number

lets understand the use of \xhh escape sequence with the help of some interesting examples
here hh is one or more hexadecimal digits(0....9,a...f,A...F).......firstly understand the meaning of one or more characters...it means there can be any no. of hexadecimal digits after \x.....

1.
int main()
{
char *s="A\x3a";
printf("%s",s);
return 0;
}

Output:
A:

Explanation:
here '\x3a' is a hexadecimal number n it is a single char....... firstly it will get converted in to decimal notation i.e. 58 and it is the ASCII value of char ':' hence at the place of \x3a there is :........................ so the output is A:............

2.
int main()
{
char *s="A\x003a";
printf("%s",s);
return 0;
}

Output:
A:

Explanation:
decimal notation of 003a is 58.................here i m trying to show there can b any no. of hexadecimal digits after \x...............

3.
#define new '\xa'
int main()
{
printf("A%c",new);
return 0;
}

Explanation:
here the output is A n the cursor is in the next line bcoz as we know the ascii value of new line character is 10.................so very interesting there is one more escape sequence to do the work of new line character..................