C: Why does the "Enter" key trigger output in this code? -


here's code exercise 1-13 in "the c programming language":

#include <stdio.h>  int main() {     int c, currentindex, currentlength;      currentlength = currentindex = 0;      while ((c = getchar()) != eof){         if (c == '\t' || c == '\n' || c == ' '){             if (currentlength == 0){                 continue;             }              printf("length of word %d: ||", currentindex);              (int = 0; < currentlength; i++){                 putchar('-');             }             putchar('\n');              currentlength = 0;             ++currentindex;          } else {             ++currentlength;         }        }      return 0; } 

so can compile , run ./a.out, when press "enter" start new line of input ('\n') runs printf() , putchar() functions(and neither ' ' or '\t' trigger output). while loop doesn't end (it ends should end-of-file(ctrl-d)) i'm wondering why these functions being called when are. prevents input of multiple lines @ time. here's example of it's output:

    how long these words     length of word 0: ||---     length of word 1: ||----     length of word 2: ||---     length of word 3: ||-----     length of word 4: ||----- 

just clear, output printf() , putchar() when press "enter". ctrl-d ends loop , program.

getchar() default in buffered mode, characters not given program until enter pressed. duplicate of question: how avoid press enter getchar()


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -