Very simple C program not doing anything (character counting)

In summary, this conversation discusses a program in C that is supposed to count characters, but it does not seem to work when run. It is suggested to try sending the input using the keyboard shortcuts ctrl-z or ctrl-D, as the program may be expecting an end of file signal. It is also mentioned that the program may not work on certain machines without a raw terminal.
  • #1
bakin
58
0

Homework Statement


Code:
#include <stdio.h>
main()
{
    long nc;

    nc=0;
    while (getchar() !=EOF)
        ++nc;
    printf("%ld\n", nc);
}


Homework Equations





The Attempt at a Solution



I'm trying to learn C and am using the book "C Programming Language". It says that the above program counts characters. But, when I run it and enter something, it just skips down to the next line without doing anything. There's a similar code in the next section that counts lines, and it does the same thing. Am I missing something?
 
Physics news on Phys.org
  • #2
You are probably trying this on a machine without a raw terminal.
On windows (in a cmd prompt) or any unix shell it will buffer a line of text before sending it to the program.

Try typing something and then doing ctrl-z (windows) or ctrl-D (unix) to signal end of file and send the input to your program.

On unix you can set the terminal to raw mode for this example, it's a bit confusing but worked perfectly well with a raw teletype in 1979 when C was written.
 

Related to Very simple C program not doing anything (character counting)

1. How can I fix my C program if it is not counting characters?

There could be several reasons why your C program is not counting characters. First, make sure you have included the necessary header files and initialized any variables you are using for counting. Additionally, double check your logic and make sure you are using the correct functions and syntax for character counting. If you are still having trouble, try debugging your program or seeking help from a more experienced programmer.

2. Why is my C program not displaying the correct character count?

If your C program is displaying an incorrect character count, it could be due to a logical error in your code. Check to see if you are properly incrementing your character count variable and if you are including all necessary characters in your count. It may also be helpful to use a debugger to step through your code and see where the issue is occurring.

3. Is there a specific function I should use for character counting in C?

There are a few different functions you can use for character counting in C, such as strlen() and strchr(). It ultimately depends on your specific program and what you are trying to achieve. Make sure to read the documentation for these functions to understand their usage and see which one would be most appropriate for your program.

4. Can I count characters in C without using any built-in functions?

Yes, it is possible to count characters in C without using any built-in functions. You could create your own function or loop through a string and manually increment a counter variable. However, using built-in functions can often make the process more efficient and less prone to errors.

5. How can I improve the efficiency of my character counting program in C?

One way to improve the efficiency of your character counting program in C is to use pointers instead of array indexing. Pointers can be faster and use less memory, especially when dealing with large strings. Additionally, make sure to properly free any allocated memory and avoid unnecessary loops or function calls.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
710
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
929
Back
Top