開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- コンパイラー: LLVM/Clang, GCC(gcc)
- プログラミング言語: C
中学数学からはじめる暗号入門 ~現代の暗号はどのようにして作られたのか~ (知りたい!サイエンス 141) (関根 章道(著)、技術評論社))の前編(暗号の歴史あれこれ)、第2章(またまた被害に - シーザー式暗号とエニグマ暗号機)の31ページのコラム(プログラム言語)のC言語のプログラム例に誤植(?)があったから報告してみた。
誤植といっても、あくまで例だから、コンパイルできる正確なコードを記述するという意図はなく、それっぽいC言語のコードを例示するためだけで誤植ではないのかも。
本著のコードはintではなくIntとiが大文字になっているのと、セミコロン「;」がない。
#include <stdio.h>
Int main(void) {
printf("hello!")
return 0
}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ cc sample.c
sample.c:3:1: error: unknown type name 'Int'; did you mean 'int'?
Int main(void) {
^~~
int
sample.c:4:19: error: expected ';' after expression
printf("hello!")
^
;
sample.c:5:11: error: expected ';' after return statement
return 0
^
;
3 errors generated.
$
修正。
#include <stdio.h>
int main(void) {
printf("hello!");
return 0;
}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ cc sample.c $ ./a.out hello!$
おまけ。
21st Century Cを参考に今っぽく。あと改行を追加して出力を読みやすく。
#include <stdio.h>
// void削除
int main() {
// 改行を追加
printf("hello!\n");
// returnの書略
/* return 0; */
}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ cc sample.c $ ./a.out hello! $
さらにおまけ。(コメントを省略しただけ。)
#include <stdio.h>
int main() {
printf("hello!\n");
}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ cc sample.c $ ./a.out hello! $
以前と同様に報告完了。
0 コメント:
コメントを投稿