Reading user input and displaying the ASCII equivalent - c

I'm writing a program that asks the user to input a number and displays the ASCII equivalent. It also displays the ASCII numbers of the 5 previous numbers and the five next numbers.
here's my code
#include <stdio.h>
displayASCIITABLE(int input);
int main()
{
int input;
printf("enter a number:\n");
displayASCIITABLE(input);
return 0;
}
displayASCIITABLE(int input)
{
printf("Number Character\n");
for(int i=input-5;i<=input+5;i++)
{
printf("%d %c\n",i,i);
}
}
The code works perfectly but some of the numbers ASCII value is not showing here's an example
for example, the ASCII value of 7 8 9 10 as shown in the image above

These numbers are probably escape codes, for example 10 is a newline. These types of characters aren't supposed to be shown but affect things like formatting

Not all character codes in ASCII are printable. Please, read about ASCII on Wikipedia, it will help you.
For instance, only characters from 32 ( , space) to 126 (~, tilde) are printable (i.e.: they are displayable on a terminal or in a text file, etc). There are also whitespace characters like \n, \r, \t, and others, that are considered control characters, but they do have some visual effect in a terminal (or in a text file, etc).
NOTE: the strange symbols you see in your terminal are not part of ASCII (intended as US-ASCII), but they are an extension to ASCII (but their codes are platform-dependent, since they rely on which character encoding your terminal has).
TL;DR: you cannot print any integer as it were a character; some character codes are just not displayable.

Related

Format Specifiers in C and their Roles?

wrote this code to "find if the given character is a digit or not"
#include<stdio.h>
int main()
{
char ch;
printf("enter a character");
scanf("%c", &ch);
printf("%c", ch>='0'&&ch<='9');
return 0;
}
this got compiled, but after taking the input it didn't give any output.
However, on changing the %c in the second last line to %d format specifier it indeed worked. I'm a bit confused as in why %d worked but %c didn't though the variable is of character datatype.
Characters in C are really just numbers in a token table. The %c is mainly there to do the translation between the alphanumeric token table that humans like to read/write and the raw binary that the C program uses internally.
The expression ch>='0'&&ch<='9' evaluates to 1 or 0 which is a raw binary integer of type int (it would be type bool in C++). If you attempt to print that one with %c, you'll get the symbol table character with index 0 or 1, which isn't even a printable character (0-31 aren't printable). So you print a non-printable character... either you'll see nothing or you'll see some strange symbols.
Instead you need to use %d for printing an integer, then printf will do the correct conversion to the printable symbols '1' and '0'
As a side-note, make it a habit to always end your (sequence of) printf statements with \n since that "flushes the output buffer" = actually prints to the screen, on many systems. See Why does printf not flush after the call unless a newline is in the format string? for details
In a memory, int take 4 bytes of memory you are trying to storing the int values in a character which will return the ascii value not a int value in %c if you are using a %d which will return the int value which are storing in a memory of 4 bytes memory.

Reading a string by each character in stdinput in c to do a conversion

so I need to create a program in C that reads a string character by character and output each character in the string's corresponding ascii decimal/hexadecimal/octal representation. This program should stop reading the string upon reaching a newline character.
For example given "Hi" the program should print "72 105" if I chose the decimal option.
So my idea is to read the string provided character by character into a large character array checking each character to see if it is the new line and then afterwards looping through elements 0-N where N is the index where char_array[N]=='\n' printing each element as its decimal representation.
So far, I've come up with this code which should cover the decimal representation case but I've come across an error I'm unsure how to troubleshoot.
#include <stdio.h>
int main(){
char char_arr[1000],conversion,temp;
int i=-1,j=0;
printf("Integer conversion in decimal (d), octal (o), or hexadecimal (h)? ");
scanf("%c",&conversion);
printf("Enter a message: ");
do{ i++;
if(scanf("%c",&char_arr[i])!=1){
break;}
}
while(char_arr[i]!='\n');
for(j=0;j<i;j++){
printf("%d",char_arr[j]);
}
return 0;
}
When I run this program it simply prints
Integer conversion in decimal (d), octal (o), or hexadecimal (h)? d
Enter a message:
and gives me no option to enter a message.
Does anyone have any thoughts or explanations for why this would occur? I'm new to C.

Character User-input Either Skipped or not Skipped yet not Stored

I am currently attempting to obtain a user character input which proceeds a user non-character input, such as integer, float/double, etc. I've read several Stack overflow solutions, and not a single one seems to work for me. Here are the six different ways I attempted to write this code:
#include <stdio.h>
int main(void)
{
int integer;
char character;
scanf("%d", &integer);
fflush(stdin);
scanf("%c", &character);
printf("The ASCII Code of %c is %d", character, character);
This gave an ASCII Code of 10 (\n, i.e. line feed character) implying fflush(stdin) did not flush the line feed whitespace. Then, from this, it would be more relevant and convenient if we looked at lines 7-8. Now, I deleted fflush(stdin) and added a space before %c conversion specifier in scanf(), i.e.
scanf(" %c", &character) This also did not work, thus I tried the following:
scanf("%c\n", &character) This did allow me to input a value, unlike the previous scenarios, albeit the character was not the same as that inputted because the ASCII code generated was still 10. I also attempted to manipulate the code using getchar() but the ASCII code was either 0 or 10 (space or line feed) and not the actual character input. Thus, I would very much appreciate it if anyone knows a way to resolve this issue. Thank you!

buffer associated with input file stream [duplicate]

This question already has answers here:
Is there any way to peek at the stdin buffer?
(3 answers)
Closed 8 years ago.
how can we see the contents of buffer assocaited with input file stream(STDIN) . suppose if we are giving input using scanf, getchar or any input function how it is actually getting stored in the buffer. especially when we press "enter" key .
example:
case:1)
$ input two integers:
10 20 (enter)
$ input two integers:
10(enter)
20(enter)
case 2:
$enter two characters
a b(enter)
$enter two characters
a(enter)
b(enter)
why in case 1 it is ignoring spacebar(ASCI-32) but in case2 it is taking spacebar as next input. is it propert of scanf function or terminal .
In the first case
Here it is ignoring the space bar because, according to the ascii character set " space " is
a character whose ascii value in decimal is 32.
When "%d" encounters the value 32 it ignores it because it cannot be an integer since
the integer literals have their range between 48(0) and 57(9).
whereas
in the second case we use "%c" to input characters for which the space(ascii - 32) is
a perfectly valid input, and thus is not ignored.
You can also use %d to input characters but then you will have to provide the ascii value
for a character that you want to input, for eg:
If you want to enter and display 'A' as character then your input must be 65.
Hope this helps to clear things a bit.

Extra loop when using getchar() and WHAT indeed getchar() behave

When I want to figure out what does getchar() actually do, this little piece of loop did confuse me.
int i;
int c;
for (i = 0; i < 100; i++) {
c = getchar();
printf("%d\n", c);
printf("i is %d\n", i);
}
The input and output is:
input: 1
output:
49
i is 0
10
i is 1
input: 12
output:
49
i is 2
50
i is 3
10
i is 4
As I previously supposed, if I enter 1 character, the getchar() should extract it out and putchar() would print it, then the program move to the next loop and wait for my next input. But the results seem to show that the code do not work as I supposed:
What do the output numbers mean?
There is always an extra loop printing 10, what does this 10 mean? If it means EOF, why after replacing c = getchar(); with c = (getchar() != EOF); within the loop, the code always print 1 which, as I supposed, should print a 0 in the last loop?
Thx very much!
Question
What do the output numbers mean?
The output numbers refer to the value of your characters according to your character set, usually based on ASCII (some mainframes use also EDCDIC).
C11 (n1570), § 5.2.1 Character sets
Two sets of characters and their associated collating sequences shall be defined: the set in
which source files are written (the source character set), and the set interpreted in the
execution environment (the execution character set). Each set is further divided into a
basic character set, whose contents are given by this subclause, and a set of zero or more
locale-specific members (which are not members of the basic character set) called
extended characters. The combined set is also called the extended character set. The
values of the members of the execution character set are implementation-defined.
Therefore, through this character encoding, 49 is the character'1' and 50 is the character '2'.
Question
There is always an extra loop printing 10, what does this 10 mean?
With ASCII charset, 10 is the linefeed character '\n'.
When you are typing the character '1' on your keyboard, the standard input stream stdin will receive in fact two characters : '1' and '\n', since you are pressing <Enter> to valide your input.
Therefore, you should clean the standard input stream once you have done your getchar call. One possible way to achieve it is to consume every characters until you reach a newline character or EOF:
#include <stdio.h>
int c;
while ((c = getchar()) != '\n' && c != EOF)
;
On BSD, there is also the function fpurge and, on Solaris and GNU/Linux, __fpurge is available.
Question
If it means EOF, why after replacing c = getchar(); with c = (getchar() != EOF); within the loop, the code always print 1 which, as I supposed, should print a 0 in the last loop?
The value of EOF can't be 10, since EOF must have a negative value.
C11 (n1570), § 7.21.1 Introduction
EOF, which expands to an integer constant expression, with type int and a negative value [...].
What do the output numbers mean?
Character codes of the character getchar() returns, since you get a one and you're printing it using the %d specifier. In this case, it seems your character encoding is ASCII or maybe UTF-8, so 1 stands for 49, 2 for 50, etc.
2: [... too long to quote...]
10 is the ASCII and Unicode char code for newline ('\n'). Since you press Enter (getchar() waits for it!), you will get the character that Enter sent to the terminal.

Resources