mirror of
https://github.com/Aexiar/c.git
synced 2024-10-22 14:05:45 +02:00
233 lines
121 KiB
HTML
233 lines
121 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN" dir="ltr">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
|
||
<title>第一章:变量(⭐) | Hi,终于等到你</title>
|
||
<meta name="description" content="许大仙前端、Java、大数据、云原生">
|
||
<meta name="generator" content="VitePress v1.2.3">
|
||
<link rel="preload stylesheet" href="/c/assets/style.bTUc_nQv.css" as="style">
|
||
|
||
<script type="module" src="/c/assets/app.NavY-WoM.js"></script>
|
||
<link rel="preload" href="/c/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
|
||
<link rel="modulepreload" href="/c/assets/chunks/framework.BE8if9e6.js">
|
||
<link rel="modulepreload" href="/c/assets/chunks/theme.Dwe0RGAL.js">
|
||
<link rel="modulepreload" href="/c/assets/notes_01_c-basic_02_xdx_index.md.Cux_q1Jy.lean.js">
|
||
<link rel="shortcut icon" href="/c//favicon.ico">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,shrink-to-fit=no">
|
||
<meta name="keywords" content="许大仙,许大仙的博客">
|
||
<script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
|
||
<script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
|
||
</head>
|
||
<body>
|
||
<div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar has-sidebar top" data-v-ae24b3ad data-v-ccf7ddec><div class="wrapper" data-v-ccf7ddec><div class="container" data-v-ccf7ddec><div class="title" data-v-ccf7ddec><div class="VPNavBarTitle has-sidebar" data-v-ccf7ddec data-v-ab179fa1><a class="title" href="/c/" data-v-ab179fa1><!--[--><!--]--><!--[--><img class="VPImage logo" src="/c/logo.svg" alt data-v-8426fc1a><!--]--><span data-v-ab179fa1>许大仙</span><!--[--><!--]--></a></div></div><div class="content" data-v-ccf7ddec><div class="content-body" data-v-ccf7ddec><!--[--><!--]--><div class="VPNavBarSearch search" data-v-ccf7ddec><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-ccf7ddec data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://aexiar.github.io/" target="_blank" rel="noreferrer" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>首页</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://aexiar.github.io/coa6/notes/" target="_blank" rel="noreferrer" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>计组6件套</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/c/notes/" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>c/c++</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://aexiar.github.io/web-design/notes/" target="_blank" rel="noreferrer" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>前端</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://aexiar.github.io/java/notes/" target="_blank" rel="noreferrer" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>Java</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://aexiar.github.io/big-data/notes/" target="_blank" rel="noreferrer" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>大数据</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://aexiar.github.io/linux/notes/" target="_blank" rel="noreferrer" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>云原生</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://aexiar.github.io/open-software/notes/" target="_blank" rel="noreferrer" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>开源软件</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-ccf7ddec data-v-e6aabb21><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="Switch to dark theme" aria-checked="false" data-v-e6aabb21 data-v-d1f28634 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-d1f28634></span><span class="vpi-moon moon" data-v-d1f28634></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-ccf7ddec data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/Aurorxa" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-eee4e7cb><span class="vpi-social-github" /></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-ccf7ddec data-v-d0bd9dde data-v-b6c34ac9><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-b6c34ac9><span class="vpi-more-horizontal icon" data-v-b6c34ac9></span></button><div class="menu" data-v-b6c34ac9><div class="VPMenu" data-v-b6c34ac9 data-v-e7ea1737><!----><!--[--><!--[--><!----><div class="group" data-v-d0bd9dde><div class="item appearance" data-v-d0bd9dde><p class="label" data-v-d0bd9dde>Appearance</p><div class="appearance-action" data-v-d0bd9dde><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="Switch to dark theme" aria-checked="false" data-v-d0bd9dde data-v-d1f28634 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-d1f28634></span><span class="vpi-moon moon" data-v-d1f28634></span><!--]--></span></span></button></div></div></div><div class="group" data-v-d0bd9dde><div class="item social-links" data-v-d0bd9dde><div class="VPSocialLinks social-links-list" data-v-d0bd9dde data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/Aurorxa" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-eee4e7cb><span class="vpi-social-github" /></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-ccf7ddec data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-ccf7ddec><div class="divider-line" data-v-ccf7ddec></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-17a5e62e><button data-v-17a5e62e>返回顶部</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-575e6a36><div class="curtain" data-v-575e6a36></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-575e6a36><span class="visually-hidden" id="sidebar-aria-label" data-v-575e6a36> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0 collapsible has-active" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>C语言基础</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-b8d55f3b><span class="vpi-chevron-right caret-icon" data-v-b8d55f3b></span></div></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/c/notes/01_c-basic/01_xdx/" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>C 语言入门</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/c/notes/01_c-basic/02_xdx/" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>变量和进制</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0 collapsible collapsed" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>C 语言高级</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-b8d55f3b><span class="vpi-chevron-right caret-icon" data-v-b8d55f3b></span></div></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/c/notes/02_dsa/01_xdx/" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>初识 C 语言</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0 collapsible collapsed" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>C++ 核心编程</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-b8d55f3b><span class="vpi-chevron-right caret-icon" data-v-b8d55f3b></span></div></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/c/notes/03_coa/03_xdx/" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>计算机组成原理前言</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0 collapsible collapsed" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>C++ 标准库</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-b8d55f3b><span class="vpi-chevron-right caret-icon" data-v-b8d55f3b></span></div></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/c/notes/03_coa/03_xdx/" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>计算机组成原理前言</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0 collapsible collapsed" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>QT 桌面开发</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-b8d55f3b><span class="vpi-chevron-right caret-icon" data-v-b8d55f3b></span></div></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/c/notes/04_os/01_xdx/" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>操作系统前言</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-575e6a36><section class="VPSidebarItem level-0 collapsible collapsed" data-v-575e6a36 data-v-b8d55f3b><div class="item" role="button" tabindex="0" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><h2 class="text" data-v-b8d55f3b>Linux 高并发服务器开发</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-b8d55f3b><span class="vpi-chevron-right caret-icon" data-v-b8d55f3b></span></div></div><div class="items" data-v-b8d55f3b><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b8d55f3b data-v-b8d55f3b><div class="item" data-v-b8d55f3b><div class="indicator" data-v-b8d55f3b></div><a class="VPLink link link" href="/c/notes/04_os/01_xdx/" data-v-b8d55f3b><!--[--><p class="text" data-v-b8d55f3b>操作系统前言</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" data-v-3f215769 data-v-a5bbad30><div class="content" data-v-a5bbad30><div class="outline-marker" data-v-a5bbad30></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-a5bbad30>目录</div><ul class="VPDocOutlineItem root" data-v-a5bbad30 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _c_notes_01_c-basic_02_xdx_" data-v-39a288b8><div><h1 id="第一章-变量-⭐" tabindex="-1">第一章:变量(⭐) <a class="header-anchor" href="#第一章-变量-⭐" aria-label="Permalink to "第一章:变量(⭐)""></a></h1><h2 id="_1-1-程序中变化的数据" tabindex="-1">1.1 程序中变化的数据 <a class="header-anchor" href="#_1-1-程序中变化的数据" aria-label="Permalink to "1.1 程序中变化的数据""></a></h2><ul><li>在生活中,我们使用最多的不是固定的数据,而是会变化的数据: <ul><li>① 购物车商品的<code>数量</code>、<code>价格</code>等。</li><li>② 一首歌<code>播放的时间</code>、<code>进度条</code>、<code>歌词的展示</code>等。</li><li>③ 微信聊天中<code>消息条数</code>、<code>时间</code>、<code>语音的长度</code>、<code>头像</code>、<code>名称</code>等。</li><li>④ 游戏中技能的<code>冷却时间</code>、<code>血量</code>、<code>蓝量</code>、<code>buff 时间</code>、<code>金币的数量</code>等。</li><li>……</li></ul></li></ul><ul><li>下图是一个<code>购物车</code>中<code>变化</code>的<code>数据</code>,即:</li></ul><p><img src="/c/assets/1.CXNJqOOc.png" alt=""></p><ul><li>那么,在实际开发中,我们就会使用<code>变量</code>来<code>保存</code>和<code>操作</code>这些<code>变化</code>的<code>数据</code>。</li></ul><h2 id="_1-2-变量" tabindex="-1">1.2 变量 <a class="header-anchor" href="#_1-2-变量" aria-label="Permalink to "1.2 变量""></a></h2><ul><li>变量的定义:变量是程序中不可或缺的组成单位,最基本的存储单元。其实,变量就是一个存储数据的临时空间,可以向其中存储不同类型的数据,如:整数、小数、字符、字符串等,并且变量中的数据在程序运行的时候可以动态改变。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li><code>变量</code>:用来<code>存储数据</code>的<code>容器</code>。</li><li><code>数据</code>:可以是一个用来计算的<code>数字</code>,如:上文购物车中的<code>价格</code>等;也可以是一句话中的<code>关键词</code>或<code>其它任意格式的数据</code>。</li><li>变量的<code>特别</code>之处就在于<code>它存放的数据是可以改变</code>的。</li></ul></div><ul><li>我们可以将<code>变量</code>想象为一个<code>容器</code>,盒子中<code>装的</code>就是我们想要的<code>数据</code>,并且我们需要<code>给</code>盒子<code>取</code>一个<code>特别的名称</code>;通过这个<code>特别的名称</code>,我们可以<code>给</code>盒子<code>添加数据</code>或<code>移除数据</code>,这个<code>特别的名称</code>就是<code>变量名</code>。</li></ul><p><img src="/c/assets/2.E0LS08Y5.png" alt=""></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① <code>变量</code>是内存中的一个<code>存储区域</code>,该区域的数据可以在<code>同一类型</code>范围内<code>不断变化</code>。</li><li>② 通过<code>变量名</code>,可以<code>操作</code>这块内存区域,向其中<code>存储数据</code>或<code>获取数据</code>以及<code>移除数据</code>。</li><li>③ 变量的构成包含三个要素:<code>数据类型</code>、<code>变量名</code>、<code>需要存储的数据</code>。</li><li>④ 在生活中,我们会经常说:这件衣服的价格是 <code>100(整型)</code> 元,这双鞋子的价格是 <code>250.5(小数,浮点类型)</code> 元,<code>今天天气真好(字符串类型)</code>之类的话;在计算机科学中,这些都是数据,并且它们是有类型,即:数据类型。(数据类型用于定义变量所能存储的数据的种类以及可以对这些数据进行的操作的一种分类,每种数据类型都有特定的属性和用途,它们决定了变量在内存中如何表示和存储,以及变量可以执行哪些操作)</li></ul></div><h2 id="_1-3-变量的声明和使用" tabindex="-1">1.3 变量的声明和使用 <a class="header-anchor" href="#_1-3-变量的声明和使用" aria-label="Permalink to "1.3 变量的声明和使用""></a></h2><ul><li>① 变量必须先声明,后使用。</li><li>② 可以先声明变量再赋值,也可以在声明变量的同时进行赋值。</li><li>③ 变量的值可以在同一类型范围内不断变化。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>在实际开发中,我们通常都会在声明变量的同时,给其赋值,这被称为初始化。</p></div><ul><li>示例:先声明,再使用</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 声明一个整型变量,取名为 a</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 给变量赋值</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 10</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"a = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, a);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><ul><li>示例:初始化(声明变量的同时给其赋值)</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 声明一个整型变量,取名为 b ,并直接赋值(初始化,实际开发中最为常用)</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 200</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 修改变量 b 的值,将变量 a 的值赋值给变量 b</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 300</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"b= </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, b);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><ul><li>示例:同时声明多个变量并赋值</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 同时声明多个整型的变量并赋值</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c1 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 10</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, c2 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 20</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, c3 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 30</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"c1 = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, c1);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"c2 = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, c2);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"c3 = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, c3);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="_1-4-输出变量" tabindex="-1">1.4 输出变量 <a class="header-anchor" href="#_1-4-输出变量" aria-label="Permalink to "1.4 输出变量""></a></h2><ul><li>在计算机中,所谓的<code>输入</code>和<code>输出</code>都是以计算机(CPU 和内存)为主体而言的,即:</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>输入:从输入设备(键盘、鼠标、扫描仪)向计算机输入数据。</p><p>输出:从计算机向外部输出设备(显示器、打印机)输出数据。</p></div><p><img src="/c/assets/3.XcPl7d9s.png" alt=""></p><ul><li>在 C 语言中,提供了 <code>printf()</code> 函数用于输出信息,其函数声明是:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> char</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">__format</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, ...) {</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ...</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li><code>printf</code> 的标准含义是格式化输出文本,来源于 <code>print formatted(格式化打印)</code>的缩写,其语法规则,如下所示:</li></ul><p><img src="/c/assets/4.DL02VQMp.png" alt=""></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>格式化字符串:是使用双引号括起来的字符串,里面包含了普通的字符串和格式占位符。</li><li>格式占位符(格式声明符):由 <code>%</code> 和<code>格式字符</code>组成,作用是将输出的数据转换为指定的格式后输出,这里的 <code>%d</code> 表示整数。</li><li>输出列表:是程序要输出的一些数据,可以是常量、变量或表达式,需要和格式占位符一一对应。</li></ul></div><ul><li><p>在计算机中,二进制、八进制、十进制以及十六进制的英文名称和缩写,如下所示:</p><ul><li>二进制(binary),缩写是 bin。</li><li>八进制(octal),缩写是 oct。</li><li>十进制(decimal),缩写是 dec。</li><li>十六进制(Hexadecimal),缩写是 hex。</li></ul></li><li><p>其实,我们也可以在 Windows 系统中的计算器中来看到,即:</p></li></ul><p><img src="/c/assets/5.Dr67r_Ws.png" alt=""></p><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>在生活中的 decimal 是小数的意思;但是,在 C 语言中,decimal 的完整含义是 decimal integer ,即十进制整数。</p></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 声明变量并赋值</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> num </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 18</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 使用输出语句,将变量 num 的值输出,其中 %d 表示输出的是整数</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"我今年</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">岁</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="_1-5-输入数据赋值给变量" tabindex="-1">1.5 输入数据赋值给变量 <a class="header-anchor" href="#_1-5-输入数据赋值给变量" aria-label="Permalink to "1.5 输入数据赋值给变量""></a></h2><ul><li>在 C 语言中,提供了 <code>scanf()</code> 函数用于从标准输入(通常是键盘)中读取数据并根据变量的地址赋值给变量(变量需要提前声明),其函数声明是:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> scanf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> char</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">__format</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, ...) {</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ...</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>其语法规则,如下所示:</li></ul><p><img src="/c/assets/6.C5XiXNVN.png" alt=""></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p><code>&age</code>、<code>&num</code> 中的 <code>&</code>是寻址操作符,<code>&age</code> 表示变量 <code>age</code> 在内存中的地址。</p></div><div class="caution custom-block github-alert"><p class="custom-block-title">CAUTION</p><p></p><ul><li><p>① scanf() 函数中的 <code>%d</code>,如果是连着写,即:<code>%d%d</code>,那么在输入数据的时候,数据之间不可以使用逗号<code>,</code>分隔,只能使用空白字符(空格、tab 键或回车键),即:<code>2空格3tab</code>或<code>2tab3回车</code>等。</p></li><li><p>② 如果是 <code>%d,%d</code>,则输入的时候需要加上逗号<code>,</code>,即:<code>2,3</code>。</p></li><li><p>③ 如果是 <code>%d %d</code>,则输入的时候需要加上空格,即:<code>2空格3</code>。</p></li></ul></div><ul><li>示例:计算圆的面积,半径由用户指定</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> float</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"请输入一个半径:"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> scanf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%f</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">radius);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> double</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> area </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3.1415926</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"半径是</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%f</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">的圆的面积是</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%.2lf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, radius, area);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><ul><li>示例:输入一个整数值,求其绝对值</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> num;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"请输入一个整数:"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> scanf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">num);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> absNum;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (num </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"><</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> absNum </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">num;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> absNum </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> num;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">的绝对值是:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num, absNum);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><ul><li>示例:输入多个变量的值,求其乘积</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a, b, c;</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"请输入整数 a 、b 和 c 的值:"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> scanf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %d</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">c);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> result </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> × </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> × </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, a, b, c, result);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="_1-6-标识符" tabindex="-1">1.6 标识符 <a class="header-anchor" href="#_1-6-标识符" aria-label="Permalink to "1.6 标识符""></a></h2><h3 id="_1-6-1-概述" tabindex="-1">1.6.1 概述 <a class="header-anchor" href="#_1-6-1-概述" aria-label="Permalink to "1.6.1 概述""></a></h3><ul><li>在 C 语言中,变量、函数、数组名、结构体等要素命名的时候使用的字符序列,称为标识符。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>在上世纪 60 - 70 年代的时候,因为国家贫穷,人民生活不富裕等原因,家长虽然会给孩子取名为:<code>张建国</code>、<code>李华强</code>等;但是,也会取小名为<code>二狗子</code>、<code>狗剩</code>等,目的是希望孩子能健康成长(养活),像 <code>张建国</code>、<code>李华强</code>、<code>二狗子</code>、<code>狗剩</code>都是名字(标识符),伴随人的一生。</p></div><h3 id="_1-6-2-标识符的命名规范" tabindex="-1">1.6.2 标识符的命名规范 <a class="header-anchor" href="#_1-6-2-标识符的命名规范" aria-label="Permalink to "1.6.2 标识符的命名规范""></a></h3><ul><li><p>强制规范:</p><ul><li>① 只能由<code>小写</code>或<code>大写英文字母</code>,<code>0-9</code> 或 <code>_</code> 组成。</li><li>② 不能以<code>数字</code>开头。</li><li>③ 不可以是<code>关键字</code>。</li><li>④ 标识符具有<code>长度</code>限制,不同编译器和平台会有所不同,一般限制在 63 个字符内。</li><li>⑤ 严格<code>区分大小写字母</code>,如:Hello、hello 是不同的标识符。</li></ul></li><li><p>建议规范:</p><ul><li>① 为了提高阅读性,使用有意义的单词,见名知意,如:sum,name,max,year 等。</li><li>② 使用下划线连接多个单词组成的标识符,如:max_classes_per_student 等。</li><li>③ 多个单词组成的标识符,除了使用下划线连接,也可以使用小驼峰命名法,除第一个单词外,后续单词的首字母大写,如: studentId、student_name 等。</li><li>④ 不要出现仅靠大小写区分不同的标识符,如:name、Name 容易混淆。</li><li>⑤ 系统内部使用了一些下划线开头的标识符,如:C99 标准添加的类型 <code>_Bool</code>,为防止冲突,建议开发者尽量避免使用下划线开头的标识符。</li></ul></li><li><p>示例:合法(不一定建议)的标识符</p></li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>a、BOOK_sun、MAX_SIZE、Mouse、student23、Football、FOOTBALL、max、_add、num_1、sum_of_numbers</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><ul><li>示例:非法的标识符</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>$zj、3sum、ab#cd、23student、Foot-baii、s.com、b&c、j**p、book-1、tax rate、don't</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><h3 id="_1-6-3-关键字" tabindex="-1">1.6.3 关键字 <a class="header-anchor" href="#_1-6-3-关键字" aria-label="Permalink to "1.6.3 关键字""></a></h3><ul><li>C 语言中的关键字是编译器<code>预定义</code>的<code>保留字</code>,它们有<code>特定</code>的<code>含义</code>和<code>用途</code>,用于控制程序的结构和执行。</li><li>C80 和 C90 (ANSI C)定义的关键字,如下所示:</li></ul><table tabindex="0"><thead><tr><th>类型(功能)</th><th>具体关键字</th></tr></thead><tbody><tr><td>数据类型关键字</td><td><code>char</code>、<code>double</code>、<code>float</code>、<code>int</code>、<code>long</code>、<code>short</code>、<code>signed</code>、<code>unsigned</code>、<code>void</code></td></tr><tr><td>存储类说明符关键字</td><td><code>auto</code>、<code>extern</code>、<code>register</code>、<code>static</code>、<code>typedef</code>、<code>volatile</code>、<code>const</code></td></tr><tr><td>控制语句关键字</td><td><code>break</code>、<code>case</code>、<code>continue</code>、<code>default</code>、<code>do</code>、<code>else</code>、<code>for</code>、<code>goto</code>、<code>if</code>、<code>return</code>、<code>switch</code>、<code>while</code></td></tr><tr><td>结构体、联合体和枚举关键字</td><td><code>enum</code>、<code>struct</code>、<code>union</code></td></tr><tr><td>其他关键字</td><td><code>sizeof</code></td></tr></tbody></table><ul><li>C99 新增的关键字,如下所示:</li></ul><table tabindex="0"><thead><tr><th>类型(功能)</th><th>具体关键字</th></tr></thead><tbody><tr><td>数据类型关键字</td><td><code>_Bool</code>、<code>_Complex</code>、<code>_Imaginary</code></td></tr><tr><td>存储类说明符关键字</td><td><code>inline</code>、<code>restrict</code></td></tr><tr><td>其他关键字</td><td><code>_Complex</code>、 <code>_Imaginary</code></td></tr></tbody></table><ul><li>C11 新增的关键字,如下所示:</li></ul><table tabindex="0"><thead><tr><th>类型(功能)</th><th>具体关键字</th></tr></thead><tbody><tr><td>存储类说明符关键字</td><td><code>_Atomic</code></td></tr><tr><td>其他关键字</td><td><code>_Alignas</code>、 <code>_Alignof</code>、 <code>_Generic</code>、 <code>_Noreturn</code>、 <code>_Static_assert</code>、 <code>_Thread_local</code></td></tr></tbody></table><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li>① 关键字不能用作标识符(如:变量名、函数名等)。</li><li>② 不要死记硬背这些关键字,在实际开发中,并不一定全部使用到;而且,在学到后面的时候,会自动记住这些关键字以及对应的含义。</li></ul></div><h1 id="第二章-常量-⭐" tabindex="-1">第二章:常量(⭐) <a class="header-anchor" href="#第二章-常量-⭐" aria-label="Permalink to "第二章:常量(⭐)""></a></h1><h2 id="_2-1-概述" tabindex="-1">2.1 概述 <a class="header-anchor" href="#_2-1-概述" aria-label="Permalink to "2.1 概述""></a></h2><ul><li>在程序运行过程中,不能改变的量就是常量。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 在数学中的 <code>π</code>,就是一个常量,其值为 3.1415926 。</li><li>② 在生活中,人类的性别只有<code>男</code>和<code>女</code>;其中,<code>男</code>和<code>女</code>也是常量。</li><li>③ ...</li></ul></div><h2 id="_2-2-常量的分类" tabindex="-1">2.2 常量的分类 <a class="header-anchor" href="#_2-2-常量的分类" aria-label="Permalink to "2.2 常量的分类""></a></h2><ul><li>在 C 语言中的变量的分类,如下所示: <ul><li>① 字面量常量。</li><li>② 标识符常量: <ul><li><code>#define</code> 宏定义的标识符常量。</li><li><code>const</code> 关键字修饰的标识符常量。</li><li>枚举常量。</li></ul></li></ul></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>所谓的<code>字面量常量</code>,就是可以直接使用的常量,不需要声明或定义,包括:整数常量、浮点数常量以及字符常量。</li><li>所谓的<code>标识符常量</code>,就是使用标识符来作为常量名,包括: <code>#define</code> 宏定义的标识符常量、<code>const</code> 关键字修饰的标识符常量、枚举常量。</li></ul></div><ul><li>示例:字面量常量</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'A'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 12.3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "你好"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li>示例:字面量常量</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"整数常量 =》</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"字符常量 =》</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%c\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'A'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"浮点数常量 =》</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%f\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">12.3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"字符串常量 =》</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%s\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"你好"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="_2-3-使用-define-定义常量" tabindex="-1">2.3 使用 #define 定义常量 <a class="header-anchor" href="#_2-3-使用-define-定义常量" aria-label="Permalink to "2.3 使用 #define 定义常量""></a></h2><ul><li><code>#define</code> 来定义常量,也叫作宏定义,就是用一个标识符来表示一个常量值,如果在后面的代码中出现了该标识符,那么编译时就全部替换成指定的常量值,即用宏体替换所有宏名,简称<code>宏替换</code>。</li><li>格式是:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">#define 常量名 常量值</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="caution custom-block github-alert"><p class="custom-block-title">CAUTION</p><p></p><ul><li>① 其实<code>宏定义</code>的常量的<code>执行时机</code>是在<code>预处理</code>阶段,将所有<code>宏常量</code>替换完毕,才会继续编译代码。</li><li>② 不要以 <code>;</code> 结尾,如果有 <code>;</code> ,分号也会成为常量值的一部分。</li><li>③ <code># define</code> 必须写在 <code>main</code> 函数的外面!!!</li><li>④ <code>常量名</code>习惯用<code>大写字母</code>表示,如果多个单词,使用 <code>_</code> 来分隔,以便和变量区分。</li></ul></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PI</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3.1415926</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> double</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2.5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> double</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> area </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PI </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"半径为</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%lf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">的圆的面积是</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%.2lf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, radius, area);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="_2-4-const-关键字" tabindex="-1">2.4 const 关键字 <a class="header-anchor" href="#_2-4-const-关键字" aria-label="Permalink to "2.4 const 关键字""></a></h2><ul><li>C99 标准新增,这种方式跟定义一个变量是类似的;只不过,需要在变量的数据类型前加上 <code>const</code> 关键字。</li><li>和使用 <code>#define定义宏常量</code>相比,const 定义的常量有详细的数据类型,而且会在编译阶段进行安全检查,在运行时才完成替换,所以会更加安全和方便。</li><li>格式是:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> 数据类型 常量名 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> 常量值;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PI </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3.1415926</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> double</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2.5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> double</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> area </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PI </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> radius;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"半径为</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%lf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">的圆的面积是</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%.2lf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, radius, area);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="_2-5-枚举常量" tabindex="-1">2.5 枚举常量 <a class="header-anchor" href="#_2-5-枚举常量" aria-label="Permalink to "2.5 枚举常量""></a></h2><ul><li>格式:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">enum</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> 枚举常量 {</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> xxx </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> yyy;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ...</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 默认情况下,枚举常量是从 0 开始递增的。</li><li>② 也可以在定义枚举常量的时候,自定义它们的值。</li></ul></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">enum</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> sex {</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> MALE </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> FEMALE </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">};</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, MALE);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, FEMALE);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">enum</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Sex {</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> MALE </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> FEMALE </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">};</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> enum</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Sex sex;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"请输入性别(1 表示男性, 2 表示女性):"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> scanf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">sex);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"您的性别是:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, sex);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="_2-6-defind-定义常量-vs-const-定义常量" tabindex="-1">2.6 #defind 定义常量 VS const 定义常量 <a class="header-anchor" href="#_2-6-defind-定义常量-vs-const-定义常量" aria-label="Permalink to "2.6 #defind 定义常量 VS const 定义常量""></a></h2><ul><li>① 执行时机:<code>#define</code> 是预处理指令,在编译<code>之前</code>执行;<code>const</code> 是关键字,在编译<code>过程</code>中执行。</li><li>② 类型检查:<code>#define</code> 定义常量<code>不用指定类型</code>,<code>不进行类型检查</code>,只是简单地文本替换;<code>const</code> 定义常量<code>需要指定数据类型</code>,<code>会进行类型检查</code>,类型安全性更强。</li></ul><h1 id="第三章-二进制" tabindex="-1">第三章:二进制 <a class="header-anchor" href="#第三章-二进制" aria-label="Permalink to "第三章:二进制""></a></h1><h2 id="_3-1-概述" tabindex="-1">3.1 概述 <a class="header-anchor" href="#_3-1-概述" aria-label="Permalink to "3.1 概述""></a></h2><ul><li>计算机的底层只有<code>二进制</code>,即计算机中<code>运算</code>和<code>存储</code>的<code>所有数据</code>都需要转换为<code>二进制</code>,包括:数字、字符、图片、视频等。</li></ul><p><img src="/c/assets/7.DO8XxSV6.jpg" alt=""></p><ul><li>之前,我们也提到现代的计算机(量子计算机除外)几乎都遵循<code>冯·诺依曼</code>体系结构,其理论要点如下: <ul><li>① <strong>存储程序</strong>:<code>程序指令</code>和<code>数据</code>都存储在计算机的内存中,这使得程序可以在运行时修改。</li><li>② <strong>二进制逻辑</strong>:所有数据和指令都以<code>二进制</code>形式表示。</li><li>③ <strong>顺序执行</strong>:指令按照它们在内存中的顺序执行,但可以有条件地改变执行顺序。</li><li>④ <strong>五大部件</strong>:计算机由<code>运算器</code>、<code>控制器</code>、<code>存储器</code>、<code>输入设备</code>和<code>输出设备</code>组成。</li><li>⑤ <strong>指令结构</strong>:指令由操作码和地址码组成,操作码指示要执行的操作,地址码指示操作数的位置。</li><li>⑥ <strong>中心化控制</strong>:计算机的控制单元(CPU)负责解释和执行指令,控制数据流。</li></ul></li><li>所以,再次论证了为什么计算机只能识别二进制。</li></ul><h2 id="_3-2-进制" tabindex="-1">3.2 进制 <a class="header-anchor" href="#_3-2-进制" aria-label="Permalink to "3.2 进制""></a></h2><h3 id="_3-2-1-常见的进制" tabindex="-1">3.2.1 常见的进制 <a class="header-anchor" href="#_3-2-1-常见的进制" aria-label="Permalink to "3.2.1 常见的进制""></a></h3><ul><li>在生活中,我们最为常用的进制就是<code>十进制</code>,其规则是<code>满 10 进 1</code> ,即:</li></ul><p><img src="/c/assets/8.CUXrdefp.jpeg" alt=""></p><ul><li>在计算机中,常见的进制有<code>二进制</code>、<code>八进制</code>和<code>十六进制</code>,即: <ul><li>二进制:只能 0 和 1 ,满 2 进 1 。</li><li>八进制:0 ~ 7 ,满 8 进 1 。</li><li>十六进制:0 ~ 9 以及 A ~ F ,满 16 进 1 。</li></ul></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>在十六进制中,除了 0 到 9 这十个数字之外,还引入了字母,以便表示超过 9 的值。字母 A 对应十进制的 10 ,字母 B 对应十进制的 11 ,以此类推,字母 F 对应十进制的 15。</p></div><ul><li>进制的换算举例,如下所示:</li></ul><table tabindex="0"><thead><tr><th>十进制</th><th>二进制</th><th>八进制</th><th>十六进制</th></tr></thead><tbody><tr><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>1</td><td>1</td><td>1</td><td>1</td></tr><tr><td>2</td><td>10</td><td>2</td><td>2</td></tr><tr><td>3</td><td>11</td><td>3</td><td>3</td></tr><tr><td>4</td><td>100</td><td>4</td><td>4</td></tr><tr><td>5</td><td>101</td><td>5</td><td>5</td></tr><tr><td>6</td><td>110</td><td>6</td><td>6</td></tr><tr><td>7</td><td>111</td><td>7</td><td>7</td></tr><tr><td>8</td><td>1000</td><td>10</td><td>8</td></tr><tr><td>9</td><td>1001</td><td>11</td><td>9</td></tr><tr><td>10</td><td>1010</td><td>12</td><td>a 或 A</td></tr><tr><td>11</td><td>1011</td><td>13</td><td>b 或 B</td></tr><tr><td>12</td><td>1100</td><td>14</td><td>c 或 C</td></tr><tr><td>13</td><td>1101</td><td>15</td><td>d 或 D</td></tr><tr><td>14</td><td>1110</td><td>16</td><td>e 或 E</td></tr><tr><td>15</td><td>1111</td><td>17</td><td>f 或 F</td></tr><tr><td>16</td><td>10000</td><td>20</td><td>10</td></tr><tr><td>...</td><td>...</td><td>...</td><td>...</td></tr></tbody></table><ul><li>二进制和十六进制的关系:十六进制是以 16 为基数的进制系统,16 在二进制中表示为 ( 2^4 ),即:一个十六进制可以表示 4 位二进制。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>十六进制的范围是:0 ~ F (0 ~ 15)对应的二进制数的范围是:0000 ~ 1111 (0 ~ 15)。</p></div><ul><li>每个十六进制数都可以映射到一个唯一的 4 位二进制数,即:</li></ul><table tabindex="0"><thead><tr><th>十六进制</th><th>二进制</th></tr></thead><tbody><tr><td>0</td><td>0000</td></tr><tr><td>1</td><td>0001</td></tr><tr><td>2</td><td>0010</td></tr><tr><td>3</td><td>0011</td></tr><tr><td>4</td><td>0100</td></tr><tr><td>5</td><td>0101</td></tr><tr><td>6</td><td>0110</td></tr><tr><td>7</td><td>0111</td></tr><tr><td>8</td><td>1000</td></tr><tr><td>9</td><td>1001</td></tr><tr><td>A</td><td>1010</td></tr><tr><td>B</td><td>1011</td></tr><tr><td>C</td><td>1100</td></tr><tr><td>D</td><td>1101</td></tr><tr><td>E</td><td>1110</td></tr><tr><td>F</td><td>1111</td></tr></tbody></table><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>由此可见,每个十六进制数字确实由 4 位二进制数表示。</p></div><ul><li>二进制和八进制的关系:八进制是以 8 为基数的进制系统,8 在二进制中表示为 ( 2^3 );即:一个八进制位可以表示 3 个二进制位。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>八进制的范围是:0 ~ 7 对应的二进制数的范围是:000 ~ 111。</p></div><ul><li>每个八进制数位都可以映射到一个唯一的 3 位二进制数,即:</li></ul><table tabindex="0"><thead><tr><th>八进制</th><th>二进制</th></tr></thead><tbody><tr><td>0</td><td>000</td></tr><tr><td>1</td><td>001</td></tr><tr><td>2</td><td>010</td></tr><tr><td>3</td><td>011</td></tr><tr><td>4</td><td>100</td></tr><tr><td>5</td><td>101</td></tr><tr><td>6</td><td>110</td></tr><tr><td>7</td><td>111</td></tr></tbody></table><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>由此可见,每个八进制数字确实由 3 位二进制数表示。</p></div><h3 id="_3-2-2-c-语言中如何表示不同进制的整数" tabindex="-1">3.2.2 C 语言中如何表示不同进制的整数? <a class="header-anchor" href="#_3-2-2-c-语言中如何表示不同进制的整数" aria-label="Permalink to "3.2.2 C 语言中如何表示不同进制的整数?""></a></h3><ul><li><p>规则如下:</p><ul><li>在 C 语言中,如果是<code>二进制</code>(字面常量),则需要在二进制整数前加上 <code>0b</code> 或 <code>0B</code> 。</li><li>在 C 语言中,如果是<code>八进制</code>(字面常量),则需要在八进制整数前加上 <code>0</code> 。</li><li>在 C 语言中,如果是<code>十进制</code>(字面常量),正常数字表示即可。</li><li>在 C 语言中,如果是<code>十六进制</code>(字面常量),则需要在十六进制整数前加上 <code>0x</code>或<code>0X</code> 。</li></ul></li><li><p>示例:</p></li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> num1 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> 0b</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10100110</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 二进制</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> num2 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> 0</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">717563</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 八进制</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> num3 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1000</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 十进制</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> num4 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> 0x</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">af72</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 十六进制</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"num1 = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num1);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // num1 = 166</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"num2 = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num2);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // num2 = 237427</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"num3 = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num3);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // num3 = 1000</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"num4 = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num4);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // num4 = 44914</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="_3-2-3-输出格式" tabindex="-1">3.2.3 输出格式 <a class="header-anchor" href="#_3-2-3-输出格式" aria-label="Permalink to "3.2.3 输出格式""></a></h3><ul><li>在 C 语言中,可以使用不同的<code>格式占位符</code>来<code>输出</code>不同<code>进制</code>的整数,如下所示: <ul><li><code>%d</code>:十进制整数。</li><li><code>%o</code> :八进制整数。</li><li><code>%x</code>:十六进制整数。</li><li><code>%#o</code> :显示前缀 <code>0</code> 的八进制整数。</li><li><code>%#x</code> :显示前缀 <code>0x</code> 的十六进制整数。</li><li><code>%#X</code> :显示前缀 <code>0X</code> 的十六进制整数。</li></ul></li></ul><div class="caution custom-block github-alert"><p class="custom-block-title">CAUTION</p><p></p><p>C 语言中没有输出二进制数的格式占位符!!!</p></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> num </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 100</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 的十进制整数: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num, num);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 100 的十进制整数: 100</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 的八进制整数: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%o\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num, num);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 100 的八进制整数: 144</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 的十六进制整数: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%x\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num, num);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 100 的十六进制整数: 64</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 的八进制(前缀)整数: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%#o\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num, num);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 100 的八进制(前缀)整数: 0144</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 的十六进制(前缀)整数: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%#x\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num, num);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 100 的十六进制(前缀)整数: 0x64</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 的十六进制(前缀)整数: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%#X\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, num, num);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 100 的十六进制(前缀)整数: 0X64</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="_3-3-进制的转换" tabindex="-1">3.3 进制的转换 <a class="header-anchor" href="#_3-3-进制的转换" aria-label="Permalink to "3.3 进制的转换""></a></h2><h3 id="_3-3-1-概述" tabindex="-1">3.3.1 概述 <a class="header-anchor" href="#_3-3-1-概述" aria-label="Permalink to "3.3.1 概述""></a></h3><ul><li>不同进制的转换,如下所示:</li></ul><p><img src="/c/assets/9.uqLiL_yu.png" alt=""></p><ul><li>在计算机中,数据是从右往左的方式排列的;其中,最右边的是低位,最左边的是高位,即:</li></ul><p><img src="/c/assets/10.CkykpHY2.png" alt=""></p><h3 id="_3-3-2-二进制和十进制的转换" tabindex="-1">3.3.2 二进制和十进制的转换 <a class="header-anchor" href="#_3-3-2-二进制和十进制的转换" aria-label="Permalink to "3.3.2 二进制和十进制的转换""></a></h3><h4 id="_3-3-2-1-二进制转换为十进制" tabindex="-1">3.3.2.1 二进制转换为十进制 <a class="header-anchor" href="#_3-3-2-1-二进制转换为十进制" aria-label="Permalink to "3.3.2.1 二进制转换为十进制""></a></h4><ul><li>规则:从最低位开始,将每个位上的数提取出来,乘以 2 的 (位数 - 1 )次方,然后求和。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 在学术界,将这种计算规则,称为<code>位权相加法</code>。</li><li>② <code>八进制转换为十进制</code>、<code>十六进制转换为十进制</code>和<code>二进制转换为十进制</code>的算法相同!!!</li></ul></div><ul><li>示例:十进制转十进制</li></ul><p><img src="/c/assets/11.DV1YbrOP.png" alt=""></p><ul><li>示例:二进制转十进制</li></ul><p><img src="/c/assets/12.AHNJT9TV.png" alt=""></p><h4 id="_3-3-2-2-十进制转换二进制" tabindex="-1">3.3.2.2 十进制转换二进制 <a class="header-anchor" href="#_3-3-2-2-十进制转换二进制" aria-label="Permalink to "3.3.2.2 十进制转换二进制""></a></h4><ul><li>规则:将该数不断除以 2 ,直到商为 0 为止,然后将每步得到的余数倒过来,就是对应的二进制。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 在学术界,将这种计算规则,称为<code>短除法</code>或<code>连续除2取余法</code>。</li><li>② 很好理解,只有不断地除以 2 ,就能保证最大的数字不超过 2 ,这不就是二进制(只能有 0 或 1)吗?</li><li>③ <code>八进制转换为二进制</code>、<code>十六进制转换为二进制</code>和<code>十进制转换为二进制</code>的算法相同!!!</li></ul></div><ul><li>示例:十进制转十进制</li></ul><p><img src="/c/assets/13.Bs-MOwx2.png" alt=""></p><ul><li>示例:十进制转二进制</li></ul><p><img src="/c/assets/14.StzjmBz-.png" alt=""></p><h3 id="_3-3-3-二进制转八进制" tabindex="-1">3.3.3 二进制转八进制 <a class="header-anchor" href="#_3-3-3-二进制转八进制" aria-label="Permalink to "3.3.3 二进制转八进制""></a></h3><ul><li><p>规则:每 3 位二进制就是一个八进制。</p></li><li><p>示例:011 101 001 -> 351</p></li></ul><p><img src="/c/assets/15.C0wVWaxD.png" alt=""></p><h3 id="_3-3-4-二进制转十六进制" tabindex="-1">3.3.4 二进制转十六进制 <a class="header-anchor" href="#_3-3-4-二进制转十六进制" aria-label="Permalink to "3.3.4 二进制转十六进制""></a></h3><ul><li><p>规则:每 4 位二进制就是一个八进制。</p></li><li><p>示例:1110 1001 -> 0xE9</p></li></ul><p><img src="/c/assets/16.LXJMAihe.png" alt=""></p><h2 id="_3-4-原码、反码和补码" tabindex="-1">3.4 原码、反码和补码 <a class="header-anchor" href="#_3-4-原码、反码和补码" aria-label="Permalink to "3.4 原码、反码和补码""></a></h2><h3 id="_3-4-1-概述" tabindex="-1">3.4.1 概述 <a class="header-anchor" href="#_3-4-1-概述" aria-label="Permalink to "3.4.1 概述""></a></h3><ul><li>机器数:一个数在计算机的存储形式是二进制,我们称这些二进制数为机器数。机器数可以是有符号的,用机器数的最高位来存放符号位,0 表示正数,1 表示负数。</li></ul><p><img src="/c/assets/17._UTCq3PD.png" alt=""></p><ul><li>真值:因为机器数带有符号位,所以机器数的形式值不等于其真实表示的值(真值),以机器数 1000 0001 为例,其真正表示的值(首位是符号位)为 -1,而形式值却是 129 ,因此将带有符号位的机器数的真正表示的值称为机器数的真值。</li></ul><p><img src="/c/assets/18.BjQ5kBL-.png" alt=""></p><h3 id="_3-4-2-原码" tabindex="-1">3.4.2 原码 <a class="header-anchor" href="#_3-4-2-原码" aria-label="Permalink to "3.4.2 原码""></a></h3><ul><li>原码的表示与机器数真值表示的一样,即用第一位表示符号,其余位表示数值。</li><li>规则: <ul><li>正数的<code>原码</code>是它本身对应的二进制数,符号位是 0 。</li><li>负数的<code>原码</code>是它本身绝对值对应的二进制数,但是符号位是 1 。</li></ul></li><li>+1 的原码,使用 8 位二进数来表示,就是:</li></ul><table tabindex="0"><thead><tr><th>十进制数</th><th>原码(8位二进制数)</th></tr></thead><tbody><tr><td>+1</td><td><code>0</code>000 0001</td></tr></tbody></table><ul><li>-1 的原码,使用 8 位二进数来表示,就是:</li></ul><table tabindex="0"><thead><tr><th>十进制数</th><th>原码(8位二进制数)</th></tr></thead><tbody><tr><td>-1</td><td><code>1</code>000 0001</td></tr></tbody></table><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>按照原码的规则,会出现 <code>+0</code> 和 <code>-0</code> 的情况,即:<code>0</code>000 0000(+0)、<code>1</code>000 0000(-0),显然不符合实际情况;所以,计算机底层虽然存储和计算的都是二进数,但显然不是原码。</p></div><h3 id="_3-4-3-反码" tabindex="-1">3.4.3 反码 <a class="header-anchor" href="#_3-4-3-反码" aria-label="Permalink to "3.4.3 反码""></a></h3><ul><li><p>规则:</p><ul><li>正数的反码和它的原码相同。</li><li>负数的反码是在其原码的基础上,符号位不变,其余各位取反。</li></ul></li><li><p>+1 的反码,使用 8 位二进数来表示,就是:</p></li></ul><table tabindex="0"><thead><tr><th>十进制数</th><th>原码(8位二进制数)</th><th>反码(8位二进制数)</th></tr></thead><tbody><tr><td>+1</td><td><code>0</code>000 0001</td><td><code>0</code>000 0001</td></tr></tbody></table><ul><li>-1 的反码,使用 8 位二进数来表示,就是:</li></ul><table tabindex="0"><thead><tr><th>十进制数</th><th>原码(8位二进制数)</th><th>反码(8位二进制数)</th></tr></thead><tbody><tr><td>-1</td><td><code>1</code>000 0001</td><td><code>1</code>111 1110</td></tr></tbody></table><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>按照反码的规则,如果是 <code>+0</code>,对应的原码是 <code>0</code>000 0000;那么,其反码还是 <code>0</code>000 0000 ;如果是 <code>-0</code>,对应的原码是 <code>1</code>000 0000,其反码是 <code>1</code>111 1111,显然不符合实际情况;所以,计算机底层虽然存储和计算的都是二进数,但显然不是反码。</p></div><h3 id="_3-4-4-补码" tabindex="-1">3.4.4 补码 <a class="header-anchor" href="#_3-4-4-补码" aria-label="Permalink to "3.4.4 补码""></a></h3><ul><li><p>规则:</p><ul><li>正数的补码和它的原码相同。</li><li>负数的补码是在其反码的基础上 + 1 。</li></ul></li><li><p>+1 的补码,使用 8 位二进数来表示,就是:</p></li></ul><table tabindex="0"><thead><tr><th>十进制数</th><th>原码(8位二进制数)</th><th>反码(8位二进制数)</th><th>补码(8位二进制数)</th></tr></thead><tbody><tr><td>+1</td><td><code>0</code>000 0001</td><td><code>0</code>000 0001</td><td><code>0</code>000 0001</td></tr></tbody></table><ul><li>-1 的补码,使用 8 位二进数来表示,就是:</li></ul><table tabindex="0"><thead><tr><th>十进制数</th><th>原码(8位二进制数)</th><th>反码(8位二进制数)</th><th>补码(8位二进制数)</th></tr></thead><tbody><tr><td>-1</td><td><code>1</code>000 0001</td><td><code>1</code>111 1110</td><td><code>1</code>111 1111</td></tr></tbody></table><ul><li>如果 0 ,按照 <code>+0</code> 的情况进行处理,即:</li></ul><p><img src="/c/assets/19.COIOzcmT.png" alt=""></p><ul><li>如果 0 ,按照 <code>-0</code> 的情况进行处理,即:</li></ul><p><img src="/c/assets/20.Cu__mjav.png" alt=""></p><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li>① 补码表示法解决了<code>原码</code>和<code>反码</code>存在的<code>两种</code>零(<code>+0</code> 和 <code>-0</code>)的问题,即:在补码表示法中,只有<code>一个</code>零,即 0000 0000。</li><li>② 补码使得<code>加法运算</code>和<code>减法运算</code>可以统一处理,通过将减法运算<code>转换</code>为加法运算,可以简化硬件设计,提高了运算效率。</li><li>③ 计算机底层<code>存储</code>和<code>计算</code>的都是<code>二进数的补码</code>。</li></ul></div><h3 id="_3-4-5-总结" tabindex="-1">3.4.5 总结 <a class="header-anchor" href="#_3-4-5-总结" aria-label="Permalink to "3.4.5 总结""></a></h3><ul><li>① 正数的原码、反码和补码都是一样的,三码合一。</li><li>② 负数的反码是在其原码的基础上,按位取反(0 变 1 ,1 变 0 ),符号位不变;负数的补码是其反码 + 1 。</li><li>③ 0 的补码是 0 。</li></ul><h2 id="_3-5-计算机底层为什么使用补码" tabindex="-1">3.5 计算机底层为什么使用补码? <a class="header-anchor" href="#_3-5-计算机底层为什么使用补码" aria-label="Permalink to "3.5 计算机底层为什么使用补码?""></a></h2><ul><li>如果计算是 <code>2 - 2</code> ,那么可以转换为 <code>2 + (-2)</code>,这样计算机内部在处理<code>减法计算</code>的时候,就会将其转换为<code>加法计算</code>的形式,以简化硬件设计和提高计算效率。</li><li><code>最高位</code>表示<code>符号位</code>,由于符号位的存在,如果使用<code>原码</code>来计算,就会导致<code>计算结果不正确</code>,即:</li></ul><p><img src="/c/assets/21.BX_KzkHt.png" alt=""></p><ul><li><code>补码</code>的设计可以巧妙的让<code>符号位</code>也参与计算,并且可以得到<code>正确的计算结果</code>,即:</li></ul><p><img src="/c/assets/22.COt_QxSP.png" alt=""></p></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-d4a0bba5><!--[--><!--]--><div class="edit-info" data-v-d4a0bba5><!----><div class="last-updated" data-v-d4a0bba5><p class="VPLastUpdated" data-v-d4a0bba5 data-v-7e05ebdb>上次更新: <time datetime="2024-07-11T08:05:05.000Z" data-v-7e05ebdb></time></p></div></div><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-d4a0bba5><span class="visually-hidden" id="doc-footer-aria-label" data-v-d4a0bba5>Pager</span><div class="pager" data-v-d4a0bba5><a class="VPLink link pager-link prev" href="/c/notes/01_c-basic/01_xdx/" data-v-d4a0bba5><!--[--><span class="desc" data-v-d4a0bba5>上一篇</span><span class="title" data-v-d4a0bba5>C 语言入门</span><!--]--></a></div><div class="pager" data-v-d4a0bba5><a class="VPLink link pager-link next" href="/c/notes/02_dsa/01_xdx/" data-v-d4a0bba5><!--[--><span class="desc" data-v-d4a0bba5>下一篇</span><span class="title" data-v-d4a0bba5>初识 C 语言</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><footer class="VPFooter has-sidebar" data-v-5d98c3a5 data-v-e315a0ad><div class="container" data-v-e315a0ad><p class="message" data-v-e315a0ad>Released under the MIT License.</p><p class="copyright" data-v-e315a0ad>Copyright © 2024 许大仙</p></div></footer><!--[--><!--]--></div></div>
|
||
<script>window.__VP_HASH_MAP__=JSON.parse("{\"notes_index.md\":\"CdHKXnBk\",\"index.md\":\"CZVqUadY\",\"notes_01_c-basic_02_xdx_index.md\":\"Cux_q1Jy\",\"notes_01_c-basic_01_xdx_index.md\":\"DA3nuW_4\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"zh-CN\",\"dir\":\"ltr\",\"title\":\"许大仙\",\"titleTemplate\":\"Hi,终于等到你\",\"description\":\"许大仙前端、Java、大数据、云原生\",\"base\":\"/c/\",\"head\":[],\"router\":{\"prefetchLinks\":true},\"appearance\":true,\"themeConfig\":{\"lastUpdatedText\":\"上次更新\",\"returnToTopLabel\":\"返回顶部\",\"search\":{\"provider\":\"local\"},\"logo\":\"/logo.svg\",\"nav\":[{\"text\":\"首页\",\"link\":\"https://aexiar.github.io/\"},{\"text\":\"计组6件套\",\"link\":\"https://aexiar.github.io/coa6/notes/\"},{\"text\":\"c/c++\",\"link\":\"/notes/\"},{\"text\":\"前端\",\"link\":\"https://aexiar.github.io/web-design/notes/\"},{\"text\":\"Java\",\"link\":\"https://aexiar.github.io/java/notes/\"},{\"text\":\"大数据\",\"link\":\"https://aexiar.github.io/big-data/notes/\"},{\"text\":\"云原生\",\"link\":\"https://aexiar.github.io/linux/notes/\"},{\"text\":\"开源软件\",\"link\":\"https://aexiar.github.io/open-software/notes/\"}],\"sidebar\":{\"/notes/\":[{\"text\":\"C语言基础\",\"collapsed\":true,\"items\":[{\"text\":\"C 语言入门\",\"link\":\"/notes/01_c-basic/01_xdx/\"},{\"text\":\"变量和进制\",\"link\":\"/notes/01_c-basic/02_xdx/\"}]},{\"text\":\"C 语言高级\",\"collapsed\":true,\"items\":[{\"text\":\"初识 C 语言\",\"link\":\"/notes/02_dsa/01_xdx/\"}]},{\"text\":\"C++ 核心编程\",\"collapsed\":true,\"items\":[{\"text\":\"计算机组成原理前言\",\"link\":\"/notes/03_coa/03_xdx/\"}]},{\"text\":\"C++ 标准库\",\"collapsed\":true,\"items\":[{\"text\":\"计算机组成原理前言\",\"link\":\"/notes/03_coa/03_xdx/\"}]},{\"text\":\"QT 桌面开发\",\"collapsed\":true,\"items\":[{\"text\":\"操作系统前言\",\"link\":\"/notes/04_os/01_xdx/\"}]},{\"text\":\"Linux 高并发服务器开发\",\"collapsed\":true,\"items\":[{\"text\":\"操作系统前言\",\"link\":\"/notes/04_os/01_xdx/\"}]}]},\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/Aurorxa\"}],\"docFooter\":{\"prev\":\"上一篇\",\"next\":\"下一篇\"},\"footer\":{\"message\":\"Released under the MIT License.\",\"copyright\":\"Copyright © 2024 许大仙\"},\"outline\":{\"level\":[1,6],\"label\":\"目录\"},\"outlineTitle\":\"当前页大纲\"},\"locales\":{},\"scrollOffset\":134,\"cleanUrls\":false}");</script>
|
||
|
||
</body>
|
||
</html> |