I have accepted a character as an input from the user. I want to print the ASCII value of that character as an output. How can I do that without using any pre-defined function (if it exists) for the same?
+4
A:
Instead of printf("%c", my_char)
, use %d
to print the numeric (ASCII) value.
Mark Rushakoff
2010-08-11 16:25:46
+1 that was fast.
karlphillip
2010-08-11 16:27:02
@Karl: Rumor has it that everyone with a rep score over 10k has been abducted and replaced by an AI. Except Jon Skeet, who was an AI all along.
Steven Sudit
2010-08-11 16:30:06
Thank you for the solution. I tried using this method. It is not giving me any error but it's displaying the value 10 for any character inputed. So I'm trying to figure out the problem.
Khushboo
2010-08-11 16:38:09
fast but wrong? to my understanding the ascii value of a character is an unsigned entity. So %u would have been better, I think. So I prefer Matt's answer, 4 min more, but well invested ;-)
Jens Gustedt
2010-08-11 16:45:38
I guess Mark's answer is the best solution bcoz' a few more programmers have given me the same solution but I think there's some problem on my side coz' of which problem is occurring.
Khushboo
2010-08-11 16:56:20
@user: If it's printing out the same value every time, then you're *passing in* the same value each time. Double check that your arguments to `printf` are what you actually meant to pass in.
Mark Rushakoff
2010-08-11 16:57:05
@Jens: [The standard absolutely **does not** specify whether a `char` is signed or unsigned](http://stackoverflow.com/questions/2054939/char-is-signed-or-unsigned-by-default).
Mark Rushakoff
2010-08-11 16:59:41
@Mark: I tried giving 'a' as well as 'A' but its giving the same ASCII value 10 for both which is actually wrong for both. What could be the possible error? Here's the code I'm executing: #include<stdio.h> void main() { char ch; printf("Enter a character: "); scanf("%c", printf("%d", ch); }
Khushboo
2010-08-11 17:03:10
While `char` could be signed or unsigned, I seem to remember the standard requiring that all the basic characters required for the translation/execution character set have positive values... Can anyone confirm?
R..
2010-08-11 17:27:07
@user417316: I think you're reading the newline from the end of a line...
R..
2010-08-11 17:28:14
@Mark: The question was about ASCII values not about the numerical values of a `char`. (I am well aware that `char` can be signed or unsigned.) To my understanding an ASCII value is always positive. Thus I would accept an %u as a solution. But if even more nitpicking you would have had to provide a translation function from the execution character set to ASCII to really have a completely correct solution.
Jens Gustedt
2010-08-11 21:18:53
@Jens: Since ASCII values lie strictly in the range 0 to 127, both `(%d", c)` and `("%u", (unsigned char)c)` will only print positive values for ASCII characters. Characters with negative values in a signed `char` are not in the ASCII set.
caf
2010-08-12 00:07:37
@caf: sure, if you assume that the charset actually *is* ASCII. The C specification does not impose this, so as I said, the answer given here is wrong. I only wanted to make a point that a %u comes closer to what I would accept as a correct answer than %d. BTW, I don't know any quick answer to the real question myself, that is to output the ASCII value of a character, regardless what the execution character set is.
Jens Gustedt
2010-08-12 06:28:35
+3
A:
Also consider printf("%hhu", c);
to precisely specify conversion to unsigned char and printing of its decimal value.
Update0
So I've actually tested this on my C compiler to see what's going on, the results are interesting:
char c = '\xff';
printf("%c\n", c);
printf("%u\n", c);
printf("%d\n", c);
printf("%hhu\n", c);
This is what is printed:
� (printed as ASCII)
4294967295 (sign extended to int)
-1 (sign extended to int)
255 (handled correctly)
Thanks caf for pointing out that the types may be promoted in unexpected ways (which they evidently are for the %d
and %u
cases). Furthermore it appears the %hhu
case is casting back to a char unsigned
, probably trimming the sign extensions off.
Matt Joiner
2010-08-11 16:29:37
You need to cast `c` to `(unsigned char)`, otherwise it may be promoted to `int` (which doesn't match `%u`).
caf
2010-08-12 00:08:25
It's a technicality, but the `%u` specifier (even when used with `hh`) requires a (promoted) `unsigned int` argument, otherwise the behaviour is explicitly undefined. `char` may be promoted to `int` rather than `unsigned int` (since it may or may not be signed), so an explicit cast to `unsigned char` is necessary to ensure it is promoted to `unsigned int`.
caf
2010-08-15 13:11:47
+1
A:
This demo shows the basic idea:
#include <stdio.h>
int main()
{
char a = 0;
scanf("%c",&a);
printf("\nASCII of %c is %i\n", a, a);
return 0;
}
karlphillip
2010-08-11 16:55:20