Master the use of variables in C++ from definition to practice _C language _ Scripting home

Mastering the use of variables in C++ from definition to practice

Updated: March 26, 2024 09:55:03 Author: Xiao Wan Ge,
Variables are containers used to store data values, in C++, there are different types of variables (using different keyword definitions), this article gives you an introduction to the use of variables in C++ from definition to practice records, interested friends follow the small series to see it

C++ variable

A variable is a container used to store data values.

In C++, there are different types of variables (defined using different keywords), such as:

  • int- Store integers (no decimal points), for example123 或 - 123.
  • double- Store floating-point numbers with decimal points, for example19.99 或 19.99
  • char- Stores a single character, for example'a' 或 'B'. Character values are enclosed in single quotation marks
  • string- Store text, for example"Hello World". String values are enclosed in double quotes
  • bool- Stores values with two states:true 或 false

Declare (create) variables

To create a variable, specify the type and assign a value to it:

grammar

type variableName = value;

Where type is one of the C++ types (such as int) and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to a variable.

To create a variable that should store numbers, refer to the following example:

Give an example

Create a variable named myNum of type int and assign it a value of 15:

int myNum = 15;
cout << myNum;

You can also declare a variable without assigning a value and assign a value later:

Give an example

int myNum;
myNum = 15;
cout << myNum;

Note that if you assign a new value to an existing variable, it will override the previous value:

Give an example

int myNum = 15; // myNum is 15. myNum = 10; // myNum is now 10 cout << myNum; // Output 10

Other types

Demonstrations of other data types:

Give an example

int myNum = 5; // Integer (no decimal) double myFloatNum = 5.99; // floating-point number (with decimal points) char myLetter = 'D'; // string myText = "Hello"; // String (text) bool myBoolean = true; // Boolean value (true or false)

Display variable

The cout object is used with the << operator to display variables.

To combine text and variables, use the << operator to separate them:

Give an example

int myAge = 35;
cout << "I am " << myAge << " years old.";

Add variables

To add one variable to another, you can use the + operator:

Give an example

int x = 5;
int y = 6;
int sum = x + y;
cout << sum;

C++ identifier

All C++ variables must be identified by a unique name.

These unique names are called identifiers.

Identifiers can be short names (such as x and y) or more descriptive names (age, sum, totalVolume).

Note: Descriptive names are recommended to create understandable and maintainable code:

Give an example

// OK int minutesPerHour = 60; // Yes, but it's not easy to understand what m is. int m = 60;

The general rule for naming variables is:

  • The name can contain letters, numbers, and underscores
  • Name must start with a letter or underscore (_)
  • Name is case sensitive (myVar and myvar are different variables)
  • The name cannot contain Spaces or special characters, such as! , #, %, etc.
  • Reserved words (such as C++ keywords, such as int) cannot be used as names

C++ constant

When you don't want someone else (or yourself) to change the value of an existing variable, use the const keyword (this will declare the variable as a "constant", i.e. unchangeable and read-only) :

Give an example

const int myNum = 15; // myNum will always be 15 myNum = 10; // Error: Attempt to assign read-only variable 'myNum'

You should always declare a variable as a constant when you have values that are unlikely to change:

Give an example

const int minutesPerHour = 60;
const float PI = 3.14;

A note on constants

When you declare a constant variable, you must assign a value to it:

Give an example

Like this: const int minutesPerHour = 60;

However, this will not work

const int minutesPerHour; minutesPerHour = 60; // Error

finally

This article about learning to use variables in C++ : from definition to practice is introduced to this article, more related to the use of variables in C++ please search the previous articles of the script home or continue to browse the following related articles hope that you will support the script home in the future!

Related article

  • C++实现病人就医管理系统

    C++ to achieve patient medical management system

    This article mainly introduces in detail the C++ language to achieve patient medical management system, which has certain reference value, interested partners can refer to it
    2019-01-01
  • C语言深入探索动态内存分配的使用

    C explores the use of dynamic memory allocation in depth

    How much space to allocate to the array? Do you have such a question as I did when I first learned C? This installment will talk about dynamic memory allocation, after reading this article, you may have a better understanding of memory allocation
    2022-04-04
  • 纯C语言:贪心Prim算法生成树问题源码分享

    Pure C language: Greedy Prim algorithm generation tree problem source code sharing

    This article mainly introduces the source code of the greedy Prim algorithm generating tree problem, and friends in need can refer to it
    2014-01-01
  • C语言实现学生信息管理程序

    C language to achieve student information management program

    This article mainly introduces the C language to realize the student information management program in detail, which has certain reference value, interested partners can refer to it
    2019-03-03
  • Opengl ES之FBO帧缓冲对象使用详解

    Opengl ES FBO frame buffer object usage detail

    This article mainly introduces the Opengl ES FBO frame buffer object use details,
    in need of friends can use for reference, I hope to be helpful, I wish you a lot of progress, early promotion and pay rise
    2022-09-09
  • C++中的volatile关键字及其作用

    The volatile keyword in C++ and its role

    This article introduced the volatile keyword in C++, which is used to identify variables that may be accidentally modified and that the compiler should not optimize. This article describes the functions and usage of the keyword volatile through specific code examples, helping readers better understand the application scenarios and implementation principles of the keyword in C++
    2023-04-04
  • C经典算法之二分查找法

    C Binary search method of classical algorithm

    This article mainly introduces the relevant information of the binary search method of the C classical algorithm, here provide two methods to help you achieve such a function, the need for friends can refer to
    2017-10-10
  • 关于C++的.cpp文件运行全过程

    About C++.cpp file operation process

    This article mainly introduces the whole process of C++.CPP file operation, has a good reference value, I hope to help you. If there are mistakes or incomplete areas, please feel free to comment
    2023-02-02
  • C++动态规划实现查找最长公共子序列

    C++ dynamic programming implements finding the longest common subsequence

    This article focuses on the longest common subsequence of C++ dynamic programming, where you want to maximize an indicator. In this example, you want to find the longest common subsequence
    2022-06-06
  • C++如何调用python并取返回值

    How does C++ call python and get a return value

    This article mainly introduces C++ how to call python and take the return value of the problem, has a good reference value, I hope to help you, if there are mistakes or not fully considered the place, hope to be free to advise
    2024-03-03

Latest comments