#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
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.
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-.
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.
I have a very simple C example program that does a crude count of characters words and spaces from input. The program compiles without error but when tested the program doesn't return any of the int variables via the print function. I am using VS2012 for coding and compiling. Stepping into the code shows that the values are being calculated correctly. Is there something wrong with my code or the compiler?
#include <stdio.h>
#define IN 1
#define OUT 0
/* count digits, white space, others */
main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF){
++nc;
if(c == '\n'){
++nl;
}
if (c == ' ' || c == '\n' || c == '\t'){
state = OUT;
} else if (state == OUT){
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
It works if you run it with stdin coming from a file a.exe < test.txt. It works if you run it with stdin coming from the console on Linux. It does not work if you run it with stdin coming from the console on Windows.
It must be some sort of Windows console oddity.
In order to see the actual output, start the debugger with Ctrl-F5. This will keep the console window open.
See this answer for more information.
Your loop is permanent. Meaning it doesn't end.. so it doesn't reach the call of printf.
EOF is indicator that clearly doesn't work like that here. You must break the loop on a specific key. Like enter for instance, which character decimal representation is 13 for carriage return. 10 for NL.
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