02 - C++ basic grammar: input and output, variables and data types


insert image description here

1️⃣ Input and output of C++ program

✨ input operation

C++ provides a variety of ways to perform input operations, including using cin, , scanfand so on.

♬ Use cinto enter

#include <iostream>

int main() {
    
    
    int number;
    std::cout << "请输入一个整数: ";
    std::cin >> number;
    std::cout << "你输入的整数是: " << number << std::endl;

    return 0;
}

insert image description here

  • std::cinUsed to receive input from the console.
  • >>Operators are used to read data from an input stream.

cinIt is an input stream object in the C++ standard library, and the >>input value can be stored in the corresponding variable through the operator. In the above example, we used to cinget an integer from the user input and store it in numberthe variable.

♬ Use scanfto enter

#include <cstdio>

int main() {
    
    
    int number;
    printf("请输入一个整数: ");
    scanf("%d", &number);
    printf("你输入的整数是: %d\n", number);

    return 0;
}
  • scanfUsed to receive input from the console.
  • %dIs a format string that specifies that the data type of the input is an integer.
  • &numberIs the address of the variable, used to store the input value into the variable.

scanfIt is an input function in the C standard library. It specifies the type of input through a format string, and uses the &operator to obtain the address of the variable. In the above example, we used to scanfget an integer from the user input and store it in numberthe variable.

✨ output operations

C++ also provides a variety of ways to perform output operations, including using cout, , printfand so on.

♬ Use coutfor output

#include <iostream>

int main() {
    
    
    int number = 10;
    std::cout << "这是一个数字: " << number << std::endl;

    return 0;
}
  • std::coutUsed to output information to the console.
  • <<Operators are used to insert data into an output stream.

coutIt is an output stream object in the C++ standard library, and <<the operator can be used to output data to the console. In the above example, we used to coutoutput a number to the console.

♬ Use printffor output

#include <cstdio>

int main() {
    
    
    int number = 10;
    printf("这是一个数字: %d\n", number);

    return 0;
}
  • printfUsed to format output messages to the console.

  • %dis a format string that specifies that the data type of the output is an integer.

printfIt is an output function in the C standard library, and the output type and format are specified by a format string. In the above example, we used to printfoutput a number to the console.

✨ difference

  • cinand coutare the standard input and output streams of C++, and scanfand printfare the input and output functions of the C language.
  • cinand coutprovide a more advanced and safer way of input and output, you can directly use the >>and <<operator for input and output.
  • scanfand printfneed to use the format string to specify the data type of the input and output, and need to use the address of the variable to read and write the data.

In C++, it is recommended to use cinand coutfor input and output operations, because they are more convenient, intuitive and safe.

2️⃣ Namespace and using declaration

In C++, namespaces are used to organize code and avoid name conflicts. A common namespace is stdthat it contains many definitions of the standard library. To simplify coding, we can use usingdeclarations to avoid frequent use of full namespace qualifiers.

#include <iostream>
using namespace std;

int main() {
    
    
    int number;
    cout << "请输入一个整数: ";
    cin >> number;
    cout << "你输入的整数是: " << number << endl;

    return 0;
}

In the above example, we use using namespace std;the declaration so that we can directly use coutand cinwithout writing std::coutand std::cin. However, it should be noted that in large projects, in order to avoid naming conflicts, it is best to avoid using using namespace, and use namespace qualifiers to specify explicitly.

3️⃣ Variables, constants and data types

✨ variables

In C++, variables are used to store and manipulate data. Before using a variable, you need to declare the variable and specify its data type.

int main() {
    
    
    int age = 30; // 整数类型变量
    float weight = 68.5; // 浮点数类型变量
    char letter = 'A'; // 字符类型变量
    bool isTrue = true; // 布尔类型变量

    return 0;
}

In the above example, we declared several variables of different types, including integer type ( int), floating point type ( float), character type ( char), and boolean type ( bool). Through assignment, we can store specific values ​​into these variables.

✨ constant

Constants are unchangeable values ​​used to store fixed data. In C++, constconstants can be defined using the keyword.

int main() {
    
    
    const int MAX_VALUE = 100; // 整数常量
    const float PI = 3.14159; // 浮点数常量
    const char NEW_LINE = '\n'; // 字符常量

    return 0;
}

In the above example, we constdefined several constants using the keyword. The value of a constant cannot be changed after it is defined, and it is used to store data that remains unchanged during program execution.

✨ data types

C++ provides rich data types for storing different types of data. Common data types include integer types, floating point types, character types, Boolean types, etc.

♬ integer type

C++ provides integer types of different sizes, including int, short, long, long longand so on.

int main() {
    
    
    int age = 30; // 有符号整数类型
    unsigned int count = 10; // 无符号整数类型
    short distance = 1000; // 短整数类型
    long population = 7000000000; // 长整数类型

    return 0;
}

♬ Floating point type

C++ provides floating-point number types with different precisions, including float, double, long doubleand so on.

int main() {
    
    
    float weight = 68.5; // 单精度浮点数类型
    double pi = 3.14159; // 双精度浮点数类型

    return 0;
}

♬ character type

C++ uses the character type charto represent individual characters.

int main() {
    
    
    char letter = 'A'; // 字符类型
    char name[] = "John"; // 字符串类型

    return 0;
}

♬ Boolean type

C++ provides the boolean type bool, which is used to represent the truth value, and the value is trueor false.

int main() {
    
    
    bool isTrue = true; // 布尔类型

    return 0;
}

In the above examples, we have shown several common data types and how to use them. According to actual needs, choosing the appropriate data type can improve the efficiency and readability of the program.

This is a study note of input and output, variables, constants and data types in C++ basic grammar. In the next study, we will explore more features and syntax of C++ in depth. Remember to keep practicing and writing code to deepen your understanding and mastery of C++.

Guess you like

Origin blog.csdn.net/mingfeng4923/article/details/131205320