C ASCII Values Explanation And Examples

In the aforementioned scenario, we assign the character variable 'A' to the ASCII value 65, ensuring that the numerical value 65 is stored in the character variable instead of the character 'A'.

Simple ASCII Value Example

Let's consider an example to showcase the ASCII value of a character variable.

Example

Example

#include <stdio.h>  

int main()    //main function 

{  

    char ch;    // variable declaration  

    printf("Enter a character: ");  

    scanf("%c",&ch);  // user input  

    printf("\nThe ASCII value of the ch variable is: %d", ch);  

    return 0;  

}

Output:

Output

Enter a character: A



The ASCII value of the ch variable is: 65

Explanation:

In this illustration, initially, it requests the user to input a character, which will then be saved in the 'ch' variable. When we output the 'ch' variable using the %c format specifier, it will exhibit 'A' as the output since we have provided 'A' as the character input. Utilizing the %d format specifier will show its ASCII value, which is 65.

Why are ASCII Values required in C?

In the realm of C programming, ASCII values play a crucial role as characters are essentially encoded as integer values following the ASCII standards. This encoding system allows the compiler and processor to effectively handle, modify, and compare characters through numeric computations.

In the C programming language, characters such as 'A', '1', and '@' are internally represented as integers, facilitating efficient memory utilization. The comparison of characters is made possible through various relational operators due to their underlying ASCII integer values. This feature is particularly useful in developing encoders, parsers, and algorithms that depend on character encoding schemes.

How to Find the ASCII Value of a character in C?

In C programming, determining the ASCII value of a character can be achieved by assigning the character to an integer variable or by displaying the character with the %d format specifier. Converting a character to an integer yields the ASCII value as characters in C are internally stored as integers following the ASCII conventions.

Example to Print ASCII value of the character variable

Let's consider an illustration to display the ASCII value of a character variable by utilizing the %d format specifier in the C programming language.

Example

Example

#include <stdio.h>



int main() {   //main function

    char c = 'M';

    printf("The ASCII value of %c is %d\n", c, c);

    return 0;

}

Output:

Output

The ASCII value of M is 77

Explanation:

In this instance, the variable c holds the character 'M', and its ASCII value is displayed using the %d format specifier. Since characters are stored as integers internally in C, utilizing the %d format specifier with the variable c showcases its ASCII value (77).

Example to Print 0 to 255 ASCII value of the Character Variable

Let's consider another illustration to display the ASCII value of each character in the C programming language.

Example

Example

#include <stdio.h>  

int main()   //main function

{  

 int k;   // variable declaration   

 for(int k=0;k<=255;k++)  // for loop from 0-255  

 {  

     printf("The ASCII value of %c is %d\n", k,k);  

 }  

return 0;  

}

Output:

Output

The ASCII value of  is 0

The ASCII value of  is 1

The ASCII value of  is 2

The ASCII value of  is 3

The ASCII value of  is 4

The ASCII value of  is 5

........

The ASCII value of � is 255

Explanation:

In this instance, we are showcasing the ASCII value of each character. The ASCII values range from 0 to 255 for all characters, therefore, we loop through the for loop from 0 to 255.

Example to Print the ASCII value of a character using Explicit Typecasting

Let's consider an example to demonstrate the process of converting a character to its corresponding ASCII value using explicit type casting.

Example

Example

#include <stdio.h>



int main() {  //main function



    char c = 'T';



    // Find the ASCII value of a character using typecasting

    int ASCIIVal = (int)c;



    printf("ASCII value of %c is %d\n", c, ASCIIVal);

    return 0;

}

Output:

Output

ASCII value of T is 84

Explanation:

In this instance, we have selected the 'T' character which is stored in the variable c. Subsequently, the code displays both the character itself and its corresponding ASCII value by utilizing the printf function.

Example to print the Sum of the ASCII Value of the string

Let's consider a demonstration to showcase the total ASCII value of a string in the C programming language.

Example

Example

#include <stdio.h>  

int main()    //main function

{  

    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;  

}

Output:

Output

Enter a name: Michael



The ASCII value of the character M is 77

The ASCII value of the character i is 105

The ASCII value of the character c is 99

The ASCII value of the character h is 104

The ASCII value of the character a is 97

The ASCII value of the character e is 101

The ASCII value of the character l is 108

Sum of the ASCII value of a string is : 691

Explanation:

In this instance, the user is asked to provide a string input. Following the input, the program runs a while loop to determine the total sum of ASCII values for each character in the string, saving the result in the sum variable.

Advantages of ASCII Values in C

Several advantages of ASCII values in C are as follows:

  • ASCII value is simple and easy to understand, and every character contains a fixed numeric value.
  • In C programming, characters can be compared, sorted, and manipulated using their corresponding ASCII values.
  • It allows arithmetic and logical operations on the characters.
  • In C, basic ASCII uses only 7 bits, which makes it memory-efficient for simple text processing.
  • It also offers a standardized way to represent characters, which ensures consistency in data interpretation.
  • Disadvantages of ASCII Values in C

Several disadvantages of ASCII values in C are as follows:

  • It supports only basic English characters, which restricts its reusability for internationalization and non-English languages.
  • It doesn't offer any built-in mechanism for error detection or correction in text data that can be crucial in specific applications.
  • In C programming, modern systems prefer Unicode (UTF-8) for global compatibility that offers ASCII limited for modern requirements.
  • Conclusion

In summary, the ASCII value within C programming serves the purpose of encoding characters into integer values. This encoding system enables the storage, comparison, and manipulation of characters through arithmetic and logical functions. In C, there are a total of 256 standard ASCII characters available, yet typically only 128 characters (0-127) are utilized. ASCII is limited to supporting fundamental English characters.

Input Required

This code uses input(). Please provide values below: