codescracker
c

C Program to Print ASCII Value



« Previous Program Next Program »

In this article, you will learn and get code about printing of ASCII value(s) of a character, all character or all character in a string (given by user at run-time). So here are the list of programs, you will go through over here:

What is ASCII ?

ASCII stands for American Standard Code for Information Interchange. It is a character encoding standard for electronic communication.

Print ASCII Value of a Character

The question is, write a program in C that receives a character as input and prints its ASCII value as output. The answer to this question is:

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    int i;
    printf("Enter a Character: ");
    scanf("%c", &ch);
    i = ch;
    printf("\nASCII Value of %c = %d", ch, i);
    getch();
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

c program print ascii values

Now supply any character say A and press ENTER key to get the its ASCII value:

c ascii value of a character

The statement,

i = ch;

initializes the ASCII value of a character (stored in ch variable) to i. Because i is of int (inter) type. Therefore, ASCII value of ch gets initialized to i. Print the value of i.

Note - You can also print the ASCII value directly without using the above statement. To do this, just use the format specifier as %d to print the ASCII value and %c to print the character back on output.

Print ASCII Value of all Characters

The question is write a program in C that prints ASCII value of all characters. Here is its answer. In this program, without initializing the character to integer type variable, we've just printed the value using format specifier.

#include<stdio.h>
#include<conio.h>
int main()
{
    int i;
    printf("Character \t ASCII Value\n\n");
    for(i=0; i<255; i++)
        printf("%c \t\t %d\n", i, i);
    getch();
    return 0;
}

If you run the above program, you will see the output like:

c ascii value of all characters

Above snapshot is just a small part of output produced by the previous program. Because, there are 254 characters gets printed, so we're not able to show all at once, therefore you have seen the above output of showing just small things of the output.

Print ASCII Value of all Characters in a String

The question is write a program in C that receives a string as input and prints ASCII value of all the characters of given string. Here is its answer

#include<stdio.h>
#include<conio.h>
int main()
{
    char str[50];
    int i=0;
    printf("Enter the String: ");
    gets(str);
    while(str[i]!='\0')
    {
        printf("\nASCII Value of %c = %d", str[i], str[i]);
        i++;
    }
    getch();
    return 0;
}

Let's suppose, after running the above program, user has entered codescracker.com as string input. Therefore here is the output produced by above program:

c ascii value of all characters in string

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker