#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).
Related
I am trying to write a program for counting the number of characters in C. Below is my program:
#include <stdio.h>
int main(void){
// long nc;
// for(nc = 0; getchar() != EOF; nc++);
// printf("%ld\n", nc);
long nc;
nc = 0;
while(getchar() != EOF){
++nc;
}
printf("%ld\n", nc);
return 0;
}
When I execute the above program using the input :-
123<Enter>
then I press the control + ^d on my Mac to represent EOF, I am getting the output as 4D instead of just 4. Can anyone please tell me why I am getting D in my output?
[Turning my comment into an answer]
The "problem" is that the terminal program itself writes the output ^D as response to the Ctrl-D.
With the original output of your program (without the extra leading newline) the program writes its output 4 over the ^ written by the terminal. The (trailing) newline from the program then makes the terminal go to the next line where the shell takes over and writes it prompt.
This will make it seem like the output of your program is 4D.
As a possible solution, you might want to check the settings of your terminal program to see if its own output could be disabled.
When I comnpile And run the program nothing appears,i'm using codeblocks 17.12 please help me.
ANSWER: To make the result of printf appear you should end the cycle with ctrl+z or ctrl+d depending if you are using windows or linux.
main()
{
int nc;
for (nc=0; getchar() !=EOF; ++nc);
printf("%d\n", nc);
return 0;
}
for (nc=0; getchar() !=EOF; ++nc);
the ';' at the end of the line is perhaps not what you want, because of it the body of the for is empty
so printf("%d\n", nc); is executed only one time, not several, and for that you need first to go out of the for, so to reach EOF
What is your OS, what are you doing to have EOF ?
If you never reach EOF it is normal to have nothing print
If you are under Linux/Unix you can do echo blahblah | ./yourprog and yes in that case you will print 9 (because of the newline, 8 if echo -n blahblah)
Under Windows do not execute your code through codeblocks, execute it directly in a terminal, codeblocks and other IDE have unexpected behavior on the input/output
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.
Today, I wrote a simple piece of code that uses getchar() to count the characters you input. But when I compile it on Cygwin, it does not work. It always prints 0, but I never input anything or I can't input any characters it prints 0.
However, if I compile it with VC++6.0, it works.
#include<stdio.h>
int main(void)
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("The total of characters you inputed is %ld.\n", nc);
return 0;
}
This email thread talks about a bug that sounds much like yours, but I can't see that there are any follow-ups to it.
I would be interested to know what happened when you try
while(getc(stdin) != EOF)
and if that doesn't work, try
while(fgetc(stdin) != EOF)
All of them should work, though this page suggests there could be implementation differences between these functions.
Another thing you could try is to print the ASCII value of what you get:
printf("%d\n",(int)getchar());
Also, try piping output from a file instead of typing it in the console. Create a file input.txt, put some characters in it, and do
cat input.txt | ./program
EDIT: You write running cat and piping it works. I would say simply update your Cygwin version. You have encountered a bug. Get the newest versions of Cygwin and the compiler, and you should be good to go. Another option is to use scanf.
I'm working my way through K&R's 2nd edition, and I've been stumped with this seemingly simple example:
#include <stdio.h>
main(){
double c;
for(c = 0; ((getchar() != EOF) && (getchar() != '\n')); ++c)
;
printf("%.0f\n",c);
}
It simply isn't working correctly. I added in the (getchar() != '\n') portion to end the program when I press enter, but that doesn't really help either.
Here's some sample output, using the gcc that comes with Mac OSX 10.6 dev tools.
pool-000:Desktop user$ ./a.out
a
0
pool-000:Desktop user$ ./a.out
asdf
2
pool-000:Desktop user$ ./a.out
asfasf
3
So something is obviously wrong. I'm on page 18, if that helps. This isn't homework, this is for fun!
Thanks :)
Each call to getchar() will wait for a character to be read, so you're reading more than you think per iteration of the loop.
Also, at least in my opinion, a counter is (almost) never a double, you should use an integer type such as plain old int.
The problem with doing two "getchar()" operations is that you will read TWO get chars in the conditional test... before you get to the ++c.
Ditch the "EOF" comparison and it should work as you expect.