programmeurNED
programmeurNED

Reputation: 43

C beginner for-loop output issue

I am new to C (self-learning). When I execute this for-loop function when I type 'hello' the output will be 012345. However it skips to last print function outside of the for-loop?

int main() {

    int c;


    for (c = 0; getchar() != EOF; ++c) {
        printf("%d\n", c);
    }

    printf("%d\n", c);

    return 0;
}


The output:

hello
0
1
2
3
4
5

Upvotes: 1

Views: 343

Answers (5)

isrnick
isrnick

Reputation: 736

The while loop doesn't end when you type hello and presses Enter, because getchar() still won't be equal to EOF. So the loop will continue with getchar() waiting to read new characters.

You need to press Ctrl+Z on Windows' cmd.exe, or Ctrl+D on Linux's terminal, to proceed with the execution of the program without writing any character to the input, then getchar will fail to read a character and return EOF, finally exiting the loop. Once it is out of the loop the final printf call will be made before ending the program.

Alternatively you could change the condition to make the loop end when getchar reads a new line character \n, instead of when it fails to read a character, so that the loop will end after you press Enter:

int main() {

    int c;


    for (c = 0; getchar() != '\n'; ++c) {
        printf("%d\n", c);
    }

    printf("%d\n", c);

    return 0;
}

Upvotes: 0

c300g97
c300g97

Reputation: 21

I see what you're tryng to do, but first of all i'd declare "c" as a char , this way you're doing an implicit cast to char, which is allowed in C language, but should be avoided.

My best guess would be that you'r compiler (i suppose mingw on windows) is not telling you what's going on exactly, try this:

When you build (compile) something outside linux for C language do as follows :

" gcc -o nameoftheexecutable file.c -Wall " and to execute it " ./nameoftheexecutable

That -Wall will print out all warnings to the console, including EOF warnings.

By doing this, and understanding what IDE or Environment are you programming in, we can help you out.

Upvotes: -1

"When I execute this for-loop function and I type 'hello' the output will be 012345".

What do you expect else? c just get incremented by each iteration, starting at 0.

"However it skips to the last print function outside of the for-loop?"

No, it doesn´t. In fact, It isn´t even out of the for loop. The newline made by the press to Enter doesn´t let you break out of the loop; only a signalized EOF is doing that.

So, the program isn´t even terminated. It stucks in the loop.

You need to signalize EOF with CTRL + D on Linux or CTRL + Z on Windows to break out of the loop.

Upvotes: 1

Jabberwocky
Jabberwocky

Reputation: 50832

Change your program to this:

#include <stdio.h>

int main() {    
  int c;
  int ch;

  for (c = 0; (ch = getchar()) != EOF; ++c) {
    printf("%d %d\n", c, ch);
  }

  printf("%d %d\n", c, ch);    
  return 0;
}

Now you should understand yourself what happens.

Upvotes: 1

Miradil Zeynalli
Miradil Zeynalli

Reputation: 503

No it is not skipping anything.

Note: you are printing c which is integer (not character, if that is what you want). So, input "hello" has 5 characters: so, output is:

0
1
2
3
4

Then, as there is newline character '\n' as you pressed enter (there is your output 5). However, you loop never exits, unless you force program to do so (I mean, interrupting with Ctrl +C). If you, test again your code, and input data even after "hello", you will see that, you are still in the loop.

Upvotes: 1

Related Questions