![]() |
VOOZH | about |
What is a buffer?
A temporary storage area is called a buffer. All standard input and output devices contain an input and output buffer. In standard C/C++, streams are buffered. For example, in the case of standard input, when we press the key on the keyboard, it isn’t sent to your program, instead of that, it is sent to the buffer by the operating system, till the time is allotted to that program.
How does it affect Programming?
On various occasions, you may need to clear the unwanted buffer so as to get the next input in the desired container and not in the buffer of the previous variable. For example, in the case of C after encountering “scanf()”, if we need to input a character array or character, and in the case of C++, after encountering the “cin” statement, we require to input a character array or a string, we require to clear the input buffer or else the desired input is occupied by a buffer of the previous variable, not by the desired container. On pressing “Enter” (carriage return) on the output screen after the first input, as the buffer of the previous variable was the space for a new container(as we didn't clear it), the program skips the following input of the container.
In thecase of C Programming
Input:
GeeksforGeeks
a
Output:
GeeksforGeeksTime Complexity: O(1)
In thecase of C++
Input:
4
GeeksforGeeks
Output:
4Time Complexity: O(1)
In both of the above codes, the output is not printed as desired. The reason for this is an occupied Buffer. The "\n" character goes remains there in the buffer and is read as the next input.
How can it be resolved?
In thecase of C:
1. Using “ while ((getchar()) != '\n'); ”: Typing “while ((getchar()) != '\n');” reads the buffer characters till the end and discards them(including newline) and using it after the “scanf()” statement clears the input buffer and allows the input in the desired container.
Input:
GeeksforGeeks
a
Output:
GeeksforGeeks
a
Time Complexity: O(n), where n is the size of the string.
2. Using “ fflush(stdin) ”: Typing “fflush(stdin)” after “scanf()” statement, also clears the input buffer but generally it's use is avoided and is termed to be “undefined” for input stream as per the C++11 standards.
In thecase of C++:
1. Using “ cin.ignore(numeric_limits::max(),'\n'); ” :- Typing “cin.ignore(numeric_limits::max(),'\n');” after the “cin” statement discards everything in the input stream including the newline.
Input:
4
GeeksforGeeks
Output:
4
GeeksforGeeks
Time Complexity: O(1)
2. Using “ cin.sync() ”: Typing “cin.sync()” after the “cin” statement discards all that is left in the buffer. Though “cin.sync()” does not work in all implementations (According to C++11 and above standards).
Input:
4
GeeksforGeeks
Output:
4Time Complexity: O(1)
3. Using “ cin >> ws ”: Typing “cin>>ws” after “cin” statement tells the compiler to ignore buffer and also to discard all the whitespaces before the actual content of string or character array.
Input:
4
GeeksforGeeks
Output:
4
GeeksforGeeks
Time Complexity: O(1)
4.Using “ fflush(stdin) ”: Typing "fflush(stdin)" after taking the input stream by "cin" statement also clears the input buffer by prompting the '\n' to the nextline literal but generally it is avoided as it is only defined for the C++ versions below 11 standards.
Input:
369
geeksforgeeks
Output:
369
geeksforgeeks
Time Complexity: O(1)