Showing posts with label escape sequence. Show all posts
Showing posts with label escape sequence. Show all posts

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

\ooo octal number

lets understand the use of \ooo escape sequence with the help of some interesting examples
here ooo is one to three octal digits(0....7) means there must be atleast one octal digit after \ and maximum three..........
1.
int main()
{
char *s="A\0655";
printf("%s",s);
return 0;
}

Output:
A55

Explanation:
its very interesting to know the reason behind such output.......here 065 is the octal notation....... firstly it will get converted in to decimal notation i.e. 53 and it is the ASCII value of char '5' hence at the place of \065 there is 5........................ so the output is A55............

okay,lets understand it with 1 more example.....

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

Output:
A<->B

Explanation:
decimal notation of 035 is 29.................n it is the ASCII value of '<->' char.......... hence the output is A<->B........................

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

Output:
A<-A

Explanation:
decimal notation of 33 is 27.................n it is the ASCII value of '<-' char.......... hence the output is A<-A........................it shows octal number can have two octal digits also.............similarly there can be only 1 octal digit.................

try for different strings......its really amazing.................