第4章アルゴリズムの言語による実現:低水準プログラミング言語
4.2 Building C Programs4.2 Cプログラムを作る
Learning Objectives学習目標
By the end of this section, you will be able to:
この節を終えると、次のことができるようになる。
• Write C code using fundamental elements of the language
• C言語の基本要素を使ってCのコードを書く。
• Summarize the steps to develop a C program
• Cプログラムを開発する手順を要約する。
• Understand the process to compile and run a C program
• Cプログラムをコンパイルして実行する過程を理解する。
• Describe how linking is used in a C program
• Cプログラムでリンク処理がどう使われるかを説明する。
• Understand how to apply version control management
• バージョン管理を適用する方法を理解する。
As discussed, a programming language is a kind of computational model that is used to write programs. C is a popular middle-level language that is widely used to create systems software. This section is a crash course in the essentials of C.
英語のヒント
先に説明したように、プログラミング言語は、プログラムを書くために使う計算モデルの一種である。Cは、システムソフトウェアの作成に広く使われる、人気のある中水準言語である。この節では、Cの必須事項を短期集中で学ぶ。
Introduction to CC入門
The C programming language was invented in 1972 by Dennis Ritchie of Bell Labs (Figure 4.15) and popularized by the book The C Programming Language by Brian Kernighan and Dennis Ritchie. C’s peculiar name—a single letter—was a pragmatic choice, since C replaced an earlier language named B. C is a procedural, middle-level language that gives programs low-level access to memory. It is a relatively simple language, which makes learning C, and creating a C compiler, easier than for more complex languages. This combination of features made C an instant hit, and it has maintained great popularity and import to this day. C has influenced other programming languages, too. C++ is a newer language that adds the object-oriented paradigm to C.
英語のヒント
Cプログラミング言語は、ベル研究所のデニス・リッチー(図4.15)が1972年に考案し、ブライアン・カーニハンとデニス・リッチーによる書籍『The C Programming Language』によって広まった。Cという1文字だけの独特な名前は、以前のBという言語に取って代わったことから、実用的に選ばれた。Cは手続き型の中水準言語であり、プログラムからメモリへ低水準でアクセスできる。比較的単純な言語なので、より複雑な言語と比べて、Cを学ぶことも、Cコンパイラーを作ることも容易である。このような特徴の組合せによってCはたちまち好評を博し、今日まで大きな人気と重要性を保っている。Cは、ほかのプログラミング言語にも影響を与えた。C++は、Cにオブジェクト指向のパラダイムを加えた、より新しい言語である。

credit: “Dennis Ritchie 2011” by Denise Panyik-Dale/ Wikimedia Commons, CC BY 2.0 · CC BY 2.0 · Source出典
Figure 4.15 Dennis Ritchie of Bell Labs invented the C programming language. (credit: “Dennis Ritchie 2011” by Denise Panyik-Dale/ Wikimedia Commons, CC BY 2.0)
図4.15 ベル研究所のデニス・リッチーは、Cプログラミング言語を考案した。(クレジット:Denise Panyik-Dale/Wikimedia Commonsによる「Dennis Ritchie 2011」、CC BY 2.0)
Why is C so popular? Mainly because its designers managed to strike a balance between low-level and middle-level features that allows C code to execute at practically the same speed as assembly language, while allowing programmers to be productive enough to create large, dependable, programs. C is the programming language behind much of the lower-level software that we depend on, including operating systems, language compilers, assemblers, text editors, print servers, network drivers, language interpreters, and command-line utilities. Here are some specific software products that are written in C:
英語のヒント
なぜCはこれほど人気があるのだろうか。主な理由は、設計者が低水準と中水準の機能の釣合いをうまく取ったことである。そのおかげで、Cのコードは事実上アセンブリ言語と同じ速度で実行でき、その一方でプログラマーは、大規模で信頼できるプログラムを作るのに十分な生産性を保てる。Cは、オペレーティングシステム、言語コンパイラー、アセンブラー、テキストエディター、プリントサーバー、ネットワークドライバー、言語インタープリター、コマンドラインユーティリティーなど、私たちが頼る低水準寄りのソフトウェアの多くを支えるプログラミング言語である。Cで書かれた具体的なソフトウェア製品には、次のものがある。
• The Java virtual machine (ANSI C)
• Java仮想マシン(ANSI C)
• Linux, an open-source operating system (C, and some assembly)
• Linux、オープンソースのオペレーティングシステム(Cと一部のアセンブリ言語)
• Python (C)
• Python(C)
• macOS X kernel (C)
• macOS Xのカーネル(C)
• Windows (C, C++)
• Windows(C、C++)
• The Oracle database (C, C++)
• Oracleデータベース(C、C++)
• Cisco routers (C)
• Ciscoのルーター(C)
INDUSTRY SPOTLIGHT産業の現場から
Applications of CCの応用
C is used in a variety of industries. One example is astrophysics, where scientists write programs that simulate the motion of stellar bodies, and control instruments such as telescopes. Owing to the large size of the universe, these simulations involve performing calculations on very large arrays of numbers. C’s ability to execute fast, and control the layout of large arrays in memory, is advantageous for this application. As a relatively simple language, C is approachable to physicists who are not necessarily expert in computer science. Scientific experiments need to be reproducible, which means that code involved in science needs to work even decades in the future. The fact that C has been a stable, popular language for so long means that it is very likely to endure, which cannot be said of newer niche languages.
英語のヒント
Cは、さまざまな産業で使われている。たとえば宇宙物理学では、科学者が天体の運動をシミュレーションしたり、望遠鏡などの装置を制御したりするプログラムを書く。宇宙が非常に大きいため、これらのシミュレーションでは、数値の巨大な配列に対する計算を行う。Cが高速に実行でき、大きな配列のメモリ内の配置を制御できることは、この用途に有利である。Cは比較的単純な言語なので、必ずしも計算機科学の専門家ではない物理学者にも取り組みやすい。科学実験には再現可能性が必要であり、科学で使うコードは数十年先でも動く必要があるということである。Cがこれほど長く安定した人気の言語であったという事実は、今後も存続する可能性が非常に高いことを意味する。新しく用途の限られた言語については、同じことはいえない。
One notable feature of C is the way it handle memory. In a program, we have variables and values. For example, in x = 10, x is a variable and 10 is the value. Every value in a program is stored in memory. Memory regions are divided into four blocks: stack, heap, static, and code blocks. These regions store various parts of a running program. Running programs create and destroy values extremely rapidly (perhaps millions or billions per second), and memory is finite, so memory locations must be reused, or else would run out quickly. When a value is created, memory is set aside as allocated memory to hold that value. Eventually, when the value is no longer needed, that memory becomes freed memory, meaning it is given back so that it can be reused. The process of allocating and freeing memory is called memory management. A memory leak happens when some memory is allocated but never freed. A memory leak is a bug that causes a program to waste resources; severe leaks can waste all the memory on the computer, causing it to become unresponsive or crash. As a middle-level programming language, C requires programmers to handle memory management manually. This type of flexibility must be used with caution as it may result in creating programs that are not reliable and secure. In high-level languages, memory management is automated.
英語のヒント
Cの注目すべき特徴の1つは、メモリの扱い方である。プログラムには変数と値がある。たとえばx = 10では、xが変数で、10が値である。プログラム内の値はすべてメモリに格納される。メモリ領域は、スタック、ヒープ、静的領域、コード領域という4つのブロックに分けられ、実行中のプログラムのさまざまな部分を格納する。実行中のプログラムは、きわめて高速に、場合によっては毎秒数百万から数十億もの値を生成し、破棄する。メモリは有限なので、メモリの場所を再利用しなければ、すぐに使い尽くしてしまう。値を生成すると、その値を保持するためにメモリを確保し、割り当て済みメモリとする。やがて値が不要になると、そのメモリは解放済みメモリになる。つまり、再利用できるよう返却される。このようにメモリを割り当て、解放する過程を、メモリ管理という。メモリを割り当てたまま解放しないと、メモリリークが起こる。メモリリークはプログラムが資源を浪費するバグであり、深刻なリークではコンピューターのメモリをすべて浪費し、応答しなくなったり、クラッシュしたりすることがある。中水準プログラミング言語であるCでは、プログラマーが手動でメモリ管理を行う必要がある。この柔軟性は、信頼性や安全性のないプログラムを作ることにもつながるため、慎重に使わなければならない。高水準言語では、メモリ管理は自動化されている。
Here are some other notable features of C:
Cには、ほかにも次のような注目すべき特徴がある。
1. Efficient execution: C is lower in expressive power than some other middle-level languages like C++ and yet simple enough that compilers can generate machine code that is comparable in speed to hand-written assembly code. A lot of research and development have focused on creating performance-oriented C compilers.
英語のヒント
1. 効率的な実行:Cは、C++のようなほかの中水準言語より表現力が低い一方、コンパイラーが手書きのアセンブリコードに匹敵する速度の機械語コードを生成できるほど単純である。多くの研究開発が、性能を重視したCコンパイラーの作成に力を注いできた。
2. Portability: C can run in multiple computing environments, also known as having the property of portability. Unix was designed to work on various hardware architectures, so the C language is not hardware-dependent. The same C code can be compiled and executed on different hardware architectures and operating systems.
英語のヒント
2. 移植性:Cは複数の計算環境で実行でき、この性質を移植性があるともいう。Unixはさまざまなハードウェア構成で動くように設計されたため、C言語はハードウェアに依存しない。同じCのコードを、異なるハードウェア構成やオペレーティングシステムでコンパイルし、実行できる。
3. Modularity: Modular programming refers to the process of dividing computer programs into separate sub-programs. A module is a separate software component, such as an error handler, that may be used by a variety of applications and functions within a system. C has language support for modularity.
英語のヒント
3. モジュール性:モジュール化プログラミングとは、コンピュータープログラムを独立したサブプログラムに分割する過程である。モジュールは、エラーハンドラーなどの独立したソフトウェア部品であり、システム内のさまざまなアプリケーションや関数で使える。Cは、言語の機能としてモジュール性を支える。
4. Procedural and structured programming support: C adheres to the procedural and structured paradigms.
4. 手続き型・構造化プログラミングへの対応:Cは、手続き型と構造化のパラダイムに従う。
5. Data types and operators: Every variable in a C program has a data type. Data types dictate how much memory is used to store the variable, and which kinds of operators can be used with the variable.
英語のヒント
5. データ型と演算子:Cプログラム内の変数には、すべてデータ型がある。データ型は、その変数を格納するために使うメモリ量と、その変数に使える演算子の種類を定める。
6. Recursion support: Recursion is the phenomenon of a system being defined in terms of itself. In code, this means a function may call itself again and again. C supports recursion. However, it does not provide a feature called “tail-calling” that makes recursion efficient, so recursion is not used in C as much as in languages that provide tail-calling. A tail call is a function call performed as the final action of a function. If the target function of a tail is the same function, the function is said to be tail recursive, which is a special case of recursion. Tail recursion (also called “tail-end recursion”) is useful and helps with code optimizations.
英語のヒント
6. 再帰への対応:再帰とは、あるシステムがそのシステム自身を使って定義される現象である。コードでは、関数が自分自身を何度も呼び出せることを意味する。Cは再帰に対応する。しかし、再帰を効率的にする「末尾呼出し」という機能は提供しないため、末尾呼出しのある言語ほど、Cでは再帰が使われない。末尾呼出しは、ある関数の最後の動作として行う関数呼出しである。末尾呼出しの呼出し先が同じ関数であれば、その関数は末尾再帰であるといい、これは再帰の特殊な場合である。末尾再帰(tail-end recursionともいう)は有用で、コードの最適化に役立つ。
7. Pointers: A pointer is a variable that holds the memory address of another variable and points to that variable (Figure 4.16). Pointers play a crucial role in the C language. They are used to store and manage addresses of dynamically allocated blocks in memory in the underlying computer system. Managing hardware devices involves manipulating certain memory locations, and C’s support for pointers is one of the reasons that it is used to implement kernels and device drivers.
英語のヒント
7. ポインター:ポインターとは、別の変数のメモリアドレスを保持し、その変数を指す変数である(図4.16)。ポインターは、C言語で非常に重要な役割を果たす。基盤となるコンピューターシステムのメモリ内で、動的に割り当てたブロックのアドレスを格納し、管理するために使われる。ハードウェア装置を管理するには、特定のメモリ位置を操作する必要がある。Cがポインターに対応することは、カーネルやデバイスドライバーの実装にCが使われる理由の1つである。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 4.16 A pointer is a variable whose value is another variable’s address in memory. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.16 ポインターとは、その値が、メモリ内の別の変数のアドレスである変数である。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)