C Program to Print Ascii Value of a Character using Array

This is a C Program to input a string & store their Ascii Values in an Array & print the Array

Problem Description

This program will take an input of string & store their ASCII values and print them.

Problem Solution

1. Create an array of characters and take its size from users as an input.
2. Enter a string.
3. Using a loop, scan each element(character) of the string and print its equivalent ASCII value, increment the value of iterator used to access the position of characters of the string.

Program/Source Code

Here is source code of the C Program to input a string & store their ascii values and print them. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C Program to Input a String & Store their Ascii Values in an Integer Array & Print the Array
  3.      */
  5.     #include <stdio.h>
  6.     void main()
  7.     {
  9.         char string[20];
  10.         int n, count = 0;
  12.         printf("Enter the no of characters present in an array \n ");
  13.         scanf("%d", &n);
  15.         printf(" Enter the string of %d characters \n" , n);
  16.         scanf("%s", string);
  18.         while (count < n)
  19.         {
  20.             printf(" %c = %d\n", string[count], string[count] );
  21.             ++ count ;
  22.         }
  24.     }
Program Explanation

1. Declare an array of characters of some fixed size, 20.
2. Take size from users as an input.
3. Now, enter a string again as an input, store it in character array declared above.
4. Run a for loop, which will scan each array element (i.e character), printing its equivalent ASCII code using %d notation and the character itself using %c inside printf() function.
5. Increment iterator, count after printf() statement to access next position.
6. Exit

Runtime Test Cases
Enter the no of characters present in an array
Enter the string of 10 characters
 s = 115
 a = 97
 n = 110
 f = 102
 o = 111
 u = 117
 n = 110
 d = 100
 r = 114
 y = 121

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.