Fundamental programming concepts like basic Input And Output Function In C enable a computer to communicate with the user by receiving data and showing the results. Streams or standard library functions are usually used to manage input and output activities in C and C++.
For I/O, streams are used in C Language and C++. Basically, a stream is a series of bytes. A C or C++ program’s standard input, output, and error streams are automatically opened when it starts executing. Standard output, error, and input streams are usually connected to the computer screen and keyboard, respectively.
Basic Input and Output Function in C
Formatted console I/O is usually done with printf() and scanf() in C. To use these functions, include <stdio.h> the header file from the standard I/O library.
printf() (Formatted Output)
- The standard output device, often the monitor, is used to show formatted data using the printf() function. The first argument it receives is a format control string that defines the display of the data. In addition to conversion specifications that begin with a % character and specify where and how the values of succeeding arguments should be entered, this format string may contain literal characters to be printed.
- For example, %d for integer, %f for float, %c for character, and %s for string are common conversion parameters.
- Syntax: printf(“<format string>”, <List of Variables>);
- As an example:
- Printing a message and the value of an integer variable x is demonstrated in this example. To advance the pointer to the next line, use the n escape sequence, which represents a newline character. Sequences of escapes start with a backslash (\).
scanf() (Formatted Input)
- In order to store formatted data in variables, the scanf() function reads it from the standard input device, which is often the keyboard. It determines what kind of data is anticipated using a format control string, just as printf().
- Syntax: scanf(“<Format String>”, &variable);
- The format string includes conversion parameters such as %d for integers, %f for floats, and %c for characters.
- The address operator (&) is usually used before the variable name (e.g., &variable) since scanf() requires the address of the memory place where the data should be saved. When reading strings, you do not require the & for arrays.
- As an example:
- This application asks the user to enter an integer, uses scanf() to receive the input, and then uses printf() to display the value that was entered.
Basic I/O in C++
C++ handles basic I/O via std::cin and std::cout. Defined in the <iostream> standard header file specifies these things.
std::cout (Standard Output Stream)
- The standard output stream is used to display output using std::cout. With the stream insertion operator (<<), it functions.
- As an example:
- Using std::cout, this straightforward program prints “Hello, world!”
std::cin (Standard Input Stream)
- Input from the standard input stream is read using std::cin. The stream extraction operator (>>) controls how it operates. Depending on the type of variable being read into, the >> operator responds to reading input automatically.
- As an example:
- This application asks the user for their first name, uses std::cin to read the input into a std::string variable, and then outputs a greeting that contains the name that was submitted.