The getline function uses the realloc function to automatically increase the memory block as required, ensuring that there is never a space shortage. This is one of the explanations why getline is safe. The value returned inside the second parameter will also inform us of the new block size. It returns -1 if an error appears, such as reaching the end of a file without receiving any bytes. Getline functions cease reading input from the stream when they meet a newline character or the end of a file.Syntax
The syntax for getline() function is shown in the next line
Explanation of Syntax
Because size “t” is an unsigned integral type, it will not return a negative value. It is mostly used for indexing and calculating objects in an array. This argument indicates the size of the memory block referred to by the first parameter in bytes. “**string” is a character array double-pointer. This specifies the place of the character array’s initial character. It will have the line read by getline function in it. “*n” is a pointer to just a variable that keeps the array’s size. “FILE *stream,” i.e., stdin is the stream from which the file will be read. It is the entity pointer that represents the stream from which characters are read. The input file descriptor is stdin.
Example for getline() function in C
Now we have an illustration to understand better the getline() function in the C programming language. We are implementing this illustration on Ubuntu 20.04 operating system. However, any other operating system can be utilized for this purpose. The terminal of the Ubuntu 20.04 operating system can be accessed by “Ctrl+Alt+T” or by looking for it in the applications. Once you get it, execute the following nano instruction to create a file. We have named it
“nano getl.c” however, the choice of name is entirely dependent on you.
There will be no output of this instruction, but we will get access to the GNU nano editor of the Ubuntu 20.04 operating system. You have to write the attached code in the file. You can modify it once you understand the functionality. Save this file and exit it.
The getline() method is defined in the stdio.h header file. The getline() function takes three arguments and returns the number of characters we entered. The size_t type characters’ variable is used to hold the return value. The malloc() function is defined in the stdlib.h header file. The string input is saved in the memory location pointed to by the pointer buffer declared earlier in the code. Use the size_t variable type, which is an integer of a particular type. The getline() function requires this. The buffer size is set to 32 characters in the code above. The value of buffer, bufsize, and then stdin is used by the getline() method for standard input.
The printf() function outputs a text string to the stdout screen on the terminal. It’s one of the most common ways to output a string. Once you close the file, now the time comes when you have to execute the code. In Ubuntu 20.04 operating system, GCC compiler is utilized for code compilation and execution. We have already installed it. If you don’t have a GCC compiler in Ubuntu 20.04 operating system, you can install it by “Sudo apt install GCC instruction. Now run the listed command.
As such, there will be no output of this instruction. Now execute the listed command to get the output of the above-attached code:
You can verify that we got no error. The system prompted us to enter something, so we entered “Kalsoom”. The number of characters that were read from the entered string will be displayed in the output.
This guide has demonstrated the concept and usage of the getline() function in C programming. We have explained the syntax of this function so that you can use it accordingly in your programs. The practical example is explained well, and its execution has been shown to users to get a wider view of the getline() function in C programming. Now, you can use this example in your code where required.