Standard Input (stdin), Output (stdout), and Error (stderr) Streams

Standard-Input-stdin-Output-stdout-Error-stderr-in-C

Standard Input (stdin), Output (stdout), and Error (stderr) Streams Overview:

So far we have looked at the basic building blocks of the C language and We also looked at the different Operators in C. In this article, We are going to look into the Standard Input (stdin) Output (stdout) and Error (stderr) Streams in C Programming Language.

In UNIX-like systems such as Linux, Everything is a file. So the input stream is also a file. This will be associated with a file descriptor.

A file descriptor is a number, which is associated with an open file.

Every program which is started will be associated with three open files ( Three streams).

  • Standard Input ( stdin) – File descriptor value Zero (0)
  • Standard Output ( stdout) – File descriptor value 1
  • Standard Error ( stderr)- File descriptor value 2

Standard Input or stdin stream:

The standard input is input stream, Which is used to provide the input to your programs.

Programs get the standard input from the input devices like Keyboard. ( There are other options like using the < input redirection operator in Linux)

In the C programming language, We have many built-in library functions to get the input from the user. Here are a few of them.

  • scanf
  • getchar
  • gets

scanf function in C Language:

The scanf function is used to read the input from the user. scanf function reads the input from the standard input stream and formats it as specified in the format string.

The scanf function is available as part of the stdio.h header file.

syntax:

The scanffunction takes the variable number parameters as input.

We can also represent the above syntax as:

The first argument of the scanf function is a format string, Which contains the desired format specifiers, If we want to read one Integer from the standard input (stdin) then we need to use the %d format specifier.

The arguments after the formatString are the variables, The data read from the stdin will be stored in these arguments. We need to specify the arguments as per the format string. If we want to read only one Integer from the stdin then format string will be %d and the following argument will be one variable

📢 The scanf function returns the number of items read.

Here is the scanf function example, Here we are reading an Integer from the standard input (stdin) and storing the value into the variable a

A couple of things to Note:

  • We specified the formatString as %d ( Because we are expecting an Integer value)
  • The argument &a contains the & ( address-of ). We need to pass the address to the scanf

We need to use the valid formatSpecifier in the formatString of the scanf function.

  • %d for the Integer Values.
  • %f for the Float values.
  • %c for the Character data.
  • %s for the string data, etc

📢 Here is the complete list of the format specifier in the C language https://sillycodes.com/format-specifiers-in-c-format/

Example to understand scanf function in C language:

Let’s look at an example. We want to write a program that takes two input numbers from the user and calculates the sum of given numbers.

Program:

Output:

We used gcc compiler to compile the code, learn more about compiling the c program at – https://sillycodes.com/compiling-c-program-in-linux-or-unix/

There are other variants of the scanffunctions like fscanf, sscanf, etc which are used to read the data from the file and strings respectively.

The getchar function in C:

The getcharfunction is used to read a character from the standard input( stdin)

Syntax:

The getchar function won’t take any parameters. It reads a character as an unsigned char and return it as int , In case of error it returns the EOF

Program to understand the getchar function:

In the following program, We are going to read a character from the stdin using the getchar function.

Getchar Program:

Program Output:

As you can see the getcharfunction read a character from the stdinand stored in to the Character variable ch

📢 The getchar in-built function is useful to read a single character from the standard input

We also have one more function called getcwhich is similar to the getchar

gets function in C Language:

The gets function is useful to read a line from the standard input. The reading stops if the new line or EOF is encountered.

📢 getscan read multiple strings. The space won’t stop the gets reading, Only the Newline or EOF stops.

Syntax:

The gets function takes a buffer as an argument(Here str is the buffer) and the Data read from the standard input is stored in the provided buffer( str)

📢 The gets won’t do any Boundary-checks for the provided buffer.

So we need to be careful how much data we are reading from the stdin and Make sure to have enough capacity in the buffer

Program to understand the gets function in C:

Write a program to read a line from the stdin using the gets function.

Program:

Program Output:

As we can see, We can read multiple strings using the getsfunction.

Standard Output or Stdout:

The standard Output stream is used to display the data from the computer to the Output devices such as Consoles.

In the C programming language, We have a few built-in library functions to get the output data from the program. Here are a few of them.

  • printf
  • putchar
  • puts

printf function in C language:

The printf function is used to display the formatted output on the output devices(Console).

printf function reads the data from the standard output stream and formats it as specified in the format string.

The printf function is available as part of the stdio.h header file.

Syntax:

The print function takes the variable number parameters as input.

We can also represent above syntax as:

The first argument of the printf function is format string, Which contains the string with desired format specifiers.

The arguments after the formatString are the variables, The values of these arguments will be replaced in the formatString to create the final Output string.

📢 The printf function returns the number of characters written on to the console.

Here is an printf function example, Here we are printing the value of the Interger num on to the Console using the printf function.

The formatString is an character string, Which if needed can contains the required format specifiers to get the desired formatted output. In the above example, The value of num i.e 10 is replaced in the formatString

We need to use the valid formatSpecifier in the formatString

  • %d for the Integer Values.
  • %f for the Float values.
  • %c for the Character data.
  • %s for the string data, etc

There are other format specifiers like %p, %e, etc we can use in the printffunction.

Lets look at the different types of printf functions

printf function without format specifiers:

If the formatString doesn’t contain any format specifiers then it will be printed as is on to the console.

This is simple printf function, We don’t have any format specifiers inside the formatString So whatever present in the formatString will be written onto the console.

In the above example, The string Hello World will be printed on the output device (As we don’t have any format specifiers).

printf with multiple format specifiers:

The printffunction can have multiple format specifier. Which helps us to format the output string.

Program:

Output:

In the above program, We have multiple format specifiers like %s, %d, and %f The first format specifier %s is replaced by the variable name, and second one %d is replaced by integer variable age, and The thrid format specifier %f is replaced by the variable weight

putchar function in C:

The putcharfunction is useful to display a character on to the output device.

Syntax:

The putchar function takes a argument ch and writes it out. The argument is written as the unsigned char

On the success, putchar returns the ch as the int . On error it will return EOF

Program to understand the putchar function:

Write a program to display a character variable onto the console using the putchar function.

Example Program:

Output:

puts function in C Programming:

The puts function is used to print a line or string on the console

📢 The puts can read multiple strings. The space won’t stop the puts

Syntax:

The puts function takes a character buffer as an argument (Here buf is the buffer) and prints the characters of buf on to the output device.

The puts() function returns an integer value, Which is the number of characters written to the console.

📢 The puts function adds a Trailing newline to the output.

Program to understand the puts function:

Use the puts function to display a sentence on the console.

Program:

Output:

Standard Error or StdErr:

The stderr or standard error stream is the default stream for the error messages. Which used to display the error messages.

Similar to the standard output stream( stdout) , The stderr messages are by default displayed on the output devices(Console)

This two streams stdout and stderr are useful to differentiate between the normal logs and errors.

In the real world, The standard output stdout messages are redirected to a file. The Standard error stderr messages, which are kind of fatal messages So are displayed on to console.

We can send the error messages to Standard error stream by using the fprintf function. We need to specify the stderr as the first argument. The fprintf expects a FILE * file pointer as a first argument.

Example program on stderr stream:

In the following we are going to use the fprintf along with the standard error stream file pointer stderr , So we will pass stderr as first argument and the data in the second argument character string ( formatString) is going to be written into the stderr

Output:

By default, the standard error stderris displayed on to the output console.

Standard Input (stdin) Output (stdout) and Error (stderr) Conclusion:

We have looked at the Standard Input (stdin) Output (stdout) and Error (stderr) streams in the C programming language. The Functions like printf puts are acts as the standard output functions similarly The functions like scanf gets are used for standard input.

Tutorials Index:

C Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

37 Responses

  1. […] Standard Input (stdin), Output (stdout), and Error (stderr) Streams […]

  2. […] Print the result on the console using printf function. […]

  3. […] 📢 The printf() and scanf() functions are used for standard input and standard output operations. […]

  4. […] Print the results on the console using the printf function. […]

  5. […] Print the result on the standard output i.e console. […]

  6. […] Display the results on the console using printf function. […]

  7. […] the numbers array. Use a For Loop to iterate over the array and take the input from the user using standard input and output functions ( printf and scanf) and update the array […]

  8. […] Use a For loop to Iterate through the array and update the array elements using the scanf function. […]

  9. […] Define another function called display() to print the array elements on the standard output. […]

  10. […] Use a For Loop to iterate over the array and update the numbers[i] with the scanf function […]

  11. […] Use a For Loop to iterate over the array from to size-1 and accept the user input and update the &amp;number[i] element using scanf function. […]

  12. […] The display() function is used to print the array elements on the Standard Output […]

  13. […] The display() function is used to print the array elements on the Standard Output […]

  14. […] Display the Results( Maxtrix-Z) using loops and printf function. […]

  15. […] Similar to scanf we can use the printf function with %s format specifier to print the strings on the standard Output […]

  16. […] Finally, We printed the original string ( src) and Destination string ( dest) on the console. […]

  17. […] If the subStr is not found in the mainStr, Then the return value res will contain the NULL. So display the “NOT FOUND” message on the console […]

  18. […] the user to provide the string for str and read it using the gets function. (The gets function won’t check the boundaries, so use it with […]

  19. […] Read the two strings from the user and update str1 and str2 respectively. […]

  20. […] Display the results on the console. […]

  21. […] the user to input a string and store it in the inputStr string. We are using the gets function. use it with caution as it won’t check […]

  22. […] the user to provide the input string and read it using the gets function and update the inputStr string with the given […]

  23. […] the user for the input string and read it using the gets function and update the inputStr […]

  24. […] the input string from the user using the gets function and store it in the inputStr […]

  25. […] the input string from the user using the gets function and store it in the inputStr […]

  26. […] string and character to search from the user and update the inputStr and ch variables. We use the gets function to read the […]

  27. […] Prompt the user to provide the input string( inputStr) and Read it using the gets function. […]

  28. […] character to search from the user and update the inputStr and ch variables respectively. We use the gets function to read the […]

  29. […] character to search from the user and update the inputStr and ch variables respectively. We use the gets function to read the […]

  30. […] the input string from the user and update the str string. we are using gets() function to read the […]

  31. […] two strings from the user and store them in mainStr and subStr respectively. We use the gets function to read the […]

  32. […] two strings from the user and store them in mainStr and subStr respectively. We use the gets function to read the […]

  33. […] two strings from the user and store them in mainStr and subStr respectively. We use the gets function to read the […]

  34. […] the two strings from the user and store them in mainStr and subStr respectively. We use the gets function to read the strings. The gets() function doesn’t do the boundary checks so be cautious while […]

  35. […] Take the two integer values from the user and store them in the variables num1 and num2 using scanf function.i.e use the address of […]

  36. […] user to provide the input string and update the inputStr character array/string. We are using the gets function to read the string from the […]

  37. […] for loop and store the values in the allocated memory. We then printed the integer variable on the console using another for […]

Leave a Reply