Articles

C++ program to find character ASCII value

C++ program to find character ASCII value


In this C++ program tutorial our task is to:

  •  Write a C++ program to find the ASCII value of the given character.

C++ program to find the ASCII value of the given character

Given below is the C++ program in which a user input of char data type is accepted and then it is type casted to int data type and the following ASCII code is obtained.

#include<iostream>
using namespace std;
int main()
{
    char s;
    cout<<"Enter a character"<<endl;
    cin>>s;
    cout<<"The ASCII value of the given character is"<<endl;
    cout<<int(s);       //Type casted from char data type to int data type.
    return 0;
}

OUTPUT :

Enter a character

a

The ASCII value of the given character is

97

 

EXPLANATION:

  • In the above code the compilation starts from the main function (here int main()) .
  • Then it takes a character as a input .
  • By typecasting the char data type to int data type.
  • When we change a character to its integer format then the ASCII value of the corresponding character gets printed out.

You can see the above code execution and output in codeblocks IDE:

C++ program to find the ASCII value of the given character

 


CPP Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sayantan Bose
- šŸ”­ Iā€™m currently working on DS Algo skills - šŸŒ± Iā€™m currently learning web develeopement - šŸ‘Æ Iā€™m looking to collaborate with Oppia - šŸ“« Reach me at https://www.linkedin.com/in/sayantan-bose-14134a1a6/ - šŸ˜„ Pronouns: his/him
Page Views :    Published Date : Aug 06,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!