tags:

views:

116

answers:

3

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
+1 that was fast.
karlphillip
@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
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
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
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
@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
@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
@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
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..
@user417316: I think you're reading the newline from the end of a line...
R..
@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
@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
@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
+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
You need to cast `c` to `(unsigned char)`, otherwise it may be promoted to `int` (which doesn't match `%u`).
caf
@caf: I don't follow, isn't it going to cast during conversion?
Matt Joiner
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
+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
thank you for the code !
Khushboo