Skip to Content
21st Century C, 2nd Edition
book

21st Century C, 2nd Edition

by Ben Klemens
May 2025
Intermediate to advanced
408 pages
5h 55m
Japanese
O'Reilly Media, Inc.
Content preview from 21st Century C, 2nd Edition

第10章. より良い構造

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

29種類の属性があり、気に入ったのは7種類だけだ。

ザ・ストロークス、"You Only Live Once"

この章では、構造化された入力を受け取る関数と、ライブラリのユーザ・インタフェースの改良について説明する。

この章ではまず、ISO C99規格でCに導入された3つの構文、すなわち複合リテラル、可変長マクロ、指定初期化子を取り上げる。この章では、これらの要素の組み合わせによってできることのすべてを探求している。

複合リテラルを使えば、より簡単にリストを関数に送ることができる。そして、可変長マクロを使えば、ユーザから複合リテラルの構文を隠すことができ、任意の長さのリストを受け取ることができる関数を残すことができる。f(1, 2)f(1, 2, 3, 4) も同様に有効である。

他の多くの言語で見られるように、foreach キーワードを実装したり、1入力の関数をベクトル化して複数の入力で演算するようにするために、同様の形式を使用することができる。

指定初期化によって、構造体の扱いが非常に簡単になり、私は以前の方法をほとんど使わなくなった点である。person_struct p = {"Joe", 22, 75, 20} のような読みにくくてエラーになりやすいゴミの代わりに、 person_struct p = {.name="Joe", .age=22, .weight_kg=75, .education_years=20}ような自己文書化された宣言を書くことができる。

構造体を初期化することが苦にならないようになった今、関数から構造体を返すことも苦にならず、関数インタフェースを明確にするために大いに役立つ。

構造体を関数に送ることも、より現実的な選択肢になる。すべてを別の可変長マクロでラップすることで、関数のユーザが指定しない関数の引数にデフォルト値を割り当てることもできる。ローン計算機の例では、amortization(.amount=200000, .rate=4.5, .years=30)amortization(.rate=4.5, .amount=200000) の両方が有効な関数である。2番目の呼び出しではローン期間が指定されていないため、関数はデフォルトの30年ローンを使用する。

残りの章では、ボイドポインタに基づく関数インタフェースを扱う場合や、ひどいインタフェースを持つレガシーコードをユーザビリティのあるものにラップする必要がある場合など、入出力構造体を使うことで生活が楽になる状況の例をいくつか紹介する。

複合リテラル

double a_value という宣言があれば、Cは を理解するのに問題はない。f(a_value)

しかし、要素のリスト({20.38, a_value, 9.8}のような複合リテラル値)を送りたい場合は、構文上の注意点がある。複合リテラルの前にタイプキャストを置かないと、パーサが混乱してしまう。リストは(double[]) {20.38, a_value, 9.8} のようになり、呼び出しは次のようになる:

f((double[]) {20.38, a_value, 9.8});

つまり、mallocfree も必要ない。複合リテラルが現れるスコープの最後では、それはただ消えるだけである。

例10-1はdouble ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learn C Programming - Second Edition

Learn C Programming - Second Edition

Jeff Szuhay
Tiny C Projects

Tiny C Projects

Daniel Gookin

Publisher Resources

ISBN: 9798341650848