954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to find ASCII value of character in c

Plz..guys i want to know is there any function that can give ASCII value of character? and do i need to use any header file to use that function

sargarpramod
Newbie Poster
4 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Here is sample code, which demonstrates on how to get a the ASCII code for the entered char.

#include <stdio.h>

#define TRUE      1

void clear_buffer( void )
{
     int ch;
     
     while( ( ch = getchar() ) != '\n' && ch != EOF );
} 

int main()
{
    unsigned int ch;
    
    while( TRUE )
    {
        printf("\nsingle char please - " ); 
        ch = getchar();
        clear_buffer();
           
        printf("%c - %d", ch, ch );
    } 
}    

/* my output
single char please - a
a - 97
single char please - s
s - 115
single char please - d
d - 100
single char please -
*/


NOTE: This is limitedto quite a lot of stuff. You coud place some error checking to make it more perfect.

And you can always find a ascii char chart on the net. Here is link for one which is found ASCII .

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

In C, you can use the char data type just like a number and it will automatically convert it to the ASCII value. Here's a minimal example:

int myNum = 'a';


myNum would have the value 97 in it.

death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

Thanks guys for replying to my query

sargarpramod
Newbie Poster
4 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

remember this fact:

a variable of type char is just an 8-bit int.

thats all it is, and all it ever was.

try this and see:

int a = 65; printf("%c", a);

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
#include<stdio.h>

void main (void)
{
    char x; 
    printf("\nEnter a Character : ");
    x = getchar();
   printf("\nThe ASCII for Char is: %d",x);
  
 }

This will work , if you need values in Hex then use of "%x" is always there. : )

Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 

>void main (void)
This is not (and never has been) correct C. The main function returns int.

>a variable of type char is just an 8-bit int.
Just to be thorough, even though your reply is over a week old, char is only guaranteed to be at least eight bits.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>void main (void)
#include<stdio.h>
int main ()
{
    char x; 
    printf("\nEnter a Character : ");
    x = getchar();
   printf("\nThe ASCII for Char is: %d",x);
   return 0;/*** was avoiding this line ****/
 }

Thanks anyway ; )

Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 
#include<stdio.h>
int main ()
{
    char x; 
    printf("\nEnter a Character : ");
    x = getchar();
   printf("\nThe ASCII for Char is: %d",x);
   return 0;/*** was avoiding this line ****/
 }

Since getchar returns an int , x should be an int . This is done to handle <a href="http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048865140&id=1043284351">EOF</a> .

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>return 0;/*** was avoiding this line ****/
Lazy? Seriously, do 8 characters of boilerplate really make enough of a difference to totally destroy the portability and correctness of your code?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

-> NOT using int main() == not good

i agree

BUT

#include<stdio.h>
int main()
{
   printf("\nHello World!");
  return 0;/* I dont think my main should return an Integer*/ 
}


I think i am right in this case. I dont have anything against int main(). But if we dont have to use it , we should make our life simple in terms of compiling. Because if your int main() doesnot have return 0; , you might get a warning or error in any C compiler in the world.

Some material to have a look at:
int main() vs void main() http://www.eskimo.com/~scs/readings/voidmain.960823.html

So i agree with you , that i should use int main() , but a small program like printing ASCII and integer doesnot need that much of attention. :)

Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 

>/* I dont think my main should return an Integer*/
It doesn't matter what you think. You still have to follow the rules. If you don't like it, go to C99 or C++ where 0 is returned automagically. And if you use C99, be sure to tell us so you don't get blasted for failing to return a value[1].

>I think i am right in this case.
I know you're not.

>But if we dont have to use it , we should make our life simple in terms of compiling.
That's the thing, you do have to use it unless your compiler offers an alternative. If your compiler doesn't offer an alternative, the behavior is undefined. You can't win this battle because nobody who's qualified to have an opinion on the matter will agree that saving a trivial line of code is worth undefined behavior.

>Some material to have a look at:
I've read it, but thanks anyway.

>So i agree with you , that i should use int main() , but a small program
>like printing ASCII and integer doesnot need that much of attention. :)
Rationalize it however you like, but I won't trust your code, and I'll encourage other people not to trust your code, because if you're that sloppy with a small program, who knows what kind of crap you write in larger programs.

[1] Of course, you'll probably still get blasted by the people who think it's a bad practice.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

lol,

I am not a Software engineer but i am an electronic engineer. I have done C++ and i am a Java addict. i know other languages too. But my point is , i am not doing anything wrong by putting void main (void)
I have done parallel to LCD interfacing using C. And if you call my void main sloppy , then that sloppiness worked. Saying this that i am wrong by using void main , is not acceptable, if its wrong it shouldn't work , but its compiling and running.
i admitted that you are right , but you cant say i am wrong and say my code is not trustable.

Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 

How many times are you prepared to run across the road blindfolded, just because you managed to get across the first time without being run over?

Being right is a matter of attitude to solving the problem, not what your current compiler will let you get away with. Of course your next compiler may be a lot less lenient, but by then the damage has been done and you have to unlearn a bunch of crap (I know, I've been there).

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Makes sense,
thanks Salem ;)

Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 

>But my point is , i am not doing anything wrong by putting void main (void)
Do you honestly believe that you're not doing anything wrong by breaking an explicit rule?

>i admitted that you are right
No, you agreed with me superficially and then went right back to saying that your way isn't wrong. :icon_rolleyes:

I'm sorry Software guy, but you're a bad programmer and you shouldn't be writing code until you understand the problem with "it works for me" arguments.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 
Plz..guys i want to know is there any function that can give ASCII value of character? and do i need to use any header file to use that function


i used a this code:
int x;
x=getch();
printf("%d",x);

punni
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

#include
int main()
{
char ch;
int x;
scanf("%c",&ch);
x=ch;
printf("%d",x);
}

masha2505
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Two resurrections of a 3-year old thread with worthless posts are enough. Closed.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You