Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

ASCII values in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

American Standard Code for information interchange(ASCII) is a character encoding that uses numeric codes to represent characters. These include upper and lowercase English letters, numbers, and punctuation symbols. Each ASCII code uses 7 bits to represent each character since the first bit of the byte is always 0. 

A character variable holds an ASCII value in C programming rather than that character itself. This ASCII value represents the character variable in numbers, and each character variable is assigned with some number range from 0 to 127. For example, the ASCII value of 'C' is 67.

Here in the above example, if we assign 'C' to the character variable, 67 is stored in the variable rather than 'A' itself.

You can also read about C dynamic array, C Static Function

Must Read Passing Arrays to Function in C

Examples to Implement ASCII Value in C

Example 1

Here is a program to print the ASCII value of the character variable.

#include <stdio.h>
int main()
{
    char c;
    printf("Enter any character ");
    scanf("%c",&c);
    printf("\nThe ASCII value of the entered character is: %d", c);
    return 0;
}

Output

Enter any character d
The ASCII value of the entered character is: 100

In this program, the first user will give the character input, and the character input is stored in variable c.

If we print the value of the 'c' variable by using %c format specifier, then it will print 'd' because we have given the character input as 'd'. But, here, we used the %d format specifier, so the ASCII value of 'A' will be printed, i.e., 100.

Also Read About, Sum of Digits in C, Tribonacci Series and Short int in C Programming

Example 2

Here is a program to print the ASCII value of Capital characters A to Z.

#include<stdio.h>
int main()
{
    int i;
    for(i=65;i<=90;i++)  // for loop from 65 to 90
    {
        printf("The ASCII value of %c character is %d\n", i,i);
    }
    return 0;
}

Output

The ASCII value of A character is 65
The ASCII value of B character is 66
The ASCII value of C character is 67
The ASCII value of D character is 68
The ASCII value of E character is 69
The ASCII value of F character is 70
The ASCII value of G character is 71
The ASCII value of H character is 72
The ASCII value of I character is 73
The ASCII value of J character is 74
The ASCII value of K character is 75
The ASCII value of L character is 76
The ASCII value of M character is 77
The ASCII value of N character is 78
The ASCII value of O character is 79
The ASCII value of P character is 80
The ASCII value of Q character is 81
The ASCII value of R character is 82
The ASCII value of S character is 83
The ASCII value of T character is 84
The ASCII value of U character is 85
The ASCII value of V character is 86
The ASCII value of W character is 87
The ASCII value of X character is 88
The ASCII value of Y character is 89
The ASCII value of Z character is 90

In the above C program, we will print the ASCII value of Capital characters A to Z. The ASCII value of Capital characters(A to Z) starts from 65 and ends at 90, so we iterate the for loop from 65 to 90.

Also read - Bit stuffing program in c

Example 3

Here is a program to print the ASCII value of the characters of a string.

#include<stdio.h>  
int main()  
{  
    char str[]="Ninjas";  
    int i=0; 
    while(str[i]!='\0')  
    {  
        printf("The ASCII value of character %c is %d\n", str[i],str[i]);  
        i++;  
    }  
    return 0;  
} 

Output

The ASCII value of character N is 78
The ASCII value of character i is 105
The ASCII value of character n is 110
The ASCII value of character j is 106
The ASCII value of character a is 97
The ASCII value of character s is 115

In the above C program, we want to print the ASCII value of the characters of a string "Ninjas". So we executed the while loop, which prints the ASCII value of all the characters of a string "Ninjas" and you can try to run code on online compiler.

Must Read what is storage class in c and Decision Making in C

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. What is ASCII code?
    Ans: ASCII is a code that uses numeric codes to represent characters. Each letter is assigned a number between 0 and 127.
     
  2. What is the full form of ASCII?
    Ans: The full form of ASCII is American Standard Code for information interchange.
     
  3. How many types of ASCII are there?
    Ans: There are two types of ASCII codes. 
    Standard ASCII code uses a seven-bit encoding system. 
    Extended ASCII code uses an eight-bit system.
     
  4. How many characters are in extended ASCII?
    Ans: Extended ASCII supports the representation of 256 different characters.
     
  5. What is the difference between ASCII and Unicode?
    Ans: Unicode is used to process, store and facilitate the interchange of text data in any language. At the same time, ASCII represents text such as uppercase and lowercase letters, symbols, and digits(0-9), etc., in computers.

Key Takeaways

In this article, we have extensively discussed the ASCII values in the C programming language. We also discussed several examples for a better understanding of the ASCII values.

We hope that this blog has helped you enhance your knowledge regarding ASCII Values and if you would like to learn more, check out our article on Strings. You can read about C language articles by clicking C Archives. Do upvote our blog to help ninjas grow. Happy Coding!

Topics covered
1.
Introduction
2.
Examples to Implement ASCII Value in C
2.1.
Example 1
2.2.
Example 2
2.3.
Example 3
3.
FAQs
4.
Key Takeaways