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

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.

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)

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

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.

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-.

output not showing up in console C

#define EOF 0
main(){
long nc;
nc = 0;
while ((getchar()) != EOF){
++nc;
printf ("%1f\n", nc);
}
}
I copied this code from "The C Programming Language", but when I run the code, it shows nothing in the console.
I'm using Mac and Eclipse.
Thanks in advance.
To print a long value in C we use:
printf("%ld", n);
And not
printf("%1d",n);//notice you are using 1 and not l
Your Console wouldn't show up anything at first! Because you haven't printed anything before you request the user for a character input using getchar(). Once you give an input say 10 , you counter will be incremented to 1 and get printed

C getchar error

I wrote the most innocuous C program but I can't get the expected result. I hope you can tell where my error is.
This is the code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int c, var4;
double var1,var2,var3;
while ((c = getchar()) != EOF) {
while (c != ':') {
putchar(c);
c = getchar();
}
//scanf("%d/%d/%d",&mm,&dd,&yy);
//scanf("%lf%lf%lf%d",&var1,&var2,&var3,&var4);
}
return 0;
}
and I'm using this file for input(command line redirection)
Name1 - Code1:
04/03/2011 4.5 5.6 9.8 145
04/03/2011 6.5 4.6 9.9 185
Name2 - Code2:
05/03/2011 4.5 5.6 9.8 135
05/03/2011 6.5 4.6 9.9 165
The error appears during while loop (I tried printf instead of putchar and it prints -1 endlessly and seems to never reach EOF)
I thinks that's all, I thank your help in advance.
You are getting one character, then going into the inner loop — which checks for ':', but not for EOF. So, unless the file ends with : (so that it will be seen by the outer loop), the inner loop will spin forever when it hits EOF.
The inner loop doesn't stop at EOF.
You're getting an error from getchar (EOF == -1) because thee file is finished. That's what it's printing endlessly.
You are not checking for EOF inside your inner getchar loop, therefore allowing it to continuously read EOF (-1) and printing that out.
I don't know what your expected result is so I cannot help you there.

Resources