To display double quotation marks " using the printf function, use¥".

To display double quotation marks " using the printf function, use¥".


To display a yen mark ¥, use ¥¥.





Solar: "By the way, I'm having trouble displaying a string that contains double quotes. I'm not sure what to do... I want to display the message


Display double quotation"


on the command prompt screen. What do you all think?"





Max: "Hmm, what's the issue? Is that all? (´▽`) Why don't you just execute:


printf("Display double quotation " ");


That should do it, right?"




Here is the program:

👇


#include <stdio.h>


int main(void)

{

printf("Display double quotation " ");




return 0;

}


Program execution result (Visual Studio 2019):


Severity Code Description Project File Line Suppression State

Error (Active) E0018 missing ')' Project2 C:\Users\solar\source\repos\Project2\Project2\program1.cpp 6

Error (Active) E0008 missing terminating " character Project2 C:\Users\solar\source\repos\Project2\Project2\program1.cpp 6

Error C2001 newline in constant Project2 C:\Users\solar\source\repos\Project2\Project2\program1.cpp 5

C:\Users\solar\source\repos\Project2\Project2\program1.cpp 6

Error C2143 syntax error: missing ')' before 'return' Project2 C:\Users\solar\source\repos\Project2\Project2\program1.cpp 6

Error C2143 syntax error: missing ';' before 'return' Project2 C:\Users\solar\source\repos\Project2\Project2\program1.cpp 6


Max: "Why isn't it running?"

Solar: " it seems like the compiler is recognizing the entire string data up to the


"Display double quotes "


as a single character string.


"At this rate, it won't be possible to display the phrase


☆Display double quotation ☆


in the command prompt screen."



Aletha:


"Sure! In that case, if you want to use the double quotation as a part of the string data inside the double quotation of the first argument of the printf function, you need to add a \ mark at the beginning of the double quote like this:


\"



."






Therefore,


By rewriting the current program


#include <stdio.h>


int main(void)

{

printf("Display double quotation " ");

return 0;

}


in the following way,


👇

#include <stdio.h>


int main(void)

{

#include <stdio.h>


int main(void)

{

printf("Display double quotation \" ");


return 0;

}



On the command prompt screen, the following message will be displayed:


       Display double quotation "






Program execution output


Displaying double quotation marks " //👈Looks like it's displayed properly, right?

C:\Users\solar\source\repos\Project2\Debug\Project2.exe (process 388636) exited with code 0.

Press any key to close this window...




Solar: "Oh, the double quotation mark " is displayed properly, right?"


ソーラー「ところで



円マーク\を含んだ文字列を表示したい場合は


どうしたらいいのかな


これもうまくいかないんだけど・・・


コマンドプロンプト画面に


円マーク\を表示します


を表示したいんだけどみんなどう思う?」


Solar: "By the way, I'm having trouble displaying a string that includes a backslash character.



I'm not sure what to do...


I want to display the string that


💖💖Display a backslash \ on the command prompt screen💖💖.


but it's not working. What does everyone think?"



Max: "It's easy, isn't it? (^▽^)

Why don't you just execute the following printf statement to display a backslash on the screen?"

👇

printf("Display a backslash \ on the screen");




そのプログラムはこちらです

👇



#include <stdio.h>


int main(void)

{

printf("円マーク\を表示します");


return 0;

}


プログラムの実行結果(Visual Studio2019の場合)


円マークu\示します

C:\Users\solar\source\repos\Project2\Debug\Project2.exe (プロセス 399196) は、コード 0 で終了しました。

このウィンドウを閉じるには、任意のキーを押してください...


エディタ画面の下側には


重大度レベル コード 説明 プロジェクト ファイル 行 抑制状態

エラー (アクティブ) E0870 マルチバイト文字のシーケンスが無効です Project2 C:\Users\solar\source\repos\Project2\Project2\program1.cpp 5

警告 C4129 '': Project2 C:\Users\solar\source\repos\Project2\Project2\program1.cpp 5


と表示されています




マックス「なんでえ~~~ 


         円マークu\示します


が表示されるんだ?」


solarplexuss「よくみると表示の表の字も抜けてる」



ソーラー「あっ ほんとだ!


面白いね


どうなっているんだろう?」



アレサ「


printf関数の第1引数の" "内で


文字列データの1部の文字として円マーク\を用いたい場合は


円マーク\の左隣の円マーク\をくっつけた


               \\


を用いることになります」


ソーラー「ははっ


              \\


円マーク2つならべるんだね」


アレサ「はい


ですので


今のプログラム

👇


#include <stdio.h>


int main(void)

{

printf("円マーク\を表示します");


return 0;

}



次のようにかきかえれば


コマンドプロンプト画面に



         円マーク\を表示します



が表示されることになります」


#include <stdio.h>


int main(void)

{

printf("円マーク\\を表示します");


return 0;

}



プログラムの実行結果



円マーク\を表示します

C:\Users\solar\source\repos\Project2\Debug\Project2.exe (プロセス 398292) は、コード 0 で終了しました。

このウィンドウを閉じるには、任意のキーを押してください...



ソーラー「あっ


ちゃんと


円マーク\を表示します


が表示されたね」









  • Xで共有
  • Facebookで共有
  • はてなブックマークでブックマーク

作者を応援しよう!

ハートをクリックで、簡単に応援の気持ちを伝えられます。(ログインが必要です)

応援したユーザー

応援すると応援コメントも書けます

英語版 Introduction to C Language that Takes You to Heaven: Hexadecimal Structures ver. 2.2117 @solarplexuss

★で称える

この小説が面白かったら★をつけてください。おすすめレビューも書けます。

フォローしてこの作品の続きを読もう

この小説のおすすめレビューを見る

この小説のタグ