C Programming

Getline function in C

Getline is the newest and most popular function for reading a string of text (). The getline() function is part of the C library. This function accepts a string from the input stream as an input, so getline() is a better option. The concept of pointers is used by getline(). For reading text, the getline method is the ideal way. The getline method reads a full line from a stream, such as a newline character. To finish the input, use the getline function to generate a stop character. The command will be completed, and this character will be removed from the input. We all have the cin object to take user input; however, the cin object doesn’t allow us to receive user input in many lines; therefore, we can use the getline() function to take input from the input stream in several lines or a string till a delimiter character is discovered.

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

size_t getline (char **string, size_t *n, FILE *stream);

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. We have named the code file “nano getl.c” however, the choice of name is entirely dependent on you.

$ nano getl.c

Here is the code example:

#include <stdio.h>
#include <stdlib.h>

int input(char *s, int length);

int main( ) {

    char *buffer;
    size_t bufsize = 32;
    size_t characters;

    buffer = (char *)malloc(bufsize * sizeof(char));
    if ( buffer == NULL )
    {
        perror("Unable to allocate buffer\n");
        exit(1);
    }

    printf("Type something: ");
    characters = getline(&buffer, &bufsize, stdin);
    printf("%zu characters were read.\n", characters);
    printf("You typed: '%s'\n", buffer);

    return 0;
}

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.

$ gcc getl.c

As such, there will be no output of this instruction. Now execute the listed command to get the output of the above-attached code:

$ ./a.out

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.

Conclusion

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.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content