Table of Contents
- What are Input and Output?
- Output
- Variable Declaration
- Input
- Practice Exercise
1. What are Input and Output?
Input and output refer to the display of information on the screen (output) and the inforamtion provided by the user to the program (input) during the execution of C++ code.
Outpput:

Input:

2. Output
Output is very simple. There are two common ways:
Method 1: Using printf (requires <cstdio> header)
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
printf("content to output");
return 0;
}
Method 2: Using cout (requires <iostream> header)
#include <iostream>
using namespace std;
int main() {
cout << "content to output" << endl;
return 0;
}
3. Variable Declaration
Declaring a variable follows this pattern:
variable_type variable_name;
Common Variable Types:
| Keyword | Description |
|---|---|
| int | Integer |
| short | Short integer |
| long | Long integer |
| long long | Very long integer |
| char | Character |
| float | Floating-point number |
| double | Double-precision floating-point |
| long double | Extended-precision floating-point |
Variable Naming Rules:
- The first character cannot be a digit.
- Only letters, digits, and underscores are allowed; no other punctuation or special characters (except underscore).
- No special symbols.
- Must not conflict with reserved keywords.
Assigning a Value to a Variable:
#include <iostream>
using namespace std;
int main() {
int a;
a = 5;
cout << a << endl;
return 0;
}
Output:

4. Input
Input is another way to assign a value to a variable, where you provide the value to the program.
Method 1: Using scanf (requires <cstdio> header)
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int a;
scanf("%d", &a);
return 0;
}
Method 2: Using cin
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
return 0;
}
5. Practice Exercise
Which of the following variable names is correct? (Feel free to post your answer in the comments!)
- A:
1haobianliang - B:
这是一个变量 - C:
gggggg_gggg - D:
j{u- - E:
666