Sunday, July 12, 2009

What is the output of the C syntax given below? & most importantly how?

char a ='\123';


printf("%c",a);

What is the output of the C syntax given below? %26amp; most importantly how?
The syntax is for an octal (radix 8) value. In this case octal 123 or decimal 83. Octal can be represented in C and C++ either by a leading zero or an escape sequence followed by one to three octal digits (0-7). (0123 or '\123' are both octal)





Knowing this, a simple look up in an ascii table (if you need to) will tell you that the result is 'S'. The printed value being without the single quotes. I'm sorry that I had to answer this homework problem as I usually don't but I couldn't let the incorrect answers stand.
Reply:First, it won't compile. \, the escape character, doesn't have a corresponding escape code for 1. Let's say the compiler ignores it, or doesn't use escape characters for single quoted characters, then '\' will print. I don't think you can enclose multiple characters in single quotes though, so again it probably won't compile.
Reply:The ascii value for 123 is the character {





printf %c will print the character value in a which is {


No comments:

Post a Comment