peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikatukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Biến toàn cục và biến cục bộ trong C++
STARTING
CONTROL STATEMENT
FUNCTION
ARRAY & POINTER
OOP
STL
ITERATORS
OTHER FEATURES
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Biến toàn cục và biến cục bộ trong C++

Trong bài này chúng ta sẽ tìm hiểu khái niệm về biến toàn cục và biến cục bộ trong C++, qua bài này sẽ giúp bạn hiểu rõ phạm vi của hai loại biến này.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Trong C++ cũng như các ngôn ngữ lập trình khác, biến có thể khai báo ở 3 nơi như sau:

  • Biến được khai báo bên trong một hàm hoặc block, ta gọi nó là biến cục bộ (local variable)
  • Biến được sử dụng như là tham số của hàm, ta gọi nó là tham số hình thức (formal parameter)
  • Biến được khai báo ở bên ngoài tất cả các hàm, ta gọi nó là biến toàn cục (global variable)

Trong bài học hôm nay chúng ta sẽ tìm hiểu về biến cục bộ (local variable) và biến toàn cục (global variable). Còn phần tham số hình thức (formal parameter) chúng ta sẽ tìm hiểu trong một bài khác.

1. Biến cục bộ trong C++

Biến cục bộ (Local variable) là biến được khai báo bên trong một hàm hoặc một block. (Tất cả những gì ở giữa dấu "{" và "}" chúng ta gọi là block).

Bài viết này được đăng tại [free tuts .net]

Biến cục bộ chỉ có phạm vi sử dụng bên trong một hàm hoặc một block. Chúng ta không thể truy cập và sử dụng biến cục bộ ở bên ngoài hàm hoặc block.

Ví dụ 1: Chúng ta khai báo biến cục bộ c bên trong 1 hàm, và biến cục bộ d bên trong 1 block.

Ví dụ
#include <iostream>
using namespace std;

int Tong(int a, int b) 
{
       int c = 0;  // c la bien cuc bo
       c = a + b;
       {
           
           cout << "Gia tri bien c ben trong block: " << c << "\n";
           int d = 40; // d la bien cuc bo khai bao ben trong mot block
           cout << "Gia tri bien d ben trong block: " << d << "\n";
       }
       cout << "Gia tri bien c ben trong ham: " << c << "\n";
       return c;   
} 

int main () {
   Tong(1,2);
   return 0;
}

Và kết quả sau khi thực thi đoạn code trên:

cplus1 PNG

Ví dụ 2: Chúng ta thử truy cập biến cục bộ bên ngoài một hàm, block

Ví dụ
#include <iostream>
using namespace std;

int Tong(int a, int b) 
{
    int c = 0;  // c la bien cuc bo
    c = a + b;
    {
           
        cout << "Gia tri bien c ben trong block: " << c << "\n";
        int d = 40; // d la bien cuc bo khai bao ben trong mot block
        cout << "Gia tri bien d ben trong block: " << d << "\n";
    }
    cout << "Gia tri bien d ben trong ham: " << d << "\n";
    cout << "Gia tri bien c ben trong ham: " << c << "\n";
    return c;   
} 

void In() {
   cout << "Gia tri cua bien c: " << c << "\n";
   cout << "Gia tri cua bien d: " << d << "\n";
 
}

int main () {
   Tong(1,2);
   In();
   return 0;
}

Và kết quả sau khi thực thi đoạn code trên:

cplus2 PNG

2. Biến toàn cục trong C++

Biến toàn cục (global variable) là biến được khai báo bên ngoài tất cả các hàm. Chúng ta có thể truy cập và sử dụng biến toàn cục ở bất kỳ đâu của chương trình. Thường thì chúng ta nên khai báo biến toàn cục trên đầu của chương trình.

Ví dụ: Chúng ta cùng xem ví dụ đơn giản có sử dụng biến toàn cục như sau:

Ví dụ
#include <iostream>
using namespace std;
int g; //g la bien toan cuc
int Tong (int a, int b) {
   {
      g = 100;
      cout << "Gia tri cua bien g ben trong block: " << g << "\n";
   }
   g = a + b;
   cout << "Gia tri cua bien g ben trong ham: " << g << "\n";
}

int main () {
   Tong(10, 11);
   return 0;
}

Và kết quả sau khi thực thi đoạn code trên như sau:

cplus3 PNG

3. Giá trị mặc định của biến toàn cục và biến cục bộ trong C++

Khi biến cục bộ được khởi tạo thì nó không được khởi tạo giá trị bởi hệ thống, chúng ta phải tự khởi tạo giá trị cho biến cục bộ.

Khi biến toàn cục được khởi tạo thì nó được hệ thống khởi tạo giá trị một cách tự động. Giá trị khởi tạo cho biến toàn cục bởi hệ thống như sau:

Kiểu dữ liệu Giá trị
int 0
char '\0'
float 0
double 0
pointer NULL

4. Biến toàn cục và biến cục bộ cùng tên.

Giả sử chúng ta có trường hợp biến cục bộ và biến toàn cục cùng tên, bên trong một hàm chúng ta muốn sử dụng biến cùng tên đó, thì giá trị của biến cục bộ hay biến toàn cục được sử dụng, chúng ta cùng tìm hiểu qua ví dụ sau:

Ví dụ
#include <iostream>
using namespace std; 
  
int g = 10; //bien toan cuc
   
int main() 
{    
    int g = 5; //bien cuc bo
    cout << "Gia tri cua bien g: " << g << "\n"; 
} 

Và kết quả sau thực thi của đoạn code trên:

cplus4 PNG

Như vậy nếu biến cục bộ và biến toàn cục cùng tên, thì hàm sẽ lấy giá trị của biến cục bộ. Vậy chúng ta muốn lấy giá trị của biến toàn cục mà trùng tên với biến cục bộ thì như thế nào?

Trong C++ cho phép chúng ta lấy giá trị của biến toàn cục trùng tên với biến cục bộ đó là sử dụng toán tử :: (scope resolution operator). Chúng ta cùng xem ví dụ sau:

Ví dụ
#include <iostream>
using namespace std; 
  
int g = 10; //bien toan cuc
   
int main() 
{    
    int g = 5; //bien cuc bo
    cout << "Gia tri cua bien g toan cuc: " << ::g << "\n";
    cout << "Gia tri cua bien g: " << g << "\n"; 
} 

Và kết quả thực thi đoạn code trên:

cplus5 PNG

5. Kết luận

Như vậy chúng ta đã cùng tìm hiểu xong phạm vi biến toàn cục và biến cục bộ trong C++.

Trong bài này chúng ta cần nhớ phạm vi sử dụng của biến toàn cục và biến cục bộ đó là biến cục bộ chỉ có phạm vi sử dụng trong một hàm hoặc một block mà biến đó khai báo, biến toàn cục có phạm vi sử dụng trong một chương trình, bất cứ hàm hoặc block nào điều có thể truy cập biến toàn cục.

Một điều cần lưu ý ở đây là dù là biến toàn cục hay cục bộ thì chúng ta cần phải khai báo nó trước khi sử dụng.

Trong bài tiếp theo chúng ta sẽ tìm hiểu về lớp lưu trữ trong C++. Các bạn nhớ theo dõi nhé.

Cùng chuyên mục:

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý chuỗi (string.h) trong C/C++

Các hàm xử lý chuỗi (string.h) trong C/C++

Thread Pools và Parallel Algorithms trong C++

Thread Pools và Parallel Algorithms trong C++

Tạo và quản lý các Multithreading trong C++

Tạo và quản lý các Multithreading trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Try, Catch, và Throw của Exception Handling trong C++

Try, Catch, và Throw của Exception Handling trong C++

Cách sử dụng Lambda Expressions trong C++

Cách sử dụng Lambda Expressions trong C++

Sử dụng weak_ptr trong C++

Sử dụng weak_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng unique_ptr trong C++

Sử dụng unique_ptr trong C++

Tổng quan về Smart Pointers trong C++

Tổng quan về Smart Pointers trong C++

Sử dụng Iterators trong STL của C++

Sử dụng Iterators trong STL của C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng trong List trong C++

[Iterator] Sử dụng trong List trong C++

[STL] Sử dụng Vector trong C++

[STL] Sử dụng Vector trong C++

Tổng quan về Iterators trong C++

Tổng quan về Iterators trong C++

Top