| | |
How to find ASCII value of character in c
![]() |
Here is sample code, which demonstrates on how to get a the ASCII code for the entered char.
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
C Syntax (Toggle Plain Text)
#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
Last edited by ssharish2005; Jun 27th, 2008 at 1:06 pm.
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:
myNum would have the value 97 in it.
c Syntax (Toggle Plain Text)
int myNum = 'a';
myNum would have the value 97 in it.
•
•
Join Date: May 2008
Posts: 38
Reputation:
Solved Threads: 2
C Syntax (Toggle Plain Text)
#include<stdio.h> void main (void) { char x; printf("\nEnter a Character : "); x = getchar(); printf("\nThe ASCII for Char is: %d",x); }
•
•
Join Date: May 2008
Posts: 38
Reputation:
Solved Threads: 2
C Syntax (Toggle Plain Text)
#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 ****/ }
Last edited by Software guy; Jul 9th, 2008 at 12:19 pm.
•
•
•
•
#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 ****/ }
getchar returns an int , x should be an int . This is done to handle EOF . "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- numeric TextBox (VB.NET)
- ArrayOutofBounds (Java)
- Pls help :Find first non-repeated char in a string (C++)
- random (C++)
- Codes FOr VGA/SVGA using ports and not interrupts (Assembly)
- HELP - Printing Integers (Assembly)
- GetKey via Interrupt or I/O (Assembly)
- String to integer to ascii (C)
- Array troubles? (C++)
- 4Q i cannot find in my book (Java)
Other Threads in the C Forum
- Previous Thread: any tutorial web site
- Next Thread: help me with this code
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






