c++中的變數型別_C ++中的變數

cunfen6312發表於2020-07-30

c++中的變數型別

Variable are used in C++, where we need storage for any value, which will change in program. Variable can be declared in multiple ways each with different memory requirements and functioning. Variable is the name of memory location allocated by the compiler depending upon the datatype of the variable.

變數用在C ++中,在這裡我們需要儲存任何值,這些值將在程式中更改。 可以用多種方式宣告變數,每種方式具有不同的記憶體要求和功能。 變數是由編譯器根據變數的資料型別分配的記憶體位置的名稱。

variables in C++

變數的基本型別 (Basic types of Variables)

Each variable while declaration must be given a datatype, on which the memory assigned to the variable depends. Following are the basic types of variables,

必須在宣告時為每個變數提供一個資料型別,分配給該變數的記憶體取決於該資料型別。 以下是變數的基本型別,

boolFor variable to store boolean values( True or False )
charFor variables to store character types.
intfor variable with integral values
float and double are also types for variables with large and floating point values
bool 用於儲存布林值的變數(True或False)
char 用於儲存字元型別的變數。
int 用於帶整數值的變數
floatdouble也是具有大和浮點值的變數的型別

宣告和初始化 (Declaration and Initialization)

Variable must be declared before they are used. Usually it is preferred to declare them at the starting of the program, but in C++ they can be declared in the middle of program too, but must be done before using them.

變數必須在使用前宣告。 通常,最好在程式啟動時宣告它們,但是在C ++中,它們也可以在程式中間宣告,但是必須在使用它們之前完成。

For example:

例如:

int i;      // declared but not initialised
char c; 
int i, j, k;  // Multiple declaration

Initialization means assigning value to an already declared variable,

初始化是指將值分配給已宣告的變數,

int i;   // declaration
i = 10;  // initialization

Initialization and declaration can be done in one single step also,

初始化和宣告也可以一步完成,

int i=10;         //initialization and declaration in same step
int i=10, j=11;

If a variable is declared and not initialized by default it will hold a garbage value. Also, if a variable is once declared and if try to declare it again, we will get a compile time error.

如果宣告瞭一個變數且預設未初始化,則它將儲存一個垃圾值。 另外,如果一次宣告瞭變數,然後嘗試再次宣告它,我們將得到編譯時錯誤。

int i,j;
i=10;
j=20;
int j=i+j;   //compile time error, cannot redeclare a variable in same scope

變數範圍 (Scope of Variables)

All the variables have their area of functioning, and out of that boundary they don't hold their value, this boundary is called scope of the variable. For most of the cases its between the curly braces,in which variable is declared that a variable exists, not outside it. We will study the storage classes later, but as of now, we can broadly divide variables into two main types,

所有變數都有其作用範圍,並且超出該範圍而不保持其值,此邊界稱為變數範圍。 在大多數情況下,它在花括號之間,其中變數宣告存在變數,而不是在變數外部。 我們將在稍後研究儲存類,但是到目前為止,我們可以將變數大致分為兩種主要型別,

  • Global Variables

    全域性變數

  • Local variables

    區域性變數

全域性變數 (Global variables)

Global variables are those, which ar once declared and can be used throughout the lifetime of the program by any class or any function. They must be declared outside the main() function. If only declared, they can be assigned different values at different time in program lifetime. But even if they are declared and initialized at the same time outside the main() function, then also they can be assigned any value at any point in the program.

全域性變數是那些一旦宣告就可以在程式的整個生命週期中被任何類或任何函式使用的變數。 必須在main()函式外部宣告它們。 如果僅宣告,則可以在程式生命週期中的不同時間為其分配不同的值。 但是,即使在main()函式之外同時宣告和初始化它們,也可以在程式中的任何位置為它們分配任何值。

For example: Only declared, not initialized

例如:僅宣告,未初始化

#include <iostream>
using namespace std;
int x;                // Global variable declared
int main()
{
    x=10;                 // Initialized once
    cout <

區域性變數 (Local Variables)

Local variables are the variables which exist only between the curly braces, in which its declared. Outside that they are unavailable and leads to compile time error.

區域性變數是僅在大括號之間宣告的地方存在的變數。 除此之外,它們不可用並導致編譯時錯誤。

Example :

範例

#include <iostream>
using namespace std;
int main()
{
    int i=10;
    if(i<20)        // if condition scope starts
    {
        int n=100;   // Local variable declared and initialized
    }              // if condition scope ends
    cout << n;      // Compile time error, n not available here
}

一些特殊型別的變數 (Some special types of variable)

There are also some special keywords, to impart unique characteristics to the variables in the program. Following two are mostly used, we will discuss them in details later.

還有一些特殊的關鍵字,以賦予程式中的變數獨特的特徵。 以下兩個是最常用的,我們將在後面詳細討論。

  1. Final - Once initialized, its value cant be changed.

    最終 -初始化後,其值將無法更改。

  2. Static - These variables holds their value between function calls.

    靜態 -這些變數在函式呼叫之間保留其值。

Example :

範例

#include <iostream.h>
using namespace std;
int main()
{
    final int i=10;
    static int y=20;
}

翻譯自: https://www.studytonight.com/cpp/variables-scope-details.php

c++中的變數型別

相關文章