C++ uses an appropriate abstraction that we call streams for performing the input and output operations in the sequential media like the system screen, the keyboard, or any file as well. Moreover, when we say the term ‘Input’, it refers to feeding some data into any program. Further, we can give input in the form of a file or through the command line. In addition, when we say the term ‘Output’, it refers to displaying some data on the computer screen, printer, or in any given file. This article will help you learn about standard functions in detail here.
C++ stdio.h
We can also perform the input and the output operations in C++ with the use of C Standard Input and Output Library i.e. cstdio, also known as stdio.h in the C language. Moreover, this library uses the streams for operating with the physical devices as well.
C++ gets()
The gets() function in C++ basically reads the characters from the ‘stdin’ and it also stores them until a newline character is initiated or the end of the file arises.
Example
This is how the gets() function actually works:
#include
#include
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
gets(str);
cout << "You entered: " << str;
return 0;
}
When we will be running the program, the output will be as follows:
Enter a string: Have a good day!
You entered: Have a good day!
C++ puts()
The puts() function in C++ basically writes down a string to the ‘stdout’.
Example
This is how the puts() function actually works:
#include
int main()
{
char str1[] = "Happy New Year 2021";
char str2[] = "Happy Birthday Dear";
puts(str1);
/*Â Printed on new line since '/n' is added */
puts(str2);
return 0;
}
When you run the program, the output will be:
Happy New Year 2021
Happy Birthday Dear
Browse more Topics under Inbuilt Functions
The scanf() Functions
The int scanf(const char *format…) function reads the given input from the standard input stream the ‘stdin’ and then scans that input on the basis of the available format.
The printf() Functions
The int printf(const char *format…) function writes the given output to the standard output stream ‘stdout’. After that, it then delivers the output on the basis of the format available.
Moreover, the format can be present in the form of a simple constant string. However, we can specify ‘%s’, ‘%d’, ‘%c’, ‘%f’, etc., for printing or reading the strings, integer, character, or the float respectively.
Example:
#include
int main( ) {
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);
return 0;
}
When we will compile and execute the code available above, it will wait for us to input some text. Moreover, then we get the following output:
$./a.out
Enter a value: eight 8
You entered: eight 8
FAQs on Standard Functions
Question 1: What are the input functions?
Answer: We use these functions to allow the transfer of the information between the computer system and the standard input or the output device.
Question 2: What is the standard input and output standard functions?
Answer: The input refers to provide the program with the data to be used within the program. Whereas, the Output refers to display the data on the system’s screen or write the data to a printer or any file.
Question 3: What is the function of puts()?
Answer: The puts(const char *str) function writes down a string to ‘stdout’ up to but not comprising the null character. Moreover, a newline character is added to the output.
Leave a Reply