Skip to main content
  1. About
  2. For Teams
Asked
Viewed 4k times
-4
#include<stdio.h>
#include<conio.h>
main()
{
    int i=369;
    printf("%c",i);
    getch();
}



O/p
===
q

Here , getting 'q' as a output .

My doubt is the ascii character limit is 255 . But why it is giving 'q' as output ?

2
  • 5
    Well, if you look at a ascii chart: asciitable.com You will see the limit is 128.
    Scotty Bauer
    –  Scotty Bauer
    2013-12-12 16:46:39 +00:00
    Commented Dec 12, 2013 at 16:46
  • 2
    @Scotty Bauer I thought the limit was 127.
    chux
    –  chux
    2013-12-12 16:51:51 +00:00
    Commented Dec 12, 2013 at 16:51

3 Answers 3

8

It's being cast to a byte. 369 % 256 = 113 = 'q'

Sign up to request clarification or add additional context in comments.

Comments

5

The "%c" in printf() takes the int parameter 369 and converts it to an unsigned char which has the value 369 & 255 or 113. Character code 113 corresponds to 'q' on a system using ASCII encoding. Thus 'q' is printed.

C11dr §7.21.6.1 8 c conversion specifier
"If no l length modifier is present, the int argument is converted to an unsigned char, and the resulting character is written."


[Edit]
Typical C systems have an 8-bit char which allows for 256 combinations, hence the above & 255 (Some systems have other char sizes). Typical C systems assign values 0 to 127 to the ASCII character set - which is only defined for codes 0 to 127. The text that may print out with values outside that range varies.

Comments

0

ascii's limit is 255, however 369=0000 0001 0111 0001, while q's ascii code is 0111 0001, see the last 8 bits? You got it!

2 Comments

Typical UCHAR_MAX is 255 and that why the 369 is truncated to 8 bits. ASCII range is 0 to 127.
To be more precise, the extended ASCII is 0-255

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.