This question already has answers here:
`getchar()` gives the same output as the input string
(5 answers)
Closed 9 years ago.
I wrote a program to read from input character by character and print it to output and here is my code:
#include <stdio.h>
main()
{
int c;
while((c = getchar()) != EOF)
{
printf("%s\n", "log1");
printf("%c\n", c);
printf("%s\n", "log2");
}
}
and this is the result:
a(my input)
log1
a
log2
log1
log2
but it should have this result:
a
log1
a
log2
what's wrong with this program?
you giving input a and newline
a(my input) You are giving a and newline
//this is because of a
log1
a
log2
//this is because of newline
log1
log2
Check for newline and avoid printing Newline.
while((c = getchar()) != EOF)
{
if(c!='\n')
{
printf("%s\n", "log1");
printf("%c\n", c);
printf("%s\n", "log2");
}
}
This is because you while((c = getchar()) != EOF) ends after hitting EOF. This is because when you type something and you hit the enter key then everything is stored in an internal buffer.
Your code stops when getchar doesn't find anything in that buffer.
You may also check out this:- Where does getchar() store the user input?
getchar is running during the second iteration. The problem is that your input was actually "a[enter]", so the second character that getchar read was a newline character, and it printed that.
If you give input of "abc", the things may seem more clear.
while(getchar() != '\n');
bear in mind that the expression in the while loop is executed everytime - so even when the character is '\n' is found, it has already been removed from the stream by the getchar() call.
Place a condition not to print \n (on pressing Enter
while((c = getchar()) != EOF)
{
if(c != '\n')
printf("%s\n", "log1");
printf("%c\n", c);
printf("%s\n", "log2");
}
Related
I run the following simple C code.
int main()
{
int c;
while ((c = getchar()) != EOF)
{
putchar(c);
printf("%d\n", c);
}
return 0;
}
The output of code when I enter character A as input from keyboard is as follow:
>A
>A65
>
>10
>
Why does this code print the number 10 after each inner while loop?
Think about what you're doing when the program is asking for input.
Are you just hitting A?
No, you're hitting AEnter, and hitting the Enter key results in a newline, so your while loop is actually doing this:
1. Enter loop
2. getchar() waits for input from stdin
2. You enter 'A', hit 'Enter'
3. c is assigned '65' (integral value of 'A')
4. putchar(c) and then printf("%d\n"), so you see 'A65'
5. getchar() immediately gets next character from stdin, which is linefeed
6. c is assigned '10' (integral value of '\n')
7. putchar(c) and printf("%d\n"), so you see a newline followed by 10.
You can make your code ignore the linefeeds/non-printable characters if you want (this depends on your locale), using isprint:
#include <stdio.h>
#include <ctype.h>
int main() {
for(int c; (c = getchar()) != EOF;) {
if (!isprint(c)) {
printf("Ignoring character with value %d\n", c);
} else {
putchar(c);
printf("%d\n", c);
}
}
return 0;
}
And if I run this:
root#6f67da78fe9a:~# gcc -o chartest source.c
root#6f67da78fe9a:~# ./chartest
A
A65
Ignoring character with value 10
When I run the program below, output give second 1. I debug the program, but I couldn't understand why compiler writing second 1 anytime?
output like this.
#include <stdio.h>
main()
{
int c;
while (c = getchar() != EOF)
printf("%d\n", c);
printf("%d - at EOF\n", c);
}
The condition in the while statement
while (c = getchar() != EOF)
is equivalent to
while (c = ( getchar() != EOF) )
due to the operator precedence. And this logical expression getchar() != EOF always yields 1 if the input was not interrupted.
You need to write
while ( ( c = getchar() ) != EOF)
The Enter key you press to "send" the input from the terminal to the program is added in the input buffer as a newline '\n', which will be read in the second iteration of the loop.
I'm new to C and ASCII numbers, so I was using a code sample to print ASCII numbers
int main(){
int c;
while ((c = getchar()) != EOF){
printf("%d\n", c);
}
}
Output for "d" was
100
10
Whatever letter I tend to type, the result will be the ASCII code of said letter and 10 at the end as well. I'm not sure where this 10 is coming from.
That's the code for the newline character that ended the input line.
if you want to stop at the end of the line and not include it, add another check.
while ((c = getchar()) != EOF && c != '\n'){
printf("%d\n", c);
}
When i run this code it prints the dos 0 character(space) once, puts a space and prints dos 1 character(smiley face with white eyes and mouth).
int c = 0, b = 1;
printf("%c %c", c, b);
But when i run this code below the result is being printed twice. I either get 2 spaces or 2 smiley faces.
while(c != -1)
{
c = getchar() != EOF;
putchar(c);
}
Edit: My code doesnt have paranthesis so it compares every single entered character with EOF and assigns the result (0 or 1) to c and then prints c as a char. In order to prevent this, we can use paranthesis to force the program to do c = getchar() first and then compare that value with EOF.
while((c = getchar()) != EOF)
Your problem is that a '\n' character is not equal to EOF. The '\n' is being read in as a second character.
This question already has answers here:
getchar() skips every other char in C
(2 answers)
Closed 7 years ago.
Can anybody tell me why the line o = getchar(); works properly only the first time and then once works, once it's not?
#include <stdio.h>
int main(void)
{
char o;
for (int i = 1; i > 0; i++)
{
printf("%d\n", i);
if (i % 10 == 0)
{
printf("Do you want to continue? (y/n): ");
o = getchar();
if (o == 'n')
break;
}
}
return 0;
}
First of all, getchar() returns an int, which may not fit into a char. Change
char o;
to
int o = 0;
Then, to come to the point of the skipping, well, it actually do not skip. When you press any key and press ENTER, the newline from the press of the ENTER key serves as the feed to the getchar() in every second iteration of the loop.
As Sourav Ghosh stated, the problem is the newline character. Once you type something in the console and press "Enter" afterwards, the text is in the input buffer. This buffer is then read by getchar. However, getchar only reads one character with each call. The problem is, that "Enter" is interpreted as a character ('\n') and is hold in the buffer, getchar is going to read it as an individual character. The next call of getchar will read the newline character, rather than the wanted character.
Solution: You could call getchar (after your actual call) until the newline is reached, thus clearing the read line inside the buffer.
while (getchar() != '\n')
;
Or just build a little function
int nextch() {
int ch = getchar();
while (getchar() != '\n')
;
return ch;
}