Cprogramming 简明教程

C - Environment Setup

要开始学习 programming in C ,第一步是要设置一个环境,使您能够用 C 编写和编辑程序,以及一个编译器,它能生成可以在您的操作系统上运行的可执行文件。需要在计算机上配备两个软件工具,(a) C 编译器和 (b) 文本编辑器。

To start learning programming in C, the first step is to setup an environment that allows you to enter and edit the program in C, and a compiler that builds an executable that can run on your operating system. You need two software tools available on your computer, (a) The C Compiler and (b) Text Editor.

The C Compiler

用源代码文件中编写源代码是您程序的人类可读源。它需要被 “编译” 为机器语言,以便您的 CPU 实际上可以按照给定的指令执行该程序。

The source code written in the source file is the human readable source for your program. It needs to be "compiled", into machine language so that your CPU can actually execute the program as per the instructions given.

有很多可用的 C 编译器。以下是广泛使用的 C 编译器精选列表 −

There are many C compilers available. Following is a select list of C compilers that are widely used −

GNU Compiler Collection (GCC) − GCC 是一个流行的开源 C 编译器。它适用于广泛的平台,包括 Windows、macOS 和 Linux。GCC 以其广泛的功能和对各种 C 标准的支持而闻名。

GNU Compiler Collection (GCC) − GCC is a popular open-source C compiler. It is available for a wide range of platforms including Windows, macOS, and Linux. GCC is known for its wide range of features and support for a variety of C standards.

Clang :Clang 是 LLVM 项目的一部分,是一个开源 C 编译器。它适用于广泛的平台,包括 Windows、macOS 和 Linux。Clang 以其速度和优化能力而闻名。

Clang: Clang is an open-source C compiler that is part of the LLVM project. It is available for a variety of platforms including Windows, macOS, and Linux. Clang is known for its speed and optimization capabilities.

Microsoft Visual C − Microsoft Visual C 是由 Microsoft 开发的一个专有 C 编译器。它仅适用于 Windows。Visual C++ 以其与 Microsoft Visual Studio 开发环境的集成而闻名。

Microsoft Visual C − Microsoft Visual C is a proprietary C compiler that is developed by Microsoft. It is available for Windows only. Visual C++ is known for its integration with the Microsoft Visual Studio development environment.

Turbo C − Turbo C 是一个由 Borland 开发的已停产的 C 编译器。它在 20 世纪 90 年代早期很流行,但不再广泛使用。

Turbo C − Turbo C is a discontinued C compiler that was developed by Borland. It was popular in the early 1990s, but it is no longer widely used.

本教程中的示例是在 GCC 编译器上编译的。最常用的免费编译器是 GNU C/C 编译器。下一部分将讲解如何在各种操作系统上安装 GNU C/C 编译器。我们一直把 C/C 放在一起,因为 GNU gcc 编译器适用于 C 和 C programming languages

The examples in this tutorial are compiled on the GCC compiler. The most frequently used and free available compiler is the GNU C/C compiler. The following section explains how to install GNU C/C compiler on various operating systems. We keep mentioning C/C together because GNU gcc compiler works for both C and C programming languages.

Installation on UNIX/Linux

如果您使用 Linux 或 UNIX,则输入以下命令从命令行检查你的系统上是否安装了 GCC −

If you are using Linux or UNIX, then check whether GCC is installed on your system by entering the following command from the command line −

$ gcc -v

如果您在 Ubuntu Linux 机器上安装了 GNU 编译器,则它应打印如下消息 −

If you have GNU compiler installed on your Ubuntu Linux machine, then it should print a message as follows −

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v . . .
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)

如果未安装 GCC,则您将不得不使用 https://gcc.gnu.org/install/ 中提供的详细说明自己安装它。

If GCC is not installed, then you will have to install it yourself using the detailed instructions available at https://gcc.gnu.org/install/

Installation on Mac OS

如果您使用 Mac OS X,获取 GCC 的最简单方法是从 Apple 的网站下载 Xcode 开发环境,并按照简单的安装说明进行操作。一旦设置了 Xcode,您将能够为 C/C++ 使用 GNU 编译器。

If you use Mac OS X, the easiest way to obtain GCC is to download the Xcode development environment from Apple’s web site and follow the simple installation instructions. Once you have Xcode setup, you will be able to use GNU compiler for C/C++.

要在 Windows 上安装 Xcode,请访问 developer.apple.com/technologies/tools/。

Xcode is currently available at developer.apple.com/technologies/tools/

Installation on Windows

要在 Windows 上安装 GCC,则需要安装 MinGW。若要安装 MinGW,请转到 MinGW 下载页面 https://www.mingw-w64.org/downloads/ ,然后单击指向 MinGW 下载页面的链接。从此处下载最新版本的 MinGW 安装程序 mingw-w64-install.exe。

To install GCC on Windows, you need to install MinGW. To install MinGW, go to the MinGW downloads page, https://www.mingw-w64.org/downloads/, and follow the link to the MinGW download page. Download the latest version of the MinGW installation program, mingw-w64-install.exe from here.

安装 Min GW 时,至少必须安装 gcc-core、gcc-g++、binutils 和 MinGW 运行时,但也可以安装更多。

While installing Min GW, at a minimum, you must install gcc-core, gcc-g++, binutils, and the MinGW runtime, but you may wish to install more.

将 MinGW 安装的 bin 子目录添加到 PATH 环境变量中,以便通过简单名称在命令行中指定这些工具。

Add the bin subdirectory of your MinGW installation to your PATH environment variable, so that you can specify these tools on the command line by their simple names.

安装完成后,你将能够从 Windows 命令行运行 gcc、g++、ar、ranlib、dlltool 和其他几个 GNU 工具。

After the installation is complete, you will be able to run gcc, g++, ar, ranlib, dlltool, and several other GNU tools from the Windows command line.

Text Editor

你需要一个文本编辑器来键入你的程序。示例包括 Windows 记事本、OS 编辑命令、Brief、Epsilon、EMACS 以及 vim 或 vi。

You will need a Text Editor to type your program. Examples include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.

文本编辑器的名称和版本在不同的操作系统上可能有所不同。例如,将在 Windows 上使用记事本,还可以在 Windows 及 Linux 或 UNIX 上使用 vim 或 vi。

The name and version of the text editors can vary on different operating systems. For example, Notepad will be used on Windows, and vim or vi can be used on windows as well as on Linux or UNIX.

使用编辑器创建的文件称为源文件,它们包含程序源代码。C 程序的源文件通常以扩展名“c”命名。

The files you create with your editor are called the source files and they contain the program source codes. The source files for C programs are typically named with the extension ".c".

开始编程之前,请确保你已安装一个文本编辑器,并且有编写计算机程序、将其保存在文件中、对其进行编译并最终执行它的足够经验。

Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, compile it and finally execute it.

Using an IDE

使用记事本或 vi 等通用文本编辑器进行程序开发可能会非常繁琐。你需要以“.c”扩展名输入并保存程序(例如“hello.c”),然后使用以下命令对其进行编译−

Using a general-purpose text editor such as Notepad or vi for program development can be very tedious. You need to enter and save the program with ".c" extension (say "hello.c"), and then compile it with the following command −

gcc -c hello.c -o hello.o
gcc -o hello.exe hello.o

然后从命令提示符运行可执行文件以获取输出。但是,如果源代码包含错误,则无法成功编译。因此,我们需要多次在编辑器程序和命令终端之间切换。为了避免这一繁琐的过程,我们应该使用 IDE(集成开发环境)。

The executable file is then run from the command prompt to obtain the output. However, if the source code contains errors, the compilation will not be successful. Hence we need to repeatedly switch between the editor program and command terminal. To avoid this tedious process, we should an IDE (Integrated Development Environment).

有许多 IDE 可用于编写、编辑、调试和执行 C 程序。示例包括 CodeBlocks、NetBeans、VSCode 等。

There are many IDEs available for writing, editing, debugging and executing C programs. Examples are CodeBlocks, NetBeans, VSCode, etc.

CodeBlocks 是一款流行的用于 C 和 C++ 的开源 IDE。它适用于各种操作系统平台,如 Windows、Linux、MacOS。

CodeBlocks is a popular open-source IDE for C and C++. It is available for installation on various operating system platforms like Windows, Linux, MacOS.

对于 Windows,请从 https://www.codeblocks.org/downloads/binaries/ URL 下载 codeblocks-20.03mingw-setup.exe。这会在你的计算机上安装 CodeBlocks 及 MinGW 编译器。在安装过程中,选择 MinGW 作为要使用的编译器。

For Windows, download codeblocks-20.03mingw-setup.exe from https://www.codeblocks.org/downloads/binaries/ URL. This will install CodeBlocks as well as MinGW compiler on your computer. During the installation process, choose MinGW as the compiler to use.

Example

安装完成后,启动它并输入以下代码−

After the installation is complete, launch it and enter the following code −

#include <stdio.h>

int main() {
   /* my first program in C */
   printf("Hello, World! \n");

   return 0;
}
Hello, World!

从“生成”菜单中生成并运行程序(使用 F9 快捷方式)。“生成日志”窗口显示成功编译的消息。输出( Hello World )显示在单独的命令提示符终端中。

From the Build menu, build and run the program (use F9 shortcut). The Build Log window shows successful compilation messages. The output (Hello World) is displayed in a separate command prompt terminal.

hello world