chevron_left
660 points
6 2 1

How to print ASCII value in C
Photo by Drew Williams

This article provides knowledge and code to print the ASCII value of one, all, or all characters in a user-specified string at runtime. Character variables hold ASCII values (integers from 0 to 127), not the characters themselves in C programming. This value is called an ASCII value. So here is the list of programs to run here: Prints the `ASCII` value of a character. Prints the `ASCII` value of all characters. prints the `ASCII` value of each character in a string

What is ASCII code? #

The full form of ASCII is the American Standard Code for Information Interchange. This is a character encoding scheme used for electronic communications. Some ASCII codes represent random or special characters; each ASCII code occupies 7 bits of memory. In the C programming language, a character variable itself does not contain a character value, but rather the ASCII value of the character variable. ASCII values represent character variables numerically, and each character variable is assigned a number from 0 to 127. For example, the ASCII value for "A" is 65.

Let's understand through an example.

We will create a program that will display the ASCII value of the character variable.

#include   
int main()  
{  
    char ch;    
    printf("Enter a character");  
    scanf("%c",&ch);  
    printf("\n The ascii value of the ch variable is : %d", ch);  
    return 0;  
}

In the above code, the first user will give the character input, and the input will get stored in the 'ch' variable. If we print the value of the 'ch' variable by using the %c format specifier, then it will display 'A' because we have given the character input as 'A', and if we use the %d format specifier then its ASCII value will be displayed, i.e., 65.

Output:
Enter a character: A
The ASCII value of the ch variable is: 65

The above output shows that the user gave the input as 'A', and after giving input, the ASCII value of 'A' will get printed, i.e., 65.

How to print ASCII value in C. #

Now, we will create a program that will display the ASCII value of all the characters.Here we use a format specifier to specify the numeric value of the character. Here %d is used to convert the character to its ASCII value.

#include   
int main()  
{  
 int k;  
 for(int k=0) 
 {  
     printf("\nThe ascii value of %c is %d", k,k);  
 }  
return 0;  
}

The above program will display the ASCII value of all the characters. As we know that ASCII value of all the characters starts from 0 and ends at 255, so we iterate the for loop from 0 to 255.

The Solution #

In C programming, character variables contain ASCII values (integers from 0 to 127) rather than the characters themselves. This integer value is the ASCII code for the character. For example, the ASCII value for "A" is 65. This means that assigning 'A' to a character variable will store 65 in the variable, not 'A' itself.

Now let's see how to print the ASCII value of a character in C programming.

#include 
int main() {  
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);  
    
    // %d displays the integer value of a character
    // %c displays the actual character
    printf("ASCII value of %c = %d", c, c);
    
    return 0;
}
Output:
Enter a character: G
ASCII value of G: 71

In this program, the user is asked to enter a character. The character is stored in variable c.

When %d format string is used, 71 (the ASCII value of G) is displayed. When %c format string is used, 'G' itself is displayed.

Solution one

Now we will write a C program that uses a for loop to print the ASCII value of every character. How to display an ASCII character code along with its value in C programming using a loop. A C program that prints an ASCII character with its character code.

#include 
int main()
{
    int i;
    for(i=0; i) 
    {
        printf("ASCII value of character %c = %d\n", i, i);
    }
    return 0;
}

In the above program, I have used the %c format specifier with an integer type. Performing this will print ASCII characters represented by the integer I.

You can use any format specifier with any type if it supports the operation.

The Conclusion

ASCII stands for American Standard Code for Information Interchange. There are 256 ASCII-encoded characters, but only 128 characters (0-127) are used. You can use the %d format specifier to print C ASCII values in arbitrary characters.To print all ASCII characters, you can use a loop that iterates over the numbers from 0 to 255 and prints the characters using the format specifiers %c and %d.

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

How to make Sum of digits at Even and Odd Places in C? Bushra Rubab - Sep 9
How to check if an array is empty in C++? AnkurRanpariyav - Aug 7
How to create the table in C++? AnkurRanpariyav - Aug 6
How to round off to 2 decimal places in C++? AnkurRanpariyav - Aug 6
How to iterate through a set in C++? AnkurRanpariyav - Aug 5
How to make text-based games in C++ AnkurRanpariyav - Aug 2
How to convert int to char c++? JimCoder - May 29
Algorithm to calculate logarithms in C. Without math.h AnkurRanpariyav - Aug 5
How to print an arraylist in java sadmin - May 10
Read file in c line by line zhteja - Sep 19
The function was not declared in this scope c++ AnkurRanpariyav - Aug 7
Check if the vector contains the given element or not in C++ AnkurRanpariyav - Aug 3
C++ nullptr was not declared in this scope cafe-coder45 - Jun 2
Qualified Name Is Not Allowed In Member Declaration in C++ JimCoder - May 29
C++ cstring to string sadmin - Sep 15
Write a 'C' program to find the day of the given date. Bushra Rubab - Sep 9
C++ Program to Find the Maximum and Minimum N Numbers AnkurRanpariyav - Aug 5
C++ round double to 2 decimal places JimCoder - May 29
How to do Increment value by 1 in select in SQL? Bushra Rubab - Sep 7
How to display selected value of drop-down list in html JimCoder - May 29