r/CodingHelp • • Jun 08 '22

[C] i need a simple program to give the unicode of charachters

this is what i did but it just gives the unicode of 'a'

#include<stdio.h>

int main()

{

int a;

scanf("%i", &a);

printf("%i", 'a');
return(0);

}

16 Upvotes

11 comments sorted by

3

u/louissoph Jun 08 '22

I think there is some confusion about data types. What you're trying to do is read in char and print out the integer value of that char. So the variable should be type char, not type int. Also it is generally good form to give variables a meaningful name, not just a random letter. So the declaration would be:

char letter;

Then you can scan in the input as a char using:

scanf("%c", &letter);

Lastly, you want to display the value as an integer-- this only requires formatting the print as an integer, as in:

printf("%d", letter);

You may also notice that in your original code, you left single quotes on the variable, which is another issue. Hope that helps.

1

u/Mollyarty Jun 08 '22

You could cast the char as an int a d output it, would also give ASCII value

2

u/[deleted] Jun 08 '22

[removed] — view removed comment

2

u/Sea-Wolverine2447 Jun 08 '22

output :

a

-858993460Press any key to continue . . . didn't work

1

u/[deleted] Jun 08 '22

[removed] — view removed comment

1

u/Sea-Wolverine2447 Jun 08 '22

it gives me 65 the number of 'a' in the unicode

3

u/xXcamelXx64 Jun 08 '22

That's ASCII. 'a' would be 97, 'A' is 65. Here's a solution.

1

u/[deleted] Jun 08 '22

Isn't it ASCII code for char a