how \n is different from other charecters??

This is a discussion on how \n is different from other charecters?? within the C Programming forums, part of the General Programming Boards category; Code: while((c = getchar()) != EOF) { putchar(c); } no we know that if we enter charecters then all get ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    46

    how \n is different from other charecters??

    Code:
    while((c = getchar()) != EOF) {
              putchar(c);
    }
    no we know that if we enter charecters then all get buffered in buffer called stdin. and we know that even 'enter' is also a charecter. i.e '\n'. so when we enter \n instead getting stored in buffer it prints all the charecters which are there in the buffer. so how this 'enter' charecter is different from other charecters???? what is the functionality of this charecter???

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    getchar and putchar are actually working in quick succession. The only reason you see a string after you press enter is because the stdout stream has been flushed. It's not something that '\n' is doing, really, but it is a valuable hint.

    Whenever you switch from an output operation to an input operation, your output buffer should be flushed to force older text to be displayed. And because getchar is line buffered, it's pretty simple to guess the appropriate time to do that, so stdout is flushed implicitly. If you want to be sure of it though, you need to call fflush yourself.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    when we press enter button then it means we typed '\n'. does it carry any extra information to the kernal to signal to flush the stdin?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    No "extra" information is provided by '\n'. '\n' represents a newline and nothing more.

    Take for example, files. Files may contain newlines, but flushes are less frequent for two reasons: we don't have to wait for user action (a) and flushing when the buffer is full or the file is closed makes writes to the hard drive more efficient (b). The periodic flushing of stdout is clearly by design, then.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    ok..ok.... now its clear....

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd like to clarify something here:

    When you use getchar() to read input, the OS will "hold" the line until you press enter. Only after you hit enter does the getchar() function get to see the stuff you have typed.

    There are various ways (different for each OS) to tell the OS that you want to see things as they happen (aka RAW) rather than line-buffered (aka COOKED). Of course, that's also a bit like going to the shops buying the ingredients versus going to a restaurant to get the meal served on a plate - in the line-buffered mode, you get all the tricky stuff of handling delete, backspace and other such editing that the OS supports. You have to handle ALL that yourself if you read directly, because you are being sent exactly what is typed - so if the user hits backspace, it gives you a backspace.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop to count the charecters of a string
    By simpatico_qa in forum C Programming
    Replies: 11
    Last Post: 04-24-2009, 03:51 AM
  2. Integers and charecters
    By anilk in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 03:56 AM
  3. C - Reading first 20 charecters from file
    By ThoughtRiot in forum C Programming
    Replies: 7
    Last Post: 11-03-2002, 07:09 PM
  4. how do i store charecters with spaces?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21