6

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?

user229044
  • 232,980
  • 40
  • 330
  • 338
Khushboo
  • 285
  • 3
  • 5
  • 12

6 Answers6

9

Instead of printf("%c", my_char), use %d to print the numeric (ASCII) value.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 10
    @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 Aug 11 '10 at 16:30
  • 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 Aug 11 '10 at 16:38
  • 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 Aug 11 '10 at 16:45
  • 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 Aug 11 '10 at 16:56
  • @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 Aug 11 '10 at 16:57
  • @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 Aug 11 '10 at 16:59
  • @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 void main() { char ch; printf("Enter a character: "); scanf("%c",&ch); printf("%d", ch); } – Khushboo Aug 11 '10 at 17:03
  • 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.. GitHub STOP HELPING ICE Aug 11 '10 at 17:27
  • 2
    @user417316: I think you're reading the newline from the end of a line... – R.. GitHub STOP HELPING ICE Aug 11 '10 at 17:28
  • 1
    @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 Aug 11 '10 at 21:18
  • @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 Aug 12 '10 at 00:07
  • 1
    @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 Aug 12 '10 at 06:28
7

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 unsigned 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.

Community
  • 1
  • 1
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • You need to cast `c` to `(unsigned char)`, otherwise it may be promoted to `int` (which doesn't match `%u`). – caf Aug 12 '10 at 00:08
  • @caf: I don't follow, isn't it going to cast during conversion? – Matt Joiner Aug 15 '10 at 11:43
  • 2
    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 Aug 15 '10 at 13:11
2

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
  • 92,053
  • 36
  • 243
  • 426
1

The code printf("%c = %d\n", n, n); displays the character and its ASCII.

MELWIN
  • 1,093
  • 4
  • 12
  • 19
0

This will generate a list of all ASCII characters and print it's numerical value.

#include <stdio.h>
#define N 127

int main()
{
    int n;
    int c;

    for (n=32; n<=N; n++) {
        printf("%c = %d\n", n, n);
    }

    return 0;
} 
kyle k
  • 5,134
  • 10
  • 31
  • 45
-1
 #include "stdio.h"
 #include "conio.h"
 //this R.M.VIVEK coding for no.of ascii values display and particular are print

void main()
{
    int rmv,vivek;
    clrscr();

    for(rmv=0;rmv<=256;rmv++)
    {
        if(printf("%d = %c",rmv,rmv))
    }

    printf("Do you like particular ascii value\n enter the 0 to 256 number");
    scanf("%d",&vivek);
    printf("\nthe rm vivek ascii value is=%d",vivek);
    getch();
}
  • `conio.h` is nonportable and not worth using here, `main()` returns `int`, and you're not going to get anything good from trying to print a `char` from the `int` value `256`. – underscore_d Nov 03 '17 at 09:28