C++ Quick Start: Input, Output, and Variable Declaration

Table of Contents

  1. What are Input and Output?
  2. Output
  3. Variable Declaration
  4. Input
  5. 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: Output example

Input: Input example

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:

  1. The first character cannot be a digit.
  2. Only letters, digits, and underscores are allowed; no other punctuation or special characters (except underscore).
  3. No special symbols.
  4. 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: Output result

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

Tags: C++ Input Output Variables Beginner

Posted on Thu, 20 Aug 2026 16:35:50 +0000 by cap2cap10