General discussion

Locked

This question is releated to C Langauge

By ms_saini3302 ·
What is the answer of the following and how it is comming:-

#include<conio.h>
#include<stdio.h>
main()
{
char a='\101';
printf("%c",a);
}

This conversation is currently closed to new comments.

9 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

This question is releated to C Langauge

by epepke In reply to This question is releated ...

That should print a capital A to standard output. \101 means octal 101 which means hexadecimal 41 which maps to capital A in ASCII. The %c code in printf prints a single character.

Note that %c only works safely with single-byte characters, i.e. ASCII. It isn't safe to use on a multi-byte character set such as Unicode.

Collapse -

This question is releated to C Langauge

by ms_saini3302 In reply to This question is releated ...

Ok you told me '\101' is taken as octal value what why it is taking as octal value why it is not taking decimal value can any one explain me.

Collapse -

This question is releated to C Langauge

by ms_saini3302 In reply to This question is releated ...

Point value changed by question poster.

Collapse -

This question is releated to C Langauge

by emcs In reply to This question is releated ...

I copied:

#include<conio.h>
#include<stdio.h>
main()
{
char a='\101';
printf("%c",a);
}

from your question.

The answer is A.
102 = B
103 = C
104 = D
105 = E
106 = F
107 = G
110 = H
111 = I
etc
117 = O
120 = P

As was stated in #1, the \ in '\101' indicates octal. '\100' = @.

This program is taking any the number, and giving the user friendly key notation. Remember that octal's do not always give A-Z, a-z, 0-9, one of the other symbols like@, ^, &. It may return the LF, VT, or any the remainding commands.

What else are you looking for in this program? It really is doing what you have asked it to do.

Let me know if this helps or hurs.

dennie

Collapse -

This question is releated to C Langauge

by emcs In reply to This question is releated ...

As you can see in the above: helps or hurs. Spelling doesnot count.

I did see where you asked this a couple of time. I again ask as someone else said, what do you think is wrong, or where you just not clear with what the output should be?

Let us know what is confusing you, someone can help.

dennie

Collapse -

This question is releated to C Langauge

by ms_saini3302 In reply to This question is releated ...

Poster rated this answer

Collapse -

This question is releated to C Langauge

by epepke In reply to This question is releated ...

It's taking an octal value because that's the way the language is defined. Backslash followed by three digits in a quoted character or string MEANS take the octal value.

The question "why" is almost meaningless. It works that way because, a quarter of a century ago, the designers of the language decided that's what it should mean. Perhaps they were used to specifying characters in octal, and they wanted to do that in their new language. It's just like the fact that preceding an integer with a 0 outside of a string means use octal. So, a=0101 would set a to octal 101, which is decimal 65.

Why? Because that's how they defined it. It's arbitrary, but that's the way it is. They could have chosen to make it decimal, but they didn't, and we just have to live with the fact.

Collapse -

This question is releated to C Langauge

by ms_saini3302 In reply to This question is releated ...

Poster rated this answer

Collapse -

This question is releated to C Langauge

by ms_saini3302 In reply to This question is releated ...

This question was closed by the author

Back to Web Development Forum
9 total posts (Page 1 of 1)  

Related Discussions

Related Forums