C Program for Character to ASCII Conversion

Character to Ascii conversion

ASCII is the abbreviation of American Standard Code for Information Interchange. It is a character encoding standard for electronic communication. ASCII encodes 128 specified characters into seven-bit integers. It contains the digits from 0 - 9, the upper and lower case English letters from A to Z and some special characters.


Example:

ASCII value of lowercase 'a' is 97.

ASCII value of uppercase 'A' is 65.


  The following C program converts a given character to its corresponding ASCII value. The algorithm, pseudocode and time complexity of the program have also been added.






1. Algorithm for Character to ASCII Conversion


1. Take a character as input.

2. Do cast the character to integer

3. Return the result




2. Pseudocode for Character to ASCII Conversion


Input : A character $c$

Output : ASCII value of character $c$

1. Procedure toAscii($c$):

2. Return (Int($c$))

3. End Procedure





3. Time complexity for Character to ASCII Conversion


Time Complexity: O(1)





4. C Program & output for Character to ASCII Conversion

Code has been copied
/*********************************************
           alphabetacoder.com
C program for character to ASCII conversion
*********************************************/

#include <stdio.h>

int main() {
    char x;
    int v;
    //take input of the character
    printf("Enter the character = ");
    scanf("%c", & x);
    //store ASCII value of the character
    v = (int) x;

    printf("\nASCII value of %c is = %d", x, v);
}

Output


Case 1:

Enter the character = b

ASCII value of b is = 98


Case 2:

Enter the character = F

ASCII value of F is = 70