C Programming

What is stdin in C Language

C is a versatile programming language that provides users with different features, such as simple keyword sets, easy writing style and low-level memory access. Because of these characteristics, C is one of the most widely used programming languages in a variety of operating systems. There are many keywords, functions and data types used in C programming languages. They are all beneficial for various uses. Among them, there is a pointer to file type keyword called “stdin” used inside a C code.

If you are unsure about stdin in C Language, you can follow this article for detailed guidelines.

What is stdin in C Language

The term “stdin” stands for “standard input” and is used in the C language to refer to the default location from which a computer program will read data. This data can come from a keyboard, file, modem, or another source. The stdin is the standard way for programs to obtain their data and is a critical component of many applications.

You can use stdin in different ways, including providing input to a program when it is first run, such as a username and password for authentication purposes, updating a program’s parameters, such as changing the flags. Further you can use it in interactive command line interfaces.

When a C program is executed, it will look for its stdin in the same place each time. It often originates from the keyboard buffer on the computer, although it can also come from a file. stdin can be redirected to another source by the operating system, either before or during the execution of the program.

There are multiple ways we can use to input and print strings using stdin, which are as follows:

Method 1: The scanf() Function

The scanf() function stands for Scan Formatted String. It receives information from the standard input stream (stdin), which is often the keyboard, and then writes the outcome into the specified parameters. It takes user-provided character, string, and numeric data through standard input. Like printf, scanf() also makes use of format specifiers.

#include <stdio.h>

int main()
{
    char book[20];
    printf("Enter your favorite book: ");
    scanf("%s", book);
    printf("Your favorite book is: %s.", book);
    return 0;
}

 

In this code, the user is first prompted to enter the name of the book. Then the user enters the name on the console and it is read using scanf() function and then printed on the screen using printf() function.

Output

Method 2: The fgets() Function

The fgets() method is used to read a text line or string from the provided file or terminal and after that saves it in the corresponding string variable.

#include <stdio.h>

int main()
{
    char str[50];
    printf("Enter any string up to 50 characters: ");
    fgets(str, 50, stdin);
    printf("You entered: %s.", str);
    return 0;
}

 

The user is initially asked to input a string with up to 50 characters in this code. After entering the string on the console, it is read using the fgets() function, written on the screen using the printf() function, and then the user can exit the program.

Output

Method 3: The getchar() and putchar() Functions

The getchar() and putchar() functions are the one used for reading and writing a character from the standard I/O devices, respectively. The getchar() function reads the next available character from the keyboard and returns it as an integer. It is by-default defined in the stdin.h header. Only one character is read by this function at a time. The character supplied as input is shown on the screen and is returned by the int putchar() method. This function only outputs one character at once. This technique may be used in the loop if you want to display many characters on the screen. Check out the sample below.

#include <stdio.h>
int main() {

   int s;
   printf("Enter a string :");
   s = getchar();
   printf("\nYou entered: ");
   putchar(s);
   return 0;
}

 

The above piece of code compiles and runs while it waits for you to enter some text. As soon as you enter text and hit Enter, the software scans just one character using getchar() and displays it using putchar() as seen below.

Output


 

Method 4: The gets() and puts() Functions

A C program can read data from stdin by using the function “gets ()”. This gives back a reference to a character array (the buffer), which contains the data from stdin. In addition to reading data from stdin in the C language, you can also write data to it. By using the “puts ()” function, you can output data to the stdin. This is useful for writing debug information or other interactive messages.

#include <stdio.h>
int main() {

   char str[50];
   printf("Enter a value :");
   gets(str);
   printf("\nYou entered: ");
   puts(str);
   return 0;
}

 

In this piece of code, gets() function reads the input string and puts() function prints that string in the output.

Output:


 

Conclusion

stdin is an essential part of any C program. It is the standard way for applications to obtain data and has many uses. Whether used for authentication, reading in parameters, or writing debug information. Understanding how to use stdin in the C language and its accompanying header files is crucial for any programmer. The above-mentioned guidelines also show four methods to write input and get output using stdin.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.