Monday, April 28, 2025

C Program - Find ASCII Value of a Character Entered by User

This C code takes a character input from the user and prints its ASCII value.  

#include <stdio.h>

int main()
{

    char original_char;

    printf("Enter Character: \n");
    scanf("%c", &original_char);

    printf("ASCII value for %c is: %d \n", original_char, original_char);

    return 0;
}

Result:

Enter Character:
a
ASCII value for a is: 97

Process returned 0 (0x0)   execution time : 3.644 s
Press any key to continue.

Here's a detailed explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which provides functions for printing to the console. 

int main()
{

    char original_char;

    printf("Enter Character: \n");
    scanf("%c", &original_char);

This code declares a character variable original_char to store the user input and prints a message asking the user to enter a character. The scanf() function reads a character value entered by the user from the console and assigns it to the original_char variable. The & symbol is used to pass the address of the original_char variable so that its value can be modified by the scanf() function. 

    printf("ASCII value for %c is: %d \n", original_char, original_char);

This code uses the printf() function to print the ASCII value of the character entered by the user. The %c placeholder in the format string is replaced with the value of original_char, and the %d placeholder is replaced with the integer value of original_char. This prints the ASCII value of the character to the console. 

    return 0;
}

This line ends the main function and returns 0 to indicate that the program has completed successfully.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...