Using EOF and getchar() to count the number of characters - c

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.

Related

No output on a character count while loop

I follow the book C programing second edition and try to output this code:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n", nc);
}
and I can't figure out why I don't have any output.
I work on crunchbang++ and to create the output I entered in the terminal:
cc -ansi file.c
then
./a.out
but when I enter characters I have a blank response.
I don't want just the working code, but an explanation, because I really want to learn how it works.
#include <stdio.h>
int main(void)
{
long num_of_chars = 0;
while(getchar() != '\n'){
++num_of_chars;
}
printf("%ld\n", num_of_chars);
}
EOF means End of File.
EOF is not a character. It is a state which indicates no more characters to read from a file stream. When you enter EOF command from the terminal, you are signalling the OS to close the input stream, not putting in a special character.
Now either you can change your code to read a file, emulate EOF, or cancel while loop on different character.
Above example will end while loop on new line.
$ ./main
This is a test
14
Edit:
Tested on linux with EOF.
Ctrl + Z will outright stop the program.
Ctrl + D will output num_of_chars if pressed twice or by doing Enter and then Ctrl + D.
Edit 2:
With EOF.
printf foo | ./your_program Output: 3
echo foo | ./your_program Output: 4 (echo adds newline caharacter)

unexpected result when counting word length in c using for loop

i am learning c language, and the program is about counting no of characters.
here is code
#include <stdio.h>
int main(void) {
// your code goes here
double nc;
for (nc=0;getchar() != EOF;nc++);
printf("%.0f\n", nc);
return 0;
}
input
''
input in none.
the output which I am getting is 1.
online compiler result
shouldn't be output equal to 0, not 1.unable to understand why is this coming.
thanks
if you put a bit more effort in yor programming adding couple of lines of code everything would be clear:
#include <stdio.h>
int main(void) {
// your code goes here
int nc;
int c;
for (nc=0;(c = getchar()) != EOF;nc++)
{
printf("The char is '%c' code: 0x%02x\n", c >= 32 ? c : '.', c);
}
printf("%d\n", nc);
return 0;
}
https://ideone.com/jfGK7h
And the mistery is solved. You have pressed the enter in the ideone input box and you have a new line there.
How did you input that input?
If you hit the <enter> key at the keyboard, then you got a single \n char, leading to that response.
Try this:
$ a.out
<Ctrl-D>
0
$ _
($ is the prompt, and <Ctrl-D> is the way to produce no input from a unix terminal) Of course, a.out is the name of your program (you didn't show how it is called)
BTW, why do you end the output in a \t in printf() ??? \t is a tab character, not a new line.... 8-.

printf("%.0f\n", nc) did not print intented info

I am following instructions to write a program of characters counting:
#include <stdio.h>
main()
{
double nc;
for (nc=0; getchar() != EOF; ++nc);
printf("%.0f\n", nc);
}
After it was compiled and run,
$ ./a.out
ff
fdg
fd
fdr
It did not print the counting.
What's the problem with my code?
I'm pretty sure you never enter EOF.
Use Control+Z on Windows or Control+D on UNIX/Linux/OSX to get EOF.

Character Counter from "The C Programming Language" Not Working As I Expected

I am reading through "The C Programming Language", and working through all the exercises with CodeBlocks. But I cannot get my character counter to work, despite copying it directly from the book. The code looks like this:
#include <stdio.h>
main(){
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
When I run the program, it opens a window I can type in, but when I hit enter all that happens is it skips down a line and I can keep typing, but I think it's supposed to print the number of characters.
Any idea what's going wrong?
This line:
while (getchar() != EOF)
means that it keeps reading until the end of input — not until the end of a line. (EOF is a special constant meaning "end of file".) You need to end input (probably with Ctrl-D or with Ctrl-Z) to see the total number of characters that were input.
If you want to terminate on EOL (end of line), replace EOF with '\n':
#include <stdio.h>
main(){
long nc;
nc = 0;
while (getchar() != '\n')
++nc;
printf("%ld\n", nc);
}
Enter is not EOF. Depending on your OS, Ctrl-D or Ctrl-Z should act as EOF on standard input.
I ran into the problem tonight, too. Finally found out that Ctrl-D on Linux worked. You build the source file using cc, and start the program and input a word, then press Ctrl-D twice when finished typing. The number that the program countered will be printed just behind the very word you just typed, and the program terminates immediately. Just like this:
The above answer provided by nujabse is correct. But recently coming across this issue myself and researching the answer, I would like to add why.
Using Ctrl+C tells the terminal to send a SIGINT to the current foreground process, which by default translates into terminating the application.
Ctrl+D tells the terminal that it should register a EOF on standard input, which bash interprets as a desire to exit.
What's the difference between ^C and ^D

count blanks from the input

I wrote a program to count blanks. I can compile it and run it, it's fine. But why it does not display the count?
#include<stdio.h>
main()
{
int count=0;
int c;
while((c=getchar())!=EOF)
{
if(c == ' ') count++;
}
printf("%d\n",count);
}
Your exact code (errors and all) works as you'd expect at ideone.
How do you terminate the input? To send an EOF signal to your program from the console type, at the beginning of a line, CtrlD in Linux or CtrlZ in Windows.
Also try to run with redirected input. Something like
yourprog < data.txt
or
echo one two three four | yourprog
You're probably not getting the EOF that you expect from input. You may be expecting the Enter key to be EOF, which will not happen. Have you tried using one of the ctrl+ combinations such as Z or D (depending on OS) to send the EOF ?

Resources