Enter two variables in C: 1. Declare the variable; 2. Use cin to receive input; 3. Press Enter to end the input.
How to use C to input two variables
In C, you can use the following steps to input two variables:
1. Declare variables
First, you need to declare the types of these two variables, for example:
<code class="cpp">int num1, num2; // 声明两个整型变量</code>
2. Use cin to receive Input
Use the cin >>
operator to receive the user's input from standard input and store it in a variable, for example:
<code class="cpp">cout << "请输入第一个数字:"; cin >> num1; cout << "请输入第二个数字:"; cin >> num2;</code>
3. Press the Enter key to end the input
After the user enters two numbers, press the Enter key to end the input.
Example:
<code class="cpp">#include <iostream> using namespace std; int main() { int num1, num2; cout << "请输入第一个数字:"; cin >> num1; cout << "请输入第二个数字:"; cin >> num2; cout << "输入的两个数字是:" << num1 << " 和 " << num2 << endl; return 0; }</code>
In this example, the program first declares two integer variables num1
and num2
. Then, use cout
to output a statement that prompts the user to enter a number. Use cin
to receive two numbers entered by the user. Finally, use cout
to output the two numbers entered.
The above is the detailed content of How to input two variables in c++. For more information, please follow other related articles on the PHP Chinese website!