C ASCII value

What is ASCII code?

ASCII is the American Standard Code for information interchange is a character encoding scheme used for electronics communication. Each character or a special character is represented by some ASCII code, and each ascii code occupies 7 bits in memory.

A character variable in C does not contain a actual character value, rather it stores the ascii value of the character variable . The ascii value represents the character variable in numbers, and each character variable is assigned with some number range from 0 to 127.

Consider the below example

char ch = 'A';

The ascii value of 'A' is 65. In the above example, the ascii value of 65 will be stored in the character variable ch rather than 'A'.

Another example to display the ascii value of the character variable.

snippet
#include <stdio.h>
int main()
{
    char ch;    // variable declaration
    printf("Enter a character");
    scanf("%c",&ch);  // user input
    printf("\n The ascii value of the ch variable is : %d", ch);
    return 0;
}

In the above code, if we print the value of the 'ch' variable by using %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.

ASCII value in C

The below example will display the ascii value of all the characters. As the ascii value of all the characters starts from 0 and ends at 255, iterate the for loop from 0 to 255.

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

The below example will sum the ascii value of a string by executing the while loop which adds the ascii value of all the characters of a string and stores it in a 'sum' variable.

snippet
#include <stdio.h>
int main()
{
    int sum=0;  // variable initialization
    char name[20];  // variable initialization
    int i=0;  // variable initialization
    printf("Enter a name: ");
    scanf("%s", name);
    while(name[i]!='\0')  // while loop
    {
        printf("\nThe ascii value of the character %c is %d", name[i],name[i]);
        sum=sum+name[i];
        i++;
    }
    printf("\nSum of the ascii value of a string is : %d", sum);
    return 0;
}

In the above code, we are taking user input as a string. After taking user input, we execute the while loop which adds the ascii value of all the characters of a string and stores it in a 'sum' variable.

Output

ASCII value in C
ASCII Chart

Below table is the ASCII chart.

DecimalOctalHexCharacter
0000NUL
1101SOH
2202STX
3303ETX
4404EOT
5505ENQ
6606ACK
7707BEL
81008BS
91109HT
10120ALF
11130BVT
12140CFF
13150DCR
14160ESO
15170FSI
162010DLE
172111DC1
182212DC2
192313DC3
202414DC4
212515NAK
222616SYM
232717ETB
243018CAN
253119EM
26321ASUB
27331BESC
28341CFS
29351DGS
30361ERS
31371FUS
324020SP
334121!
344222"
354323#
364424$
374525%
384626&
394727'
405028(
415129)
42522A*
43532B+
44542C,
45552D-
46562E.
47572F/
4860300
4961311
5062322
5163333
5264344
5365355
5466366
5567377
5670388
5771399
58723A:
59733B;
60743C<
61753D=
62763E>
63773F?
DecimalOctalHexCharacter
6410040@
6510141A
6610242B
6710343C
6810444D
6910545E
7010646F
7110747G
7211048H
7311149I
741124AJ
751134BK
761144CL
771154DM
781164EN
791174FO
8012050P
8112151Q
8212252R
8312353S
8412454T
8512555U
8612656V
8712757W
8813058X
8913159Y
901325AZ
911335B[
921345C\
931355D]
941365E^
951375F_
9614060`
9714161a
9814262b
9914363c
10014464d
10114565e
10214666f
10314767g
10415068h
10515169i
1061526Aj
1071536Bk
1081546Cl
1091556Dm
1101566En
1111576Fo
11216070p
11316171q
11416272r
11516373s
11616474t
11716575u
11816676v
11916777w
12017078x
12117179y
1221727Az
1231737B{
1241747C|
1251757D}
1261767E~
1271777FDEL
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +