-1

I was reading Kernighan Ritchie and there's this Character counting program, so I tried implementing

#include <stdio.h>
#include <stdlib.h>

int main()
{
char c;
int i;
c = getchar();
while (c != EOF)
    i= i + 1;
printf("%d",i);
}`

When I compile and run this code, after I enter some characters there's no output after that. No number is printed and I can't figure out why. The code looks fine. I also tried using scanf() but the same thing happened.

The next example was for counting lines in input and the same problem was there too.

7
  • 4
    Please be aware that C++ is not C. Commented Feb 20, 2017 at 14:58
  • 1
    if c!=EOF "while" loop will be infinite Commented Feb 20, 2017 at 15:02
  • 3
    int i = 0, c; while ((c = getchar()) != EOF) i = i + 1; Commented Feb 20, 2017 at 15:03
  • 4
    Please do not wildly edit your question after people have posted answers. Now neither your question nor the answers make any sense at all. Commented Feb 20, 2017 at 15:04
  • @Lundin Noted and sorry. Commented Feb 20, 2017 at 15:06

4 Answers 4

7

In your code, when you're hitting

 i= i + 1;

the initial value of i is indeterminate. So, the whole program invokes undefined behavior. You need to initialize i.

To elaborate, i being an automatic local variable, unless initialized explicitly, the content is indeterminate. Using an indeterminate value in this case will lead to UB.

That said, a char is not able to hold a value of EOF, change the type to int.


After that, you're wrong in the logic. getchar() is not a loop on it's own, you need to keep calling getchar() inside the while loop body to update the value of c, used in the condition check in while.

Sign up to request clarification or add additional context in comments.

2 Comments

oops, yes I assigned 0 to i but that didn't solve the problem and neither did changing type of variable c to int. I have edited the question.
... and getchar returns an int, not a char.
2
int main()

Good. Not main() and not void main(). Keep it this way.

{
char c;

Bad. getchar() returns an int.

int i;

Bad. Not initialised, value is indeterminate. You are going to increment -7445219, or perhaps a kidney pie, who knows.

c = getchar();

Ok you have read a single character.

while (c != EOF)

But you cannot compare it to EOF because EOF doesn't fit in a char.

    i= i + 1;

Looks like you forgot to do something in the body of the loop. Something that is going to change c perhaps so that your loop has a chance to finish.

printf("%d",i);

It is recommended to add a newline to the last line of your output.

}

That's all folks.

Comments

1

You only need to add an inicialization for "i"

int i = 0;

Comments

0

You missed the initialization:

int i = 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.