×

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special extra characters and control codes have unique values in typical ASCII-encoded data. Character encoding for telegraph data is the basis for ASCII encoding. It was initially released in 1963 as a computing standard by the American National Standards Institute.

The upper and lowercase letters A through Z, the digits 0 through 9, and the most common punctuation marks are all included in the ASCII encoding. It also makes use of a few control characters that aren't used for printing but were developed for teletype printing terminals.

A character variable in the C programming language instead stores the variable's ascii value instead of a character value. Each character variable is given a number between 0 and 127 as its ascii value, which represents the character variable in numbers. A's ascii value, for instance, is 65.

The following are possible representations for ASCII characters:

  • Base-16 numbers rendered as pairs of hexadecimal digits, with the decimal values of 10-15 represented by 0 through 9 and A through F
  • As octal (base 8) numerals with three digits
  • As decimal numbers from 0 to 127
  • As either 7- or 8-bit binary

Getting ASCII values

Here, characters that include both upper- and lowercase letters from the alphabet and strings and character variables are included in the ASCII values.

For example, the ASCII value of ‘A’ is 65.

This implies that 65 is saved in the character variable instead of the letter "A" if you assign the letter "A" to it.

Let us now look at how to print a character ASCII value in C programming.

Program to print the ASCII value

#include <stdio.h>
int main() {  
    char c;
    printf("Enter desired character: ");
    scanf("%c", &c);  
    // %d shows a character's integer value.
    // %c shows the actual character.
    printf("ASCII value of %c = %d", c, c);
    return 0;
}

Output

How to get ASCII value in C

The output seen above demonstrates that the user entered the character "S" and that, as a result, the character's ascii value, 83, was produced.

A character must be entered by the user in this programme. Variable c contains a copy of the character.

The ASCII value of the letter S, 83, is displayed when the format string %d is used.

G is shown when the format string %c is used.

Now similarly it is possible to find out the character corresponding to the ASCII value

Let us look at the example programme for the above-mentioned event.

#include <stdio.h>
#include<conio.h>
int main()
{
    int k;
    printf("Enter the number within the range 0 to 127: ");
    scanf("%d", &k);
    char c = k; // automatically converted to a character from an integer
    printf("Character that is corresponding to the ASCII value %d = %c\n", k, c);
    return 0;
}

Output

How to get ASCII value in C

There is also a possibility where printing of all the ASCII values takes place.

Now let us demonstrate a C program to the ASCII values of all the characters.

#include <stdio.h>  
#include<conio.h>
int main()  
{  
 int s;   // variable declaration   section
 for(int s=0;s<=255;s++)  // it is afor loop from 0-255  
 {  
     printf("\nThe ascii value of %c is %d", s,s);  
 }  
return 0;  
}

Try on the above code to see the output for better understanding.

The application mentioned above will show the ascii value for each character. Since we are aware that all characters have an ascii value that ranges from 0 to 255, we iterate the for loop from 0 to 255.

We will now write a programme that adds the ascii values in a string.

#include <stdio.h>  
#include<conio.h>
int main()  
{  
    int sum=0;  // initializing the variable
    char name[50];  // initializing the variable
    int k=0;  // initializing the variable
    printf("Enter your desired name: ");  
    scanf("%s", name);  
    while(name[k]!='\0')  // using the while loop 
    {  
        printf("\nThe ascii value of the character %c is %d", name[k],name[k]);  
        sum=sum+name[k];  
        k++;  
    }  
    printf("\nSum of the ascii value of the considered string is : %d", sum);  
    return 0;  
}  

Output:

How to get ASCII value in C

In the code above, we are accepting user input in the form of a string. After receiving user input, we run a while loop that adds the ascii values of each character in a string and stores the result in a variable called ‘sum’.


Related Topics

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.