C Programming

How to read lines from stdin in C programming

The stdin is the short form of the “standard input”, in C programming the term “stdin” is used for the inputs which are taken from the keyboard either by the user or from a file. The “stdin” is also known as the pointer because the developers access the data from the users or files and can perform an action on them.

In this write-up, we will use the built-in functions of C programming that can be used to read the input by the stdin.

How to read a line from stdin in C programming

There are different built-in functions used in c programming for reading the inputs from the stdin. The functions used for reading the lines are:

  • getline()
  • getchar()
  • putchar()
  • scanf()

We will explain all these functions in detail.

getline(): This function is used to read the lines from the stdin. To understand this function, let us consider the example, we will create a text file with the name myfile2.c and write the following script:

#include <stdio.h>

#include <stdlib.h>


int main(void)

{

  printf(“Please enter a line:\n”);

  char *line =NILL;

  size_t len = 0;

  ssize_t lineSize = 0;

  lineSize = getline(&line, &len, stdin);

  printf(“You entered %s, which has %zu chars.\n”, line, lineSize -1);

  free(line);

  return 0;

}

Compile the script of file2.c using the gcc compiler:

$ gcc myfile2.c -o myfile2

Run the code using the command:

$ ./myfile2

In the above output, we can see that a line is taken from the stdin and then displayed with the count of its characters. The getline() function reads a full sentence from the stdin and allocates some memory on the heap and saves it there. In the getline() we pass the address of the memory where the line should be stored, the address of the length of the line, and the stdin. Then simply display the line and its length using the printf() function. Moreover, in the end, we used free() so that the space occupied in memory can be cleared to re-use it next time.

getchar(): The getchar() function is used to read the first character of the stdin and the putchar() is used to display the single character on the screen. The drawback of the getchar() and putchar() is that they can read and display only one character at a time but we can use a loop to display all the characters of stdin. To understand this, write the following code:

#include <stdio.h>

#include <stdlib.h>

int main( ) {

   char c;

   int i,l;

   fprintf(stdout, "Enter the string length: ");

   fscanf(stdin,"%d", &l);

   fprintf(stdout, "Enter a value :");

   for (i=0; i<=l; i++)

   {

   c=getc(stdin);

   putc(c,stdout);

}  

   fprintf(stdout, "\n");

   return 0;

}

Compile the code using the gcc compiler:

$ gcc myfile4.c -o myfile4

Execute the myfile4:

$ ./myfile4

In the above code, we input a line “Hello! It’s Linuxhint” and the getchar() reads the first character of the line, and putchar() is used to display the line. First, we have asked the user about the length of the string and then we displayed it with the help of a loop.

scanf(): The other widely used method to read the line from the stdin is using the “scanf()” function. The scanf takes the input from the stdin, then scans it and saves it in some variable or array. For example:

#include <stdio.h>

int main( ) {


   char a[100];


   fprintf( "Enter a string :");

   fscanf(stdin,"%s", a);


   fprintf( stdout,"\nYou entered the following string: %s ", a);

   fprintf(stdout,“\n”);

   return 0;

}

Using the gcc compiler, compile the program of myfile5.c to debug the errors:

$ gcc myfile5.c -o myfile5

Execute the myfile5:

$ ./myfile5

In the above script, we simply declared the array “a” with the character data type, with the help of scanf() we took the input from the stdin. We used the “%s” constant which is used to read and print the strings too. Then displayed the string stored in array a[] that is “Hello”.

Conclusion

The stdin is used for taking the input from the keyboard and it can read in different ways. There are different functions used for reading the stdin. In this write-up, we have used different functions used to read a line. The built-in function in c programming is getline() which is used for reading the lines from the stdin. But we can also use other functions like getchar() and scanf() for reading the lines.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.