Simplest approach in printing ASCII values of a given alphabet.
Here is an example :
#include<stdio.h>
int main()
{
//we are printing the ASCII value of 'a'
char a ='a'
printf("%d",a)
return 0;
}
Try this:
char c = 'a'; // or whatever your character is
printf("%c %d", c, c);
The %c is the format string for a single character, and %d for a digit/integer. By casting the char to an integer, you'll get the ascii value.
Nothing can be more simple than this
#include <stdio.h>
int main()
{
int i;
for( i=0 ; i<=255 ; i++ ) /*ASCII values ranges from 0-255*/
{
printf("ASCII value of character %c = %d\n", i, i);
}
return 0;
}
This reads a line of text from standard input and prints out the characters in the line and their ASCII codes:
#include <stdio.h>
void printChars(void)
{
unsigned char line[80+1];
int i;
// Read a text line
if (fgets(line, 80, stdin) == NULL)
return;
// Print the line chars
for (i = 0; line[i] != '\n'; i++)
{
int ch;
ch = line[i];
printf("'%c' %3d 0x%02X\n", ch, ch, (unsigned)ch);
}
}
To print all the ascii values from 0 to 255 using while loop.
#include<stdio.h>
int main(void)
{
int a;
a = 0;
while (a <= 255)
{
printf("%d = %c\n", a, a);
a++;
}
return 0;
}
#include<stdio.h>
void main()
{
char a;
scanf("%c",&a);
printf("%d",a);
}
Chars within single quote ('XXXXXX'), when printed as decimal should output its ASCII value.
int main(){
printf("D\n");
printf("The ASCII of D is %d\n",'D');
return 0;
}
Output:
% ./a.out
>> D
>> The ASCII of D is 68
Source: Stackoverflow.com