#include<stdio.h>
#include<conio.h>
int main() {
long nc;
nc = 0;
while (getchar()!= EOF){
++nc;
printf("%ld\n", nc);
}
return 0;
}
My question is: When I input a number or a character, it increments twice >.<
for example: I ran the program, I typed 1, then its going to output
1
2
can someone please tell me why >< cause isn't it suppose to just increment 1? And the value of nc that the program is gonna show is 1? Then its going to become 2 when i enter another number or character?
After entering any number you are pressing Enter key.
and as '\n' != EOF so it is running two times.
int main() {
long nc;
nc = 0;
while (getchar()!= '\n'){ // check for enter key here.
++nc;
printf("%ld\n", nc);
}
return 0;
}
When you input a number and press Enter key, an additional \n character passed to the standard input buffer. getchar reads that number leaving behind \n in the buffer. On next iteration of loop getchar reads \n before pressing any character by you and hence inside while for second time.Hence value is printed twice as \n is not there.
Use below while condition and this shall fix the issue.
while(getchar() != '\n');
This will eat up any number of \n.
Related
I have this little program:
#include <stdio.h>
int main(){
int c;
while(c != EOF){
printf("Enter character\n");
c = getchar();
printf("Character: %c\n", c);
}
printf("FIN\n");
return 0;
}
The output of the terminal seems odd, because the while loop gets executed twice after a character was entered:
Enter character
a
Character: a //This should be the last output after a char was entered, but the loop gets executed a second time without waiting for a keyboard-input:
Enter character
Character:
Enter character
In the terminal I'm compiling and running the code like this:
gcc main.c
./a.out
What am I doing wrong?
Thanks for the answers, It's the lf entered by enter .... It's so obvious :D
You are entering 2 characters, the 'a' and a LF.
The while test is not made until both have been processed.
As soon as you press enter, a newline character gets added to the input stream. So your program actually reads two characters: a and \n. This newline character is read by getchar() and assigned to c in the second iteration and you can actually see it being printed as an empty line. Before printing c, you could use a break statement to get out of the loop: if (c == '\n') break;
If you enter abc, you will see the empty line is printed after c.
For starters your program has undefined behavior because you are using uninitialized variable c in the condition of the while loop
int c;
while(c != EOF){
//...
The function getchar also reads white space characters as for example the new line character '\n' that is placed in the buffer after pressing the Enter key.
Another problem is that you are checking the variable c after reading and outputting it
while(c != EOF){
printf("Enter character\n");
c = getchar();
printf("Character: %c\n", c);
}
Instead of getchar you should use scanf as for example
char c;
while ( scanf( " %c", &c ) == 1 )
{
//...
}
Pay attention to the blank before the conversion specifier %c. This blank means that white space characters will be skipped.
I have been experimenting with getchar(), putchar() and have been trying to use EOF. Below is the snippet of code I have been experimenting with.
#include <stdio.h>
int main(void)
{
int c;
c = getchar();
while(c != EOF)
{
putchar(c);
printf("\n");
printf("%d\n", EOF);
c = getchar();
}
return 0;
}
Input: -
a
Expected output: -
a //Due to putchar()
-1 //Value of EOF
//Now the cursor should come in next line and wait for next character.
Output getting in real time: -
a
-1
-1
//Cursor waiting for next character.
I am not able to comprehend the reason why the output is showing -1 two times.
Your code comment says
//Now the cursor should come in next line and wait for next character.
But the second loop doesn't wait. It reads the newline that was already entered, and this is shown by the extra blank line in the output.
After the first input before the loop
c = getchar();
the input buffer contains the new line character '\n' that corresponds to the pressed key Enter.
So in the while loop there are outputted
a
and
-1
due to the statement
printf("%d\n", EOF);
After that this statement in the loop
c = getchar();
reads the new line character that is present in the input buffer. So again this statement
printf("%d\n", EOF);
outputs
-1
I wrote a simple c program to count number of characters
#include <stdio.h>
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
This program is not printing me the characters. On testing different cases, I found that I am stuck in an infinite while() loop.
This program isn't printing me the characters
It won't. You did not add any statement to print them.
I found that i'm stuck in an infinite while loop
If you don't hit the breaking condition, you'll be in the loop. You need to get EOF to exit the loop. use
CTRL+Z (on windows)
CTRL+D (on linux)
Now, the solutions:
getchar() won't print the values. You have to store the values and print explicitly (if you want to) using , maybe putchar().
You either supply EOF or change the breaking condition of while() to come out of the essential infinite loop otherwise.
Apart from the coding issues, you need to think about the logic, too. In the present form of the code, getchar() counts the newline (\n) as a valid character, too. To explain, an input in the form of
$ ./a.out ENTER
a ENTER
s ENTER
d ENTER
f ENTER
g ENTER
CTRL+D
Will produce a result
10
but that is not what we generally call counting the character. You may want to review this part of the logic, too.
That said, the recommended signature of main() is int main(void).
Try the following
#include <stdio.h>
int main( void )
{
int c;
long nc = 0;
while ( ( c = getchar() ) != EOF && c != '\n' ) ++nc;
printf( "%ld\n", nc );
}
You have to generate the EOF state ( Ctrl+d in UNIX-systems or CTRL+z in Windows) or simply press Enter.
Try like this:
#include <stdio.h>
int main(void)
{
int c;
long nc = 0;
while ( ( c = getchar() ) != EOF && c != '\n' )
++nc;
printf( "%ld\n", nc );
}
while (getchar() != '\n')
++nc;
printf("%ld \n",nc);
Worked!
I have tried many variations of this code, including using a scanf function, and every time it increments by 2 points instead of one. Here is the code:
#include <stdio.h>
int main(void)
{
double nc;
for(nc = 0; getchar() != EOF; ++nc)
printf("%.0f\n", nc);
}
This is the output that I get. The input that I used was qwerty, and the outputs are numbers 0-11 instead of 0-5 as expected.
q
0
1
w
2
3
e
4
5
r
6
7
t
8
9
y
10
11
One thought I had was that when I press enter, it is counted as a value for getchar along with the character I entered and this causes the loop to run through two iterations. Can anybody further explain this concept or provide links to more information about it for me?
The trailing newline from previous getchar is taken up as input for next getchar
So use,
for(nc = 0; getchar() != EOF; ++nc)
{
printf("%.0f\n", nc);
getchar(); //"eat" the trailing newline
}
I have this program and I want it to increment by one and print the value of my counter each time I give a character
#include <stdio.h>
int main(void){
//Declarations
long nc;
//Instantiations
nc = 0;
while (getchar() != EOF){
++nc;
printf("%ld\n", nc);
}
return 0;
}
When the loop initiates if I press ENTER I get 1,2,3,4,5... which is ok.But if I type a character or something else it prints the next two numbers 12,34,56,78. Why is that happening??
I am running the program on gcc 4.6.3 Ubuntu 12.04 release.
Terminal input is normally line buffered. Your program only gets input to process when you press ENTER. If you type several characters, you will get one line of output for each character you input (plus the newline itself) as getchar() returns each character in sequence.