Counting characters in C language

In summary: This code will count the number of characters in a given string. The code will not work as described if you do not enter an end of file marker. For example, if you type in a string and then press Ctrl-D, the code will still count the characters in the string, but the string will not be terminated.
  • #1
physics kiddy
135
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
  • #2
The code should do what it says it will do. You need to be more specific about what your problem is.
 
  • #3
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);
}
 
  • #4
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.
 
  • #5
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...
 
  • #6
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.
 
  • #7
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.
 
  • #8
For Windows , pressing f6 gives the same result (EOF marker).
 
  • #9
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.
 

1. What is C programming?

C programming is a high-level, general-purpose programming language developed by Dennis Ritchie in the early 1970s. It is a powerful language that is widely used for developing operating systems, compilers, and other system software.

2. What are the benefits of learning C programming?

Learning C programming can have many benefits, including gaining a strong understanding of computer architecture and memory management, being able to write efficient and fast code, and having the ability to work with low-level programming languages.

3. Is C programming difficult to learn?

C programming can be challenging to learn at first, especially for those with no prior programming experience. However, with dedication and practice, it can be a rewarding language to master.

4. What is the difference between C and other programming languages?

C programming is a low-level language, meaning it is closer to machine code than other high-level languages like Python or Java. This gives programmers more control over how their code is executed, but it also requires a deeper understanding of computer architecture.

5. What can you do with C programming?

C programming can be used for a wide range of applications, including developing operating systems, embedded systems, and graphic user interfaces. It is also commonly used in industries such as finance, engineering, and gaming.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
5
Views
882
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
15
Views
5K
Back
Top