<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ C 语言 - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ freeCodeCamp 是一个免费学习编程的开发者社区，涵盖 Python、HTML、CSS、React、Vue、BootStrap、JSON 教程等，还有活跃的技术论坛和丰富的社区活动，在你学习编程和找工作时为你提供建议和帮助。 ]]>
        </description>
        <link>https://www.freecodecamp.org/chinese/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ C 语言 - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/chinese/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 22 May 2026 15:16:55 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/chinese/news/tag/c-yu-yan/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ 如何在 Visual Studio Code 中编写和运行 C 及 C++ 代码 ]]>
                </title>
                <description>
                    <![CDATA[ Visual Studio Code（或简称 VS Code）是一个非常常见且广泛使用的文本编辑器和 IDE（集成开发环境）。通过使用大量扩展，你可以将 VS Code 变得非常强大。 在开始介绍如何在 Visual Studio Code 上运行你的第一行 C 或 C++ 代码之前，让我根据你计算机上使用的操作系统，带你了解设置的整个过程。 C 和 C++ 编译器 要运行 C 或 C++ 代码，你只需要在你的计算机上安装一个有效的 C/C++ 编译器。如果你使用的是 Linux 操作系统，那么很有可能它已经安装在你的系统上了。但我们需要确保它已正确安装。 要检查你的系统上是否安装了编译器（GCC/G++/MinGW），你需要先检查编译器版本。 只需打开终端并使用 gcc --version 和 g++ --version。如果你得到版本号，那么说明编译器已经安装在你的系统上。 你可以在任何操作系统上使用相同的命令来检查版本，无论是 Windows、Linux 还是 macOS 系统。 如果你的终端反馈说它不知道 GCC 或 G++，那么你需要正确安装编译器。 如果你使用 ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/how-to-write-and-run-c-cpp-code-on-visual-studio-code/</link>
                <guid isPermaLink="false">66ac9b9a79db950405c051bb</guid>
                
                    <category>
                        <![CDATA[ VSCode ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C 语言 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Miya Liu ]]>
                </dc:creator>
                <pubDate>Fri, 02 Aug 2024 01:19:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2024/08/asd.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p data-test-label="translation-intro">
        <strong>原文：</strong> <a href="https://www.freecodecamp.org/news/how-to-write-and-run-c-cpp-code-on-visual-studio-code/" target="_blank" rel="noopener noreferrer" data-test-label="original-article-link">How to Write And Run C and C++ Code in Visual Studio Code</a>
      </p><!--kg-card-begin: markdown--><p>Visual Studio Code（或简称 VS Code）是一个非常常见且广泛使用的文本编辑器和 IDE（集成开发环境）。通过使用大量扩展，你可以将 VS Code 变得非常强大。</p>
<!-- more -->
<p>在开始介绍如何在 Visual Studio Code 上运行你的第一行 C 或 C++ 代码之前，让我根据你计算机上使用的操作系统，带你了解设置的整个过程。</p>
<h2 id="cc">C 和 C++ 编译器</h2>
<p>要运行 C 或 C++ 代码，你只需要在你的计算机上安装一个有效的 C/C++ 编译器。如果你使用的是 Linux 操作系统，那么很有可能它已经安装在你的系统上了。但我们需要确保它已正确安装。</p>
<p>要检查你的系统上是否安装了编译器（GCC/G++/MinGW），你需要先检查编译器版本。</p>
<p>只需打开终端并使用 <code>gcc --version</code> 和 <code>g++ --version</code>。如果你得到版本号，那么说明编译器已经安装在你的系统上。</p>
<p>你可以在任何操作系统上使用相同的命令来检查版本，无论是 Windows、Linux 还是 macOS 系统。</p>
<p>如果你的终端反馈说它不知道 GCC 或 G++，那么你需要正确安装编译器。</p>
<p>如果你使用的是最常用的 Windows 操作系统，那么我已经在 freeCodeCamp 上写了一篇详细的文章<a href="https://chinese.freecodecamp.org/news/how-to-write-and-run-c-cpp-code-on-visual-studio-code/">《如何在 Windows 上安装 C 和 C++ 编译器》</a>，一步步展示所有过程。确保先阅读整篇文章，因为它还包含一个完整的视频，为你提供全面的支持。</p>
<p>如果你使用的是其他操作系统，并且没有安装编译器，请确保在继续之前安装它们。</p>
<h2 id="vscodevscodeinsiders">如何安装 VS Code 或 VS Code Insiders</h2>
<p>你需要直接从官方网站下载 Visual Studio Code：<a href="https://code.visualstudio.com/">https://code.visualstudio.com/</a>。</p>
<p>如果你愿意，你也可以安装 VS Code Insiders，过程也是同样的。</p>
<p>Visual Studio Code Insiders 实际上是 Visual Studio Code 的 “Insiders” 版本，它包含每天发布的所有最新功能。你可以将 VS Code 视为稳定版，将 VS Code Insiders 视为预览版。</p>
<p>如果你想立即体验最新的更新，那么你也可以尝试 Visual Studio Code Insiders（我自己也在使用）。要下载 VS Code Insiders，可以访问其官方网站：<a href="https://code.visualstudio.com/insiders/">https://code.visualstudio.com/insiders/</a></p>
<p>确保下载与你的操作系统匹配的文件。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-163.png" alt="下载页面：VS Code" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>下载页面：VS Code</figcaption>
</figure>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-164.png" alt="下载页面：VS Code Insiders" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>下载页面：VS Code Insiders</figcaption>
</figure>
<p>安装过程非常简单。但我会按顺序向你展示所有步骤。现在，我将展示使用 VS Code Insiders 的安装过程，但你在这里看到的一切，对于 VS Code 也完全相同。</p>
<p>确保勾选“我接受协议”框，然后点击 <strong>Next</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-165.png" alt="接受协议并点击 Next" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>接受协议并点击 Next</figcaption>
</figure>
<p>保持所有设置不变。不用更改任何内容。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-168.png" alt="点击 Next" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>点击 Next</figcaption>
</figure>
<p>点击 <strong>Next</strong>。再次点击 <strong>Next</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-170.png" alt="点击 Next" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>点击 Next</figcaption>
</figure>
<p>确保勾选所有框（✔），然后点击 <strong>Next</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-171.png" alt="勾选所有框，并点击 Next" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>勾选所有框，并点击 Next</figcaption>
</figure>
<p>点击 <strong>Install</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-172.png" alt="点击 Install" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>点击 Install</figcaption>
</figure>
<p>完成安装可能需要一些时间。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-173.png" alt="等待安装完成" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>等待安装完成</figcaption>
</figure>
<p>点击 <strong>Finish</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-175.png" alt="点击 finish" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>点击 finish</figcaption>
</figure>
<p>恭喜 - 你已经成功在系统上安装了 VS Code/VS Code Insiders。现在，干杯！🥂</p>
<h2 id="ccvscodevscodeinsiders">如何为 C 和 C++ 代码准备 VS Code/VS Code Insiders</h2>
<p>首先，打开 VS Code 或 VS Code Insiders。</p>
<p>进入扩展选项卡。搜索 “C” 或 “C++” 并安装第一个已被微软验证的扩展。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-178.png" alt="安装 C/C++ 扩展" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>安装 C/C++ 扩展</figcaption>
</figure>
<p>还需要安装 <strong>C/C++ Extension Pack</strong>。这个扩展包同样应当由微软验证。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-179.png" alt="安装 C/C++ Extension Pack" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>安装 C/C++ Extension Pack</figcaption>
</figure>
<p>接下来，搜索 <strong>Code Runner</strong> 并安装该扩展。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-180.png" alt="安装 Code Runner 扩展" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>安装 Code Runner 扩展</figcaption>
</figure>
<p>现在，我们需要更改一些设置。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-177.png" alt="更改一些设置" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>更改一些设置</figcaption>
</figure>
<p>点击<strong>齿轮</strong>箱（它被称为管理部分），然后点击<strong>设置</strong>。或者，你也可以使用快捷键 <code>Ctrl</code> + <code>,</code>。对于 Mac 系统，需要将 <code>Ctrl</code> 键替换为 <code>Command</code> 键。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-182.png" alt="输入 “Run code in terminal” 并按下 Enter 键" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>输入 “Run code in terminal” 并按下 Enter 键</figcaption>
</figure>
<p>在搜索栏里输入 “Run code in terminal” 并按下 Enter 键。</p>
<p>向下滚动直到找到 <code>Code-runner: Run In Terminal</code>。确保该选项被勾选（✔）。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-184.png" alt="确保勾选该选项" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>确保勾选该选项</figcaption>
</figure>
<p>现在你需要重新启动你的 VS Code/VS Code Insiders。简单地关闭并重新打开程序即可。</p>
<h2 id="">如何测试你的代码</h2>
<p>只需打开 VS Code/VS Code Insiders，打开任何文件夹，并创建扩展名为 <code>.c</code> 的 C 文件和扩展名为 <code>.cpp</code> 的 C++ 文件。</p>
<p>写好代码后，你可以使用右上角的播放按钮直接运行代码。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-185.png" alt="这就是从 VS Code/Insiders 运行任何 C/C++ 程序的方法" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>这就是从 VS Code/Insiders 运行任何 C/C++ 程序的方法</figcaption>
</figure>
<p>它将直接编译并运行代码。运行代码后，代码运行按钮会默认直接运行。你的电脑已经 100% 准备好编译和运行任何 C/C++ 编程代码了。😀</p>
<h2 id="">小结</h2>
<p>感谢你阅读整篇文章。如果这篇文章对你有帮助，你也可以在 <a href="https://www.freecodecamp.org/news/author/fahimbinamin/">freeCodeCamp</a> 查看我的其他文章。</p>
<p>如果你想与我联系，你可以通过 <a href="https://twitter.com/Fahim_FBA">Twitter</a>、<a href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a> 和 <a href="https://github.com/FahimFBA">GitHub</a> 与我联系。</p>
<p>如果你想定期学习各种编程语言和大量实际案例，你也可以<a href="https://www.youtube.com/@FahimAmin?sub_confirmation=1">订阅我的 YouTube 频道</a>（Code With FahimFBA）。</p>
<p>如果你想查看我的进展，你可以在我的 <a href="https://www.polywork.com/fahimbinamin">Polywork 时间线</a>上查看。</p>
<p>你也可以<a href="https://fahimbinamin.com/">访问我的网站</a>来了解更多关于我的信息和我在做的事情。</p>
<p>非常感谢！</p>
<!--kg-card-end: markdown--> ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ 如何在 Windows 上安装 C 和 C++ 编译器 ]]>
                </title>
                <description>
                    <![CDATA[ 如果你想在 Windows 操作系统上运行 C 或 C++ 程序，那么你需要有合适的编译器。 MinGW 编译器是一个知名且广泛使用的软件，用于安装 C 和 C++ 编程语言的 GCC 和 G++ 编译器。 但是许多开发人员在安装编译器时遇到了困难，所以我将在本文中向大家展示所有步骤，并附上截图来帮助你完成安装。 我将使用 Windows 11，相同的过程适用于所有其他 Windows 操作系统。但是如果你使用的是 Windows XP，你需要更改一些步骤。 如果你也想观看我关于此主题的视频，请点击这里： 安装 MSYS2 首先，我们需要从 MSYS2 下载一个可执行文件。访问 MSYS2 的官方网站：https://www.msys2.org/。截至今天，网站看起来如下。 向下滚动，直到找到可执行文件的下载按钮。 简单地点击安装按钮，并将安装文件保存在你希望的任何位置。 完成可执行文件的下载。根据你的网络速度，下载时间不应该太长。 下载文件后，我们将获得这个可执行文件。 双击可执行文件，然后点击 Next。 保持名称不变，然后点击 Nex ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/how-to-install-c-and-cpp-compiler-on-windows/</link>
                <guid isPermaLink="false">66aca85179db950405c0523d</guid>
                
                    <category>
                        <![CDATA[ Windows ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C 语言 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Miya Liu ]]>
                </dc:creator>
                <pubDate>Tue, 30 Jul 2024 07:19:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2024/08/banner_freeCodeCamp.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p data-test-label="translation-intro">
        <strong>原文：</strong> <a href="https://www.freecodecamp.org/news/how-to-install-c-and-cpp-compiler-on-windows/" target="_blank" rel="noopener noreferrer" data-test-label="original-article-link">How to Install C and C++ Compilers on Windows</a>
      </p><!--kg-card-begin: markdown--><p>如果你想在 Windows 操作系统上运行 C 或 C++ 程序，那么你需要有合适的编译器。</p>
<p>MinGW 编译器是一个知名且广泛使用的软件，用于安装 C 和 C++ 编程语言的 GCC 和 G++ 编译器。</p>
<p>但是许多开发人员在安装编译器时遇到了困难，所以我将在本文中向大家展示所有步骤，并附上截图来帮助你完成安装。</p>
<p>我将使用 Windows 11，相同的过程适用于所有其他 Windows 操作系统。但是如果你使用的是 Windows XP，你需要更改一些步骤。</p>
<p>如果你也想观看我关于此主题的视频，请点击这里：</p>
<figure class="kg-card kg-embed-card" data-test-label="fitted">
        <div class="fluid-width-video-container">
          <div style="padding-top: 56.17977528089888%;" class="fluid-width-video-wrapper">
            <iframe width="356" height="200" src="https://www.youtube.com/embed/c7FjV8Gwk_M?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" title="How to Install C and C++ Compilers on Windows 10 / 11" name="fitvid0"></iframe>
          </div>
        </div>
      </figure>
<h2 id="msys2">安装 MSYS2</h2>
<p>首先，我们需要从 MSYS2 下载一个可执行文件。访问 MSYS2 的官方网站：<a href="https://www.msys2.org/">https://www.msys2.org/</a>。截至今天，网站看起来如下。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--8-.png" alt="Screenshot--8-" width="600" height="400" loading="lazy"></p>
<p>向下滚动，直到找到可执行文件的下载按钮。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--9-.png" alt="Screenshot--9-" width="600" height="400" loading="lazy"></p>
<p>简单地点击安装按钮，并将安装文件保存在你希望的任何位置。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--10--1.png" alt="Screenshot--10--1" width="600" height="400" loading="lazy"></p>
<p>完成可执行文件的下载。根据你的网络速度，下载时间不应该太长。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--11-.png" alt="Screenshot--11-" width="600" height="400" loading="lazy"></p>
<p>下载文件后，我们将获得这个可执行文件。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--12-.png" alt="Screenshot--12-" width="600" height="400" loading="lazy"></p>
<p>双击可执行文件，然后点击 <code>Next</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--13-.png" alt="Screenshot--13-" width="600" height="400" loading="lazy"></p>
<p>保持名称不变，然后点击 <code>Next</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--14--1.png" alt="Screenshot--14--1" width="600" height="400" loading="lazy"></p>
<p>保持所有内容不变，并点击 <code>Next</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--15-.png" alt="Screenshot--15-" width="600" height="400" loading="lazy"></p>
<p>给它一些时间来完成安装过程。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--16-.png" alt="Screenshot--16-" width="600" height="400" loading="lazy"></p>
<p>如果保持选中，则一旦你点击 <code>Finish</code>，MSYS2 终端将会打开。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--17-.png" alt="Screenshot--17-" width="600" height="400" loading="lazy"></p>
<p>我更喜欢这样做，但如果你想稍后再执行剩余任务，则需要从开始菜单自行打开终端。</p>
<p>在这种情况下，你需要点击开始按钮 &gt; 搜索 <code>MSYS2</code>，然后像下面的图片一样点击终端：</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--26-.png" alt="Screenshot--26-" width="600" height="400" loading="lazy"></p>
<p>让我假设我们已经成功打开了 <strong>MSYS2 MSYS</strong> 终端。</p>
<p>应用命令 <code>pacman -Syu</code> 来更新软件包数据库和基础软件包。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--19-.png" alt="Screenshot--19-" width="600" height="400" loading="lazy"></p>
<p>如果你看到这种安装提示，请输入 <code>Y</code> 并按回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--20-.png" alt="Screenshot--20-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--21-.png" alt="Screenshot--21-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--22-.png" alt="Screenshot--22-" width="600" height="400" loading="lazy"></p>
<p>输入 <code>Y</code> 并按回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--23-.png" alt="Screenshot--23-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--24-.png" alt="Screenshot--24-" width="600" height="400" loading="lazy"></p>
<p>终端将关闭。我们必须手动打开终端并更新其余的软件包。</p>
<p>点击开始按钮。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--25-.png" alt="Screenshot--25-" width="600" height="400" loading="lazy"></p>
<p>搜索名为 <strong>MSYS2 64bit</strong> 的文件夹。点击该文件夹展开并获得终端。通过点击 <strong>MSYS2 MSYS</strong> 打开终端。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--26--1.png" alt="Screenshot--26--1" width="600" height="400" loading="lazy"></p>
<p>通过应用命令 <code>pacman -Su</code> 来更新其余的软件包。如果终端告诉你需要执行命令 <code>pacman -Sy</code>，则可能需要执行此命令。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--27-.png" alt="Screenshot--27-" width="600" height="400" loading="lazy"></p>
<p>如果收到任何安装提示，则需要输入 <code>Y</code> 或 <code>y</code> 并按回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--29-.png" alt="Screenshot--29-" width="600" height="400" loading="lazy"></p>
<p>稍等一会儿完成安装。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--30-.png" alt="Screenshot--30-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--31-.png" alt="Screenshot--31-" width="600" height="400" loading="lazy"></p>
<p>安装完成后关闭窗口。</p>
<h2 id="gccg">安装 GCC 和 G++ 编译器</h2>
<p>点击开始按钮，找到 <strong>MSYS2 64bit</strong> 文件夹，点击该文件夹展开它。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--32-.png" alt="Screenshot--32-" width="600" height="400" loading="lazy"></p>
<p>如果你和我一样使用的是 <strong>64 位</strong>操作系统，那么我们需要使用 <strong>MSYS2 MinGW x64</strong> 终端。点击终端以打开它。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--33-.png" alt="Screenshot--33-" width="600" height="400" loading="lazy"></p>
<p>⚠️但是，如果你使用的是 <strong>32 位</strong>操作系统，那么你需要使用 <strong>MSYS2 MinGW x86</strong> 终端。然后，你需要打开该终端。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--34-.png" alt="Screenshot--34-" width="600" height="400" loading="lazy"></p>
<p>因为我使用的是 <strong>64 位</strong>操作系统，所以我打开了 64 位终端。使用命令 <code>pacman -S mingw-w64-x86_64-gcc</code> 以安装编译器。</p>
<p>⚠️如果你使用的是 <strong>32 位</strong>操作系统，那么你需要在你的 32 位终端中使用命令 <code>pacman -S mingw-w64-i686-gcc</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--35-.png" alt="Screenshot--35-" width="600" height="400" loading="lazy"></p>
<p>稍等一会儿。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--36-.png" alt="Screenshot--36-" width="600" height="400" loading="lazy"></p>
<p>如果收到安装提示，输入 <code>Y</code> 或 <code>y</code> 并按下回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--37-.png" alt="Screenshot--37-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--38-.png" alt="Screenshot--38-" width="600" height="400" loading="lazy"></p>
<p>等待安装过程完成。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--39-.png" alt="Screenshot--39-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--39--1.png" alt="Screenshot--39--1" width="600" height="400" loading="lazy"></p>
<p>编译器安装完成。</p>
<h2 id="">如何安装调试器</h2>
<p>如果你和我一样使用的是 <strong>64 位</strong>操作系统，那么你需要使用命令 <code>pacman -S mingw-w64-x86_64-gdb</code>。</p>
<p>⚠️如果你使用的是 <strong>32 位</strong>操作系统，那么你需要在你的 32 位终端中使用命令 <code>pacman -S mingw-w64-i686-gdb</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--41-.png" alt="Screenshot--41-" width="600" height="400" loading="lazy"></p>
<p>如果收到安装提示，输入 <code>Y</code> 或 <code>y</code> 并按下回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--42-.png" alt="Screenshot--42-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--38--1.png" alt="Screenshot--38--1" width="600" height="400" loading="lazy"></p>
<p>等待安装过程完成。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--44-.png" alt="Screenshot--44-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--45-.png" alt="Screenshot--45-" width="600" height="400" loading="lazy"></p>
<p>你可以关闭终端。</p>
<h2 id="">如何将目录添加到环境变量的路径</h2>
<p>打开文件资源管理器。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--46-.png" alt="Screenshot--46-" width="600" height="400" loading="lazy"></p>
<p>假设你和我一样将 MSYS 安装到默认目录。如果你使用了自定义目录，则需要前往你安装的目录。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--47-.png" alt="Screenshot--47-" width="600" height="400" loading="lazy"></p>
<p>如果你和我一样使用的是 64 位操作系统，则前往 <strong>mingw64</strong> 文件夹。</p>
<p>⚠️如果你使用的是 32 位操作系统，请前往 <strong>mingw32</strong> 文件夹。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--48-.png" alt="Screenshot--48-" width="600" height="400" loading="lazy"></p>
<p>现在我们需要转到二进制文件夹，前往 <strong>bin</strong> 文件夹。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--49-.png" alt="Screenshot--49-" width="600" height="400" loading="lazy"></p>
<p>⚠️如果你使用的是 32 位操作系统，则需要进入 <strong>mingw32</strong> 文件夹 &gt; <strong>bin</strong> 文件夹。</p>
<p>复制该目录。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--51-.png" alt="Screenshot--51-" width="600" height="400" loading="lazy"></p>
<p>⚠️如果你使用的是 32 位操作系统，并且也将 MSYS2 安装到了默认目录，那么你的目录应该如下所示：</p>
<pre><code>C:\msys64\mingw32\bin
</code></pre>
<p>打开<strong>高级系统设置</strong>。你可以通过多种方式做到这一点，一种简单的方式是点击开始按钮并搜索它，如下图所示。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--52-.png" alt="Screenshot--52-" width="600" height="400" loading="lazy"></p>
<p>从高级选项卡中点击<strong>环境变量</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--54-.png" alt="Screenshot--54-" width="600" height="400" loading="lazy"></p>
<p>点击 <strong>Path</strong> 并选择它。然后点击<strong>编辑</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--57-.png" alt="Screenshot--57-" width="600" height="400" loading="lazy"></p>
<p>将出现如下窗口：</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--58-.png" alt="Screenshot--58-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--59-.png" alt="Screenshot--59-" width="600" height="400" loading="lazy"></p>
<p>会出现一个空白框。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--60-.png" alt="Screenshot--60-" width="600" height="400" loading="lazy"></p>
<p>在这里粘贴目录。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--61-.png" alt="Screenshot--61-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--62-.png" alt="Screenshot--62-" width="600" height="400" loading="lazy"></p>
<p>点击 <strong>OK</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--63-.png" alt="Screenshot--63-" width="600" height="400" loading="lazy"></p>
<p>点击 <strong>OK</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--65-.png" alt="Screenshot--65-" width="600" height="400" loading="lazy"></p>
<p>点击 <strong>OK</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--66-.png" alt="Screenshot--66-" width="600" height="400" loading="lazy"></p>
<p>如果你想获取所有步骤的视频，你也可以观看<a href="https://www.youtube.com/watch?v=0HD0pqVtsmw">这个视频</a>。</p>
<h2 id="">检查安装</h2>
<p>现在是时候检查我们是否成功安装了上述所有内容。</p>
<p>打开终端 / PowerShell / CMD，并逐一应用命令：</p>
<p>检查 <strong>GCC</strong> 版本：</p>
<pre><code class="language-powershell">gcc --version
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--68-.png" alt="Screenshot--68-" width="600" height="400" loading="lazy"></p>
<p>检查 <strong>G++</strong> 版本：</p>
<pre><code class="language-powershell">g++ --version
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--69-.png" alt="Screenshot--69-" width="600" height="400" loading="lazy"></p>
<p>检查 <strong>GDB</strong> 版本：</p>
<pre><code class="language-powershell">gdb --version
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--70-.png" alt="Screenshot--70-" width="600" height="400" loading="lazy"></p>
<h2 id="">结论</h2>
<p>希望本文能帮助你在 Windows 操作系统上为 C 和 C++ 程序安装编译器。</p>
<p>感谢你阅读整篇文章。如果它对你有帮助，你还可以查看我在 <a href="https://www.freecodecamp.org/news/author/fahimbinamin/">freeCodeCamp</a> 上的其他文章。</p>
<p>如果你想与我联系，可以通过 <a href="https://twitter.com/Fahim_FBA">Twitter</a>、<a href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a> 和 <a href="https://github.com/FahimFBA">GitHub</a> 联系我。</p>
<p>如果你希望定期学习各种编程语言的实用例子，可以<a href="https://www.youtube.com/@FahimAmin?sub_confirmation=1">订阅我的 YouTube 频道</a>（Code With FahimFBA）。</p>
<p>如果你想查看我的进展，可以访问我的 <a href="https://www.polywork.com/fahimbinamin">Polywork时间线</a>。</p>
<p>你也可以<a href="https://fahimbinamin.com/">访问我的网站</a>以了解更多关于我的信息和我目前正在做的工作。</p>
<p>非常感谢！</p>
<!--kg-card-end: markdown--> ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ 如何用 sizeof 操作符在 C 语言中查找数组的大小 ]]>
                </title>
                <description>
                    <![CDATA[ 在用 C 语言编程时，有时你可能需要知道一个数组的大小。 例如，当你想遍历存储在数组中的所有元素，以确定一个特定的值是否存在。 在这篇文章中，你将学习如何使用 sizeof() 操作符查找一个数组的大小。 让我们深入了解一下吧。 C 语言中的数组是什么 数组让你在同一个变量名下存储多个值。 C 语言中的数组是相同数据类型的项目的集合。这意味着你可以创建一个只有整数值的数组或一个字符数组，等等。 要在 C 语言中创建一个数组，你首先需要指定数组要存储的值的数据类型。 然后，你给数组一个名称，后面是一对方括号，[]。 在方括号内，你可以指定数组的大小。 所以，这里你可以创建一个名为 faveNumbers 的 int 类型的数组，它将包含 5 个整数： int faveNumbers[5]; 要在数组声明时在其内部插入数值，请使用赋值运算符 =，和一对大括号 {}。 在大括号内，输入项目并以逗号分隔： int faveNumbers[5] = {7, 33, 13, 9, 29}; 上面的代码创建了一个数组，名称为 faveNumbers，其中有 5 个整数 ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/how-to-find-the-size-of-an-array-in-c-with-the-sizeof-operator/</link>
                <guid isPermaLink="false">6396a18ea7bffa07c74414a1</guid>
                
                    <category>
                        <![CDATA[ C 语言 ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Chengjun.L ]]>
                </dc:creator>
                <pubDate>Wed, 07 Dec 2022 03:35:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2022/12/pexels-eduardo-dutra-2115217-1.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p data-test-label="translation-intro">
        <strong>原文：</strong> <a href="https://www.freecodecamp.org/news/how-to-find-the-size-of-an-array-in-c-with-the-sizeof-operator/" target="_blank" rel="noopener noreferrer" data-test-label="original-article-link">How to Find the Size of an Array in C with the sizeof Operator</a>
      </p><p>在用 C 语言编程时，有时你可能需要知道一个数组的大小。</p><p>例如，当你想遍历存储在数组中的所有元素，以确定一个特定的值是否存在。</p><p>在这篇文章中，你将学习如何使用 <code>sizeof()</code> 操作符查找一个数组的大小。</p><p>让我们深入了解一下吧。</p><h2 id="c-">C 语言中的数组是什么</h2><p>数组让你在同一个变量名下存储多个值。</p><p>C 语言中的数组是相同数据类型的项目的集合。这意味着你可以创建一个只有整数值的数组或一个字符数组，等等。</p><p>要在 C 语言中创建一个数组，你首先需要指定数组要存储的值的数据类型。</p><p>然后，你给数组一个名称，后面是一对方括号，<code>[]</code>。</p><p>在方括号内，你可以指定数组的大小。</p><p>所以，这里你可以创建一个名为 <code>faveNumbers</code> 的 <code>int</code> 类型的数组，它将包含 <code>5</code> 个整数：</p><pre><code class="language-c">int faveNumbers[5];
</code></pre><p>要在数组声明时在其内部插入数值，请使用赋值运算符 <code>=</code>，和一对大括号 <code>{}</code>。</p><p>在大括号内，输入项目并以逗号分隔：</p><pre><code class="language-c">int faveNumbers[5] = {7, 33, 13, 9, 29};
</code></pre><p>上面的代码创建了一个数组，名称为 <code>faveNumbers</code>，其中有 <code>5</code> 个整数，<code>7, 33, 13, 9, 29</code>。</p><p>你也可以把上面的代码写成这样：</p><pre><code class="language-c">int faveNumbers[] = {7, 33, 13, 9, 29};
</code></pre><p>在上面的例子中，我没有指定数组的大小。</p><p>然而，编译器可以知道它的大小是 <code>5</code>，因为我在初始化它时给了它 <code>5</code> 个元素。</p><p>这里需要注意的是，一旦你声明了数组，你就不能改变它的大小和类型，因为它们有固定的长度。</p><h2 id="-c-">如何在 C 语言中找到一个数组的大小</h2><p>C 语言没有提供一个内置的方法来获取一个数组的大小。</p><p>尽管如此，它确实有内置的 <code>sizeof</code> 操作符，你可以用它来确定大小。</p><p>使用 <code>sizeof</code> 运算符的一般语法如下：</p><pre><code>datatype size = sizeof(array_name) / sizeof(array_name[index]);
</code></pre><p>让我们把它分解一下：</p><ul><li><code>size</code> 是存储数组大小的变量名，<code>datatype</code> 指定存储在 <code>size</code> 中的数据值的类型。</li><li><code>sizeof(array_name)</code> 计算数组的大小，单位是字节。</li><li><code>sizeof(array_name[index])</code> 计算数组中一个元素的大小。</li></ul><p>现在，让我们看看这个操作的效果，并把它分解成各个步骤，看看它是如何工作的。</p><p>首先，<code>sizeof</code> 操作符返回分配给数组的内存总量，单位是字节。</p><pre><code class="language-c">#include &lt;stdio.h&gt;
int main() {
    // 我的数组
    int faveNumbers[] = {7, 33, 13, 9, 29};

    // 使用 sizeof 来获取数组的字节大小
    size_t size = sizeof(faveNumbers);
    
    printf("The size is %d bytes \n", size);
}

// 输出

// The size is 20 bytes 
</code></pre><p>然而，上面的代码并没有直接计算出数组的大小。</p><p>你将需要一些额外的编程逻辑，像这样：</p><pre><code>array_length = (total size of the array) / (size of the first element in the array) 
</code></pre><p>要想知道数组的长度，你需要用总的内存量除以一个元素的大小——这种方法是有效的，因为数组存储的是相同类型的项目。</p><p>所以，你可以用总的字节数除以数组中第一个元素的大小。</p><p>要访问一个数组中的第一个元素，要指定名称，并在方括号中放入 <code>0</code>。</p><p>在编程和一般的计算机科学中，索引总是从 <code>0</code> 开始，所以数组中的第一个元素的索引总是 <code>0</code>。</p><pre><code class="language-c">#include &lt;stdio.h&gt;
int main() {
    int faveNumbers[] = {7, 33, 13, 9, 29};

    size_t size = sizeof(faveNumbers) / sizeof(faveNumbers[0]);
  
    printf("The length of the array is %d \n", size);
}

// 输出

// The length of the array is 5 
</code></pre><p>这里需要注意的是，数据类型的大小将因平台而异。</p><h2 id="-">总结</h2><p>希望这篇文章能帮助你了解如何使用内置的 <code>sizeof</code> 操作符在 C 语言中确定数组的大小。</p><p>谢谢你阅读本文，祝你编码愉快！</p> ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
