What is standard input/output? Explanation in the past questions of the programming test!

What is standard input/output?

If you google what standard input/output is, many sites explain that “standard input = keyboard input, standard output = screen output”. The explanation itself is fine, but why not dig a little deeper?

In this article, I will explain what standard input/output is and explain standard input/output using a programming test problem as an example.

Table of contents

  1. What is standard input/output?
  2. Challenge programming test examples!
  3. Answer example in C language (standard input/output)
  4. Solution example in Java (standard input/output)
  5. summary

What is standard input/output?

What is standard input/output?

A program is “something that takes some input, calculates and edits the input, and outputs the result”. At first, imagine a program like the one below, where you type something on the keyboard and the result is displayed on the screen.

What do you use to enter information into your computer? It’s a keyboard, right? Input from the keyboard is actually called “standard input”.

To be precise, it requires a little more explanation.

Imagine that when your program runs, there is a port into which the information you want flows. This input is called standard input . If you don’t do anything in particular, its mouth is connected to input from the keyboard. Therefore, there is no problem even if general standard input = keyboard input.

Standard input is more than just a keyboard

Standard input is not necessarily the keyboard.

It is also possible to connect a file to standard input, since the keyboard is only specified in the mouth of standard input. Is it easier to imagine that the file is “flowed” into the standard input?
You can save keystrokes in a file and use them as program input. When there is a large amount of input, it is good to write it in a file and use it as the standard input, because there is no need to input it and there are no mistakes.

what is standard output

Let’s go back to the example program where you type something and the result is displayed.
Just like standard input, there is standard output. Think of it as an exit through which the output of your program flows. This is standard output .
Screen output is specified for standard output if nothing is done. Therefore, if you do not understand well, you can think that standard output = screen display, but there is no problem for the time being.

Standard output is not just the screen

In fact, standard output is not limited to the screen.
The execution result can be directly written to a file . In other words, it is possible to specify a file instead of the screen for standard output.
If the standard output is set to a file, nothing will be displayed on the screen even if the program is executed. Instead, the results are output to a file with the specified file name.
(Such switching of the output destination is called redirection .)

Challenge programming test examples!

Now that we know what standard input/output is, let’s solve the standard input/output problem! This is an example of TOPSIC , a
programming skill assessment service .

example

Takahashi wants to process the data.

You are given integers a,b,c and a string s .
Display the calculation result of the integer a+b+c and the string s side by side.

Constraint:
1 ≤ a, b, c ≤ 1,000
1 ≤ |s| ≤100 0

Input is given in the following format.
a
b c
s

Output a+b+c and s in one line separated by spaces .

When I type:

1
2 3
test

It should print the result like this:

6 tests

Checkpoints for Solving Examples

This is a checkpoint for thinking.
・How to get the keyboard input value?
・How to get a number divided into multiple lines?
・How to get numbers separated by spaces (blanks)?
・How to recognize the input value as a number or a character string?
・How to display the result in one line?

The above are common points regardless of language. How to respond depends on the language.

Answer example in C language (standard input/output)

First, let’s solve it in C language.

#include<stdio.h>
int main()
{
int a,b,c;
char s[101];
// integer input
scanf(“%d”, &a);
// space-separated integer input
scanf( “%d %d”,&b,&c);
// string input
scanf(“%s”,s);
// output
printf(“%d %s\n”,a+b+c,s) ;
return 0
;

In C language, “scanf” is used to receive the contents of standard input. By specifying the format when receiving, you can receive it as a numerical value.
For multi-line correspondence, simply divide it into two lines and get it. The following points. I will omit the comments.

scanf(“%d”, &a);
scanf(“%d %d”,&b,&c);

It receives a as a number, and b and c as numbers. The space-separated numbers on the second line can be handled simply by specifying the format as “%d %d”. Acquisition of character strings can be handled in the same way.
And here is the resulting output: Use printf . Outputs a+b+c as a number and s as a string.

printf(“%d %s\n”,a+b+c,s);

Solution example in Java (standard input/output)

Now let’s solve it in Java .

import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);   //
integer input
int a = sc.nextInt();
delimited integer input
int b = sc.nextInt();
int c = sc.nextInt();
// string input
String s = sc.next();
// output
System.out.println((a+ b+c) + ” ” + s )
;

Unlike the C language mentioned earlier , Java is an object-oriented language. Classes and objects are coming out.

To receive standard input, use the Scanner class. Create an sc
object from the class with the new operator and use the nextInt method to get the input value. The nextInt method recognizes spaces and newlines as separators between input values . So simply running nextInt three times will get you the three values ​​indicated in the question . It doesn’t matter if it’s a newline or a space. Specifically, it is the following part.

int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();

So how can we receive a string instead of a number? There is a method called next for receiving a string, so use that.

String s = sc.next();

Finally, the program is completed by displaying the added value and the entered character string in one line.

System.out.println((a+b+c) + ” ” + s);

summary

I explained what standard input/output is, and then challenged a problem related to standard input/output. Looking at the problem, it’s interesting to see the differences between languages, even though the basic idea is the same.

If you are interested, please try other problems using standard input/output.

Leave a Comment