User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 403,039 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,842 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1234 | Replies: 16
Reply
Join Date: Jun 2008
Posts: 3
Reputation: sargarpramod is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sargarpramod sargarpramod is offline Offline
Newbie Poster

How to find ASCII value of character in c

  #1  
Jun 27th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 215
Reputation: ssharish2005 is on a distinguished road 
Rep Power: 2
Solved Threads: 16
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: How to find ASCII value of character in c

  #2  
Jun 27th, 2008
Here is sample code, which demonstrates on how to get a the ASCII code for the entered char.

  1. #include <stdio.h>
  2.  
  3. #define TRUE 1
  4.  
  5. void clear_buffer( void )
  6. {
  7. int ch;
  8.  
  9. while( ( ch = getchar() ) != '\n' && ch != EOF );
  10. }
  11.  
  12. int main()
  13. {
  14. unsigned int ch;
  15.  
  16. while( TRUE )
  17. {
  18. printf("\nsingle char please - " );
  19. ch = getchar();
  20. clear_buffer();
  21.  
  22. printf("%c - %d", ch, ch );
  23. }
  24. }
  25.  
  26. /* my output
  27. single char please - a
  28. a - 97
  29. single char please - s
  30. s - 115
  31. single char please - d
  32. d - 100
  33. single char please -
  34. */

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
Last edited by ssharish2005 : Jun 27th, 2008 at 12:06 pm.
Reply With Quote  
Join Date: Apr 2006
Posts: 35
Reputation: death_oclock is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
death_oclock death_oclock is offline Offline
Light Poster

Re: How to find ASCII value of character in c

  #3  
Jun 27th, 2008
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:

  1. int myNum = 'a';

myNum would have the value 97 in it.
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: sargarpramod is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sargarpramod sargarpramod is offline Offline
Newbie Poster

Re: How to find ASCII value of character in c

  #4  
Jun 27th, 2008
Thanks guys for replying to my query
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 45
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: How to find ASCII value of character in c

  #5  
Jun 30th, 2008
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);
Why so serious?
Reply With Quote  
Join Date: May 2008
Posts: 32
Reputation: Software guy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Software guy Software guy is offline Offline
Light Poster

Re: How to find ASCII value of character in c

  #6  
Jul 9th, 2008
#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. : )
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: How to find ASCII value of character in c

  #7  
Jul 9th, 2008
>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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: May 2008
Posts: 32
Reputation: Software guy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Software guy Software guy is offline Offline
Light Poster

Re: How to find ASCII value of character in c

  #8  
Jul 9th, 2008
Originally Posted by Narue View Post
>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 ; )
Last edited by Software guy : Jul 9th, 2008 at 11:19 am.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,477
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 141
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to find ASCII value of character in c

  #9  
Jul 9th, 2008
Originally Posted by Software guy View Post
#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 EOF .
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: How to find ASCII value of character in c

  #10  
Jul 9th, 2008
>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?
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:21 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC