Scanf exercise
Scanf Exercise
Write a program that takes 6 characters. Characters may be a combination of alphabets, numbers, or special characters. The program should take 6 characters from the user and prints the ASCII codes of the entered characters.
This is how your output should look like, as shown in Figure 1.
First, there should be a message saying, “Enter 6 characters of your choice:”.
Let’s enter some characters like alphabets, numbers, or special characters. Here I enter a b c d 1 2. So, enter the input by giving space in between. When I hit enter, it should print the ASCII codes, the equivalent ASCII codes of the characters.
- Use scanf to read 6 characters from the user and store them in variables.
- For each character, print its corresponding ASCII code by casting the character to an integer type.
The exercise requires you to write a C program that takes 6 characters as input from the user. These characters can be any combination of alphabets, numbers, or special characters. The program should then print the ASCII codes of the entered characters.
ASCII (American Standard Code for Information Interchange) is a character encoding standard used to represent text in computers and other devices that use text. Each character is assigned a unique numeric value (ASCII code), allowing computers to understand and manipulate text data.
To accomplish the task, you would need to use the scanf function in C to read the user’s input. The scanf function is used to read formatted input from the standard input stream (usually the keyboard). In this case, you would read 6 characters using scanf and store them in variables.
Once you have the characters stored, you can then use a loop or individual print statements to display the ASCII codes of each character. The ASCII code of a character can be obtained by simply casting the character to an integer type, as characters are internally represented as numeric values.
For example, if the user enters ‘A’, the ASCII code for ‘A’ is 65. If the user enters ‘1’, the ASCII code for ‘1’ is 49. (Figure 1)
Keep in mind that the ASCII codes of characters may vary based on the character encoding used by the system (e.g., ASCII or UTF-8). However, for standard ASCII characters, the codes will be consistent across most systems.
In the following article, let’s implement this.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1