<!DOCTYPE html>
	<html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
	<head>
<title>&#91;C++ Cơ bản&#93; Phần 6&#x3A; Biến global và biến local</title>
<meta name="description" content="&#91;C++ Cơ bản&#93; Phần 6&#x3A; Biến global và biến local - Savefile - Tin Tức -...">
<meta name="author" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG">
<meta name="copyright" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG [webmaster@tinhocdct.xyz]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="&#91;C++ Cơ bản&#93; Phần 6&#x3A; Biến global và biến local">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;tinhocdct.xyz&#x002F;savefile&#x002F;hoat-dong-chuyen-mon&#x002F;c-co-ban-phan-6-bien-global-va-bien-local-98.html">
<meta property="og:site_name" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG">
<meta property="og:url" content="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-6-bien-global-va-bien-local-98.html">
<link rel="shortcut icon" href="https://tinhocdct.xyz/favicon.ico">
<link rel="canonical" href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-6-bien-global-va-bien-local-98.html">
<link rel="alternate" href="https://tinhocdct.xyz/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tin-tuc/" title="Tin Tức - TIN TỨC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/thong-cao-bao-chi/" title="Tin Tức - THÔNG BÁO" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/ban-tin-noi-bo/" title="Tin Tức - BẢN TIN NỘI BỘ" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tin-cong-nghe/" title="Tin Tức - CÔNG NGHỆ - KHOA HỌC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/thu-thuat/" title="Tin Tức - THỦ THUẬT" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tien-ich/" title="Tin Tức - TIỆN ÍCH" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/ai/" title="Tin Tức - AI" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/khoa-hoc/" title="Tin Tức - KHOA HỌC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-chuyen-mon/" title="Tin Tức - HOẠT ĐỘNG CHUYÊN MÔN" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-cong-doan/" title="Tin Tức - CÔNG ĐOÀN" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-doan-the/" title="Tin Tức - ĐOÀN THỂ" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tuyen-sinh-huong-nghiep/" title="Tin Tức - TUYỂN SINH - HƯỚNG NGHIỆP" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tuyen-sinh-2025/" title="Tin Tức - TUYỂN SINH" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/huong-nghiep/" title="Tin Tức - HƯỚNG NGHIỆP" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/lich-cong-tac-tuan/" title="Tin Tức - Lịch công tác tuần" type="application/rss+xml">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/jquery/jquery.min.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/language/vi.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/global.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/news.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/main.js">
<link rel="preload" as="script" href="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1816161148536198">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/bootstrap.min.js">
<link rel="StyleSheet" href="https://tinhocdct.xyz/assets/css/font-awesome.min.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/bootstrap.non-responsive.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/style.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/news.css">
<style>
	body{background: #fff;}
</style>
	</head>
	<body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG</h2>
		<p class="pull-right"><a title="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG" href="https://tinhocdct.xyz/">https://tinhocdct.xyz</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>&#91;C++ Cơ bản&#93; Phần 6&#x3A; Biến global và biến local</h1>
		<ul class="list-inline">
			<li>Thứ tư - 07/04/2021 01:11</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
			Ta định nghĩa scope (phạm vi) là các khu vực của một chương trình. Các biến của chương trình C++ có thể được định nghĩa tại 3 vị trí:
		</div>
				<div class="imghome">
			<img alt="&#91;C++ Cơ bản&#93; Phần 6&#x3A; Biến global và biến local" src="https://tinhocdct.xyz/uploads/news/2021_04/cpp-co-ban-1-3.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<em>Phần trước:&nbsp;<a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-5-bien-va-kieu-du-lieu-97.html" target="_blank">&#91;C++ Cơ bản&#93; Phần 5: Biến và kiểu dữ liệu</a></em>
<ul>
	<li>Bên trong một khối lệnh (ví dụ như hàm&nbsp;<code>int main()</code>&nbsp;của chương trình). Các biến như vậy được gọi là biến local (local variable, nhiều tài liệu sẽ dịch là biến địa phương).</li>
	<li>Trong phần định nghĩa của một hàm. Các biến như vậy được gọi là tham số của hàm đó (parameter).</li>
	<li>Ở bên ngoài tất cả các hàm. Các biến như vậy được gọi là biến global (global variable, nhiều tài liệu sẽ dịch là biến toàn cục).</li>
</ul>

<p>Hàm, chương trình con và tham số của hàm sẽ được trình bày trong một bài viết khác. Trọng tâm của bài viết này là về biến global và local.</p>

<h1 id="biến-local">Biến local</h1>

<p>Biến local được định nghĩa trong một khối lệnh, và chỉ tồn tại bên trong khối lệnh đó. Khi khối lệnh được thực hiện xong thì biến local cũng sẽ biến mất. Biến local cũng không thể truy cập được từ bên ngoài khối lệnh của nó.</p>

<p><img alt="" height="133" src="https://tinhocdct.xyz/uploads/news/2021_04/hinh-2.png" width="550" /><br  />
<span style="font-size:24px;">Biến global</span></p>

<p>Biến global được định nghĩa bên ngoài các khối lệnh. Biến global có thể được định nghĩa ở bất cứ vị trí nào trong chương trình (tất nhiên, bên ngoài khối lệnh), tuy nhiên thường ta quy chuẩn đặt chúng ở đầu chương trình. Chúng sẽ giữ nguyên giá trị của mình trong suốt quá trình chạy của chương trình, và tất cả các hàm hay chương trình con của chương trình đều có thể truy cập vào chúng.<br  />
<img alt="" height="144" src="https://tinhocdct.xyz/uploads/news/2021_04/hinh-1.png" width="550" /></p>

<p>Trong một chương trình, ta có thể khai báo những biến local và global trùng tên nhau. Khi ta truy cập vào tên của chúng, biến local sẽ được sử dụng trong hàm mà nó được định nghĩa.</p>

<p><img alt="" height="157" src="https://tinhocdct.xyz/uploads/news/2021_04/hinh-3.png" width="550" /><br  />
<span style="font-size:24px;">Output:</span></p>

<pre>
<code>20
</code></pre>

<h1 id="giá-trị-mặc-định-của-các-biến">Giá trị mặc định của các biến</h1>

<p>Khi một biến local được khởi tạo, chương trình C++ sẽ lấy một ô nhớ bất kỳ của hệ thống để dành cho nó. Điều này dẫn tới việc biến local không có giá trị mặc định cụ thể, và ta bắt buộc phải tự tay đặt giá trị cho chúng.</p>

<p>Khi một biến global được khởi tạo, tất cả các bit biểu diễn của nó sẽ có giá trị bằng 0. Điều này có nghĩa là:</p>

<ul>
	<li>Các kiểu biến số có giá trị đều bằng 0</li>
	<li>Các kiểu chữ cái sẽ có giá trị đều bằng kí tự&nbsp;<code>&#039;\0&#039;</code>&nbsp;(kí tự null)</li>
	<li>Các con trỏ đều có địa chỉ NULL (sẽ nói trong các bài sau)</li>
	<li>Vân vân…</li>
</ul>

<p>Tuy nhiên, việc khởi tạo các biến một cách hợp lệ vẫn là một điều nên làm, để tránh việc các tác nhân bên ngoài (ví dụ: một bộ dịch không theo quy chuẩn này) có thể làm ảnh hưởng tới chương trình.</p>

<p><em>Phần sau:&nbsp;<a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-7-input-output-99.html" target="_blank">&#91;C++ Cơ bản&#93; Phần 7: Input - Output</a></em></p>
		</div>
				<div id="author">
						<p>
				<strong>Tác giả bài viết:</strong>
				Thanh Sơn
			</p>
						<p>
				<strong>Nguồn tin:</strong>
				cowboycoder.tech
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-6-bien-global-va-bien-local-98.html" title="&#91;C++ Cơ bản&#93; Phần 6&#x3A; Biến global và biến local">https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-co-ban-phan-6-bien-global-va-bien-local-98.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG
		</div>
		<div id="contact">
			<a href="mailto:webmaster@tinhocdct.xyz">webmaster@tinhocdct.xyz</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://tinhocdct.xyz/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<div id="run_cronjobs" style="visibility:hidden;display:none;"><img alt="" src="/index.php?second=cronjobs&amp;p=i8U2K6sL" width="1" height="1" /></div>
<script src="https://tinhocdct.xyz/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_is_recaptcha=0;</script>
<script src="https://tinhocdct.xyz/assets/js/language/vi.js"></script>
<script src="https://tinhocdct.xyz/assets/js/global.js"></script>
<script src="https://tinhocdct.xyz/themes/default/js/news.js"></script>
<script src="https://tinhocdct.xyz/themes/default/js/main.js"></script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1816161148536198"
     crossorigin="anonymous"></script>
<script src="https://tinhocdct.xyz/themes/default/js/bootstrap.min.js"></script>
</body>
</html>