This question already has an answer here:
what is EOF in cygwin in windows 10
(1 answer)
Closed 4 years ago.
I recently installed Cygwin64 and I am trying to run a C program which takes input from the keyboard using getchar
#include <stdio.h>
// copies input to output
int main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
I have used stty -a to check what keyboard signals do what and EOF is correctly mapped to ctrl+D however it does nothing
I have read that there may be a conflict with MinGW which I am also using
My solution is switching to using the Cygwin terminal inside VS Code which instead uses windows keyboard signals
It's still Ctrl+D. You have a typo in your code. You are not assigning the return value of getchar to c. Remove an equals sign.
while ((c = getchar()) != EOF)
Related
This question already has answers here:
How to send EOF via Windows terminal
(3 answers)
Closed 5 years ago.
I am working through K&R's exercises right now and I am at the one where you count the number of blanks, spaces, and tabs using the C language. I have built the following code:
#include <stdio.h>
#include <stdlib.h>
/*Write a program that counts blanks, tabs, and newlines*/
int main()
{
int c, numblanks, numtabs, numnewlines;
numblanks = 0;
numtabs = 0;
numnewlines = 0;
printf("Enter some text and press \"Enter\"\n");
while ((c = getchar()) != EOF) {
if (c == ' ')
++numblanks;
if (c == '\t')
++numtabs;
if (c == '\n')
++numnewlines;
}
printf("The total number of blanks is %i\n", numblanks);
printf("The total number of tabs is %i\n", numtabs);
printf("The total number of new lines is %i\n", numnewlines);
}
I am using Codeblocks and the built-in GCC compiler that is installed with it on a Windows 10 OS. When I run the program, I type some text into the program window that pops up and press "Enter" and nothing happens. I am not sure why. I was wondering if someone could help me get another pair of eyes on my code to see if there is something I am missing. Here is an image of what happens in the program when I run it:
Program window with typed text, but no reaction
I have not tried your solution but sometimes this can happen when the program is finished, the terminal closes. Try to put a getchar at the end of your program.
printf("Enter key to exit");
getchar();
This can help after string input (but sometimes require creating loop with getchar() and checking the result of this function)
#include <stdio.h>
int main(){
int c, nl=0;
while((c = getchar()) != EOF)
if(c=='\n')
nl++;
printf("%d",nl+1);
return 0;
}
On Ubuntu 18.04, GCC 7.3.0
There's no output on console when the getchar() comparison is made with EOF. it works fine with other characters. Using CTRL^D exits the program with 'code 0' without any output on console. I've tried running it in bash but still no output.
input:
line 1
line 2
line 3
line 4
line 5
expected output:
5
actual output:
program exited with code 0
On *nix systems EOF is generated by Ctrl^D whereas on Windows system EOF is generated by Ctrl^Z. I am assuming that you are using Windows system. See https://en.wikipedia.org/wiki/End-of-file
These programs are meant to read input from file. so on bash if you do this:
./linecount < textfile.txt
will give you the output
5
but i guess in editors using ctrl^D to generate/trigger EOF character doesn't work well.(At least not on mine).
This question already has answers here:
Why does program not execute final printf statement?
(2 answers)
Closed 6 years ago.
I've been trying to learn some C language via "The C Programming Language by BRIAN W KERNIGHAN & DENNIS M. RITCHIE", and I've got a question that I cannot understand.
Here's the deal, in section 1.5 (page 17) related to character counting of an input, here's my code:
#include <stdio.h>
int main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc);
printf("%0.f\n", nc);
}
This part:
printf("%0.f\n", nc);
Should print the actual character counter, right? The problem is that it gives me exactly nothing. I've been trying it via Code Blocks and also via terminal by doing "cc code.c", and all it does it just waits for me to put and input, and just nothing more.
Am I missing something here?
Thanks in advance,
Anoubis
You are trapped here getchar() != EOF if you only input random text input.
The program wait for EOF - End Of File
I copied your code into a.cand compiled it using gcc a.c -o a.out
If a run ./a.out I get the behaviour you describe until I hit Ctrl+D which corresponds to EOF in my terminal.
The program will print the number of chars received once it has received EOF.
Another way to use the code is to pipe another file to it.
Create a dummy file named blaha.txtand write something in it.
You can then pipe it to the program like this:
a.out < blaha.txt
Your program waits for input and consumes it until it reaches the end of file. You can signal the end of file from the terminal by typing a special character such as control-Z followed by enter on Windows and control-D on linux and MacOS.
This question already has answers here:
End of File (EOF) in C
(3 answers)
Closed 7 years ago.
When I use this code, I can type into the command line and get back what I typed:
main() {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
}
Output:
~/code/c $ ./a.out
one
one
two
two
But when I use this code, it only works when I pipe in data, not when I type data into the command line:
main() {
int nc;
nc = 0;
while (getchar() != EOF) {
nc++;
}
printf("%d\n", nc);
}
When I pipe data in from a text file:
~/code/c $ ./a.out < practice.txt
14
When I try to input data from the command line:
~/code/c $ ./a.out
one
two
three
What's going on?
The while loop never exits since while (getchar() != EOF) is always true. After you're done with the input, press Cntrl+D for Linux or Ctrl+Z for Windows to indicate EOF.
Like everyone mention, you program doesn't stop. The while loop needs a condition to stop. you have to either use CTRL+Z (Win) or CTRL+D(Unix) to stop it, When you pipe it actually sends that character for you. Because EOF is END_OF_FILE. So when you hit the end of your file. the OS sends that character for you.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
EndOfFile in C - EOF
I was trying examples from K&R. I am not able to understand why this code does not exit unless ctrl+c is pressed.
int main ( )
{
int c;
c = getchar();
while(c!=EOF)
{
putchar(c);
c=getchar();
}
}
Any help is appreciated. Thanks!
EDIT: Using Windows (Visual Studio 2010)
In Windows, you generate end of file from the standard input stream by pressing Ctrl+Z. Depending on the buffering behavior, you might also need to press Return.
EOF is End of File. If you read from 'keyboard', you should compare to End of Line symbol which is equal to press Return
int main ( )
{
int c;
c = getchar();
while(c!= '\n')
{
putchar(c);
c=getchar();
}
}
On Windows machine ctrl+c acts as delimiter of character scanning same as EOF
on this loop gets broken otherwise it will keep looking for characters