Counting characters in C language

Click For Summary

Discussion Overview

The discussion revolves around a C code snippet intended to count characters from user input until an end-of-file (EOF) marker is encountered. Participants explore the functionality of the code, its expected behavior, and the discrepancies between the code's description and actual output.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant questions the accuracy of a programming book's claim that the code counts characters, suggesting it does not function as expected.
  • Another participant asserts that the code should work as intended and asks for more specific details about the problem encountered.
  • Several participants request sample inputs and expected versus actual outputs to better understand the issue.
  • Some participants clarify that the program requires the input to be terminated with an EOF marker, with specific instructions for both Linux (Ctrl-D) and Windows (Ctrl-Z or F6).
  • One participant explains that the code could be part of a file reading function and discusses the nature of EOF as a special value used to indicate the end of input.
  • A later reply acknowledges a misunderstanding regarding the EOF marker and confirms that the suggested key combinations work.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of using an EOF marker to terminate input, but there is disagreement regarding the initial claim about the code's functionality and its alignment with the book's description.

Contextual Notes

Some participants express uncertainty about the specific inputs and outputs, which may affect the understanding of the code's behavior. The discussion also highlights the importance of distinguishing between character input and EOF in the context of the program's operation.

physics kiddy
Messages
135
Reaction score
1
Please explain what this C code will do :

#include <stdio.h>
/* count characters in input; 1st version */
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}

'The C Programming' Book says that this code counts characters but it doesn't do the same ... why ?
Thanks
 
Technology news on Phys.org
The code should do what it says it will do. You need to be more specific about what your problem is.
 
I assume you have compiled it and you have seen numbers printed by the program. What were they?

Place your programs in [noparse]
Code:
[/noparse] tags to make them easier to read:

Code:
#include <stdio.h>
/* count characters in input; 1st version */
main()
{
  long nc;
  nc = 0;
  while (getchar() != EOF)
    ++nc;
  printf("%ld\n", nc);
}
 
Please tell us:

1. a sample input to the program
2. what you expected the output to be
3. the actual output

This is more precise and helpful for us, than a purely verbal description of your problem.
 
When I type in anything, say, any number or any character, it just displays the character or whatever once again in the next line. I expect the program to count the number of characters I have typed. Like :

If I type 'Physics', it must count the number of characters(7) in the word Physics. But it does do that...
 
physics kiddy said:
When I type in anything, say, any number or any character, it just displays the character or whatever once again in the next line. I expect the program to count the number of characters I have typed. Like :

If I type 'Physics', it must count the number of characters(7) in the word Physics. But it does do that...

Try typing some characters and ending it with control-z.
 
It'll do, but you have to finish the input with EOF - end of file marker.

Under Linux (or any other *nix) it means entering your text, then pressing Ctrl-D. I think in Windows command line it'll be Ctrl-Z.

Edit: uart beat me. That's what happens when you google to check if it really is Ctrl-Z.
 
For Windows , pressing f6 gives the same result (EOF marker).
 
You can test what character is the program expect:
Code:
main()
{
long nc;
nc = 0;

printf("%c\n",EOF);   //Here output nothing

while (getchar() != EOF)
++nc;
printf("%ld\n", nc);     //Never be executed
}
As we saw output EOF is nothing,so none of inputs would be terminated while loop.
getchar() accept a character,which can be present as an ASCII code(One byte,Range:0~255).
But,EOF means of "End Of File",the value is -1.which used to judge when reading a file.

So,this program could be one part of file reading function,and exactly counts characters in file.
 
  • #10
I`m wrong,Ctrl-Z(or F6) worked.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
2K
  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
14
Views
4K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 66 ·
3
Replies
66
Views
6K
Replies
5
Views
2K