×

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

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.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.