Finding ASCII Value of Character

CCBeginner
Practice Now

Introduction

ASCII stands for American Standard Code for Information Interchange. It is a character encoding standard for electronic communication. Each character represented in ASCII has a unique numerical value, also known as ASCII code. In this lab, we will learn how to find the ASCII value of a character in C programming.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123203{{"`Finding ASCII Value of Character`"}} c/variables -.-> lab-123203{{"`Finding ASCII Value of Character`"}} c/data_types -.-> lab-123203{{"`Finding ASCII Value of Character`"}} c/operators -.-> lab-123203{{"`Finding ASCII Value of Character`"}} c/user_input -.-> lab-123203{{"`Finding ASCII Value of Character`"}} c/memory_address -.-> lab-123203{{"`Finding ASCII Value of Character`"}} c/function_declaration -.-> lab-123203{{"`Finding ASCII Value of Character`"}} end

Create a new file

Create a new file main.c in the ~/project/ directory and paste the following code:

#include <stdio.h>

int main()
{
    printf("\n\n\t\tLabEx - Best place to learn\n\n\n");

    char c;
    printf("Enter a character: ");
    scanf("%c", &c);
    printf("\n\nASCII value of %c = %d", c, c);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

Understanding the code

Let's understand the code line by line.

printf("\n\n\t\tLabEx - Best place to learn\n\n\n");

This line is used to print a header on the console.

char c;

This line declares a variable c of type character.

printf("Enter a character: ");

This line prints a message asking the user to input a character.

scanf("%c", &c);

This line reads the input character from the user and stores it in the variable c.

printf("\n\nASCII value of %c = %d", c, c);

This line prints the ASCII value of the input character that was read in the previous line. The %c format specifier is used to print the character and %d format specifier is used to print the corresponding ASCII value.

printf("\n\n\t\t\tCoding is Fun !\n\n\n");

This line is used to print a footer on the console.

return 0;

This line indicates that the program has finished executing and returns 0 as the exit status.

Compile and run the program

Compile and run the program using the following commands:

gcc main.c -o main
./main

Output:

                LabEx - Best place to learn


Enter a character: A

ASCII value of A = 65


                        Coding is Fun !

Test the program

Enter different characters as input and observe the corresponding ASCII values that are printed on the console.

Summary

In this lab, we learned how to find the ASCII value of a character in C programming. The ASCII value of each character is a unique numerical value and is represented in C programming using the %d format specifier. The knowledge of ASCII values is important in various applications such as encryption algorithms and data encoding.

Other C Tutorials you may like