新人Y子のプログラミングのお部屋7

前回は、「Hello, World!」が出力されるソースコードをコンパイルし、実行しました。
今回は、このソースコードを見ていきましょう。

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]「Hello, World!」を出力するコードはどこにあるか分かりますか?

[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]5行目の「printf(“Hello, World!\n”);」ですね。
[/speech_bubble]

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]その通りです。
printfが文字列を出力する命令で、ダブルクォーテーション内側の文字列が出力します。
「Hello, World!\n」が出力する文字列となります。
[/speech_bubble]
[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-hatena.png” name=”新人Y子”]でも、最後の「\n」は出力されてないように見えますが… 
[/speech_bubble]
[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]C言語では、文字列内にある「\」は特別な意味を持ちます。
例えば、「\n」は改行コード、「\t」はTABコードになります。
[/speech_bubble]
[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]改行コード「\n」は文字として見えないのですか?そして、どのような作用があるのでしょう?
[/speech_bubble]
[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]カーソルの動きをイメージしてください。
画面左上から文字を入力していくと、カーソルは右に進んでいきます。
カーソルが右まで行けば、次の行の一番左に行きます。
また、行の途中で改行すると、次の行の一番左に行きます。
例えば、次のプログラムを入力して実行してみてください。


[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]aaaaa
bbbbbccccc

と出力されました。
なるほど。ソースコード上は、aaaaaとbbbbbが同じ行でcccccが別の行でも、「\n」の作用で実行結果はこうなるのですね。
[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”techtrageman-normal.png” name=”テクトレージマン”]次回に続く[/speech_bubble]

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

目次