getline (string) in C++

getline() is a standard library function in C++ and is used to read a string or a line from input stream. It is present in the <string> header.

Prototype Syntax:

  1. Syntax 1:
     istream& getline (istream& is, string& str, char delim);

    is : It is an object of istream class and tells the function about the stream from where to read the input from.
    str : It is a string object, the input is stored in this object after being read from the stream.
    delim : It is the delimitation character which tells the function to stop reading further input after reaching this character.

  2. Syntax 2:
    istream& getline (istream& is, string& str);

    The second declaration is almost same as that of the first one. The only difference is, it does not accept any delimitation character. This function consider new line or (‘\n’) character as the delimitation character.

So basically, what the getline function does is extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.
Note: The previous stored value in the string object str will be replaced by the input string if any.

What does getline() returns?

As we can see in the prototype declared above, the return type of this function is also of type istream. This functions return the same input stream as is which it accepts as parameter.





// C++ program to demonstrate getline()
#include <iostream>
#include <string>
using namespace std;
int main ()
    string str;
    cout << "Please enter your name: \n";
    getline (cin, str);
    cout << "Hello, " << str << " welcome to GfG !\n";
    return 0;



Harsh Agarwal


Hello, Harsh Agarwal welcome to GfG!

Related Articles:

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.