246

CまたはC++プログラミングで一重引用符と二重引用符を使用する必要があるのはいつですか?

4

14 に答える 14

329

CおよびC++では、一重引用符は単一文字を識別し、二重引用符は文字列リテラルを作成します。'a'は単一の文字リテラルであり、はとnullターミネータ(つまり2文字の配列)"a"を含む文字列リテラルです。'a'

C ++では文字リテラルのタイプはですが、Cでは文字リテラルcharのタイプはですint。つまりsizeof 'a'、intが32ビット(およびCHAR_BITが8)のアーキテクチャでは4であり、sizeof(char)どこでも1であることに注意してください。

于 2010-09-10T09:45:56.677 に答える
50

一部のコンパイラは、複数文字の定数を許可する拡張機能も実装しています。C99標準は次のように述べています。

6.4.4.4p10: "複数の文字(たとえば、'ab')を含む、または1バイトの実行文字にマップされない文字またはエスケープシーケンスを含む整数文字定数の値は、実装によって定義されます。 「」

これは、たとえば次のようになります。

const uint32_t png_ihdr = 'IHDR';

結果の定数(これを実装するGCCで)は、各文字を取得して上にシフトすることで得られる値を持つため、「I」は32ビット値の最上位ビットになります。明らかに、プラットフォームに依存しないコードを作成している場合は、これに依存するべきではありません。

于 2010-09-10T11:00:45.810 に答える
31

一重引用符は文字(char)であり、二重引用符はnullで終了する文字列(char *)です。

char c = 'x';
char *s = "Hello World";
于 2010-09-10T09:45:51.690 に答える
22
于 2014-05-18T14:01:02.607 に答える
12

私は次のようなものを突っついていました:int cc ='cc'; それは基本的に整数へのバイト単位のコピーであることが起こります。したがって、それを見る方法は、基本的に2cである'cc'が整数ccの下位2バイトにコピーされるということです。雑学クイズを探しているなら、

printf("%d %d", 'c', 'cc'); would give:

99 25443

25443 = 99 + 256*99だからです

したがって、「cc」は複数文字の定数であり、文字列ではありません

乾杯

于 2012-05-20T21:29:37.990 に答える
10

一重引用符は1文字を表します。二重引用符は文字列(文字の配列)を表します。必要に応じて、一重引用符を使用して、一度に1文字ずつ文字列を作成できます。

char myChar     = 'A';
char myString[] = "Hello Mum";
char myOtherString[] = { 'H','e','l','l','o','\0' };
于 2010-09-10T09:46:31.043 に答える
8
  1. single quoteのためcharacterです;
  2. double quoteですstring
于 2010-09-10T10:26:04.803 に答える
7

Cでは、などの一重引用符'a'は文字定数を示します"a"が、は文字の配列であり、常に文字で終了し\0ます

于 2010-09-10T09:46:00.780 に答える
6

二重引用符は文字列リテラル用です。例:

char str[] = "Hello world";

一重引用符は、単一文字リテラル用です。例:

char c = 'x';

編集Davidが別の回答で述べたように、文字リテラルのタイプはですint

于 2010-09-10T09:46:16.933 に答える
5

A single quote is used for character, while double quotes are used for strings.

For example...

 printf("%c \n",'a');
 printf("%s","Hello World");

Output

a  
Hello World

If you used these in vice versa case and used a single quote for string and double quotes for a character, this will be the result:

  printf("%c \n","a");
  printf("%s",'Hello World');

output :

For the first line. You will get a garbage value or unexpected value or you may get an output like this:

While for the second statement, you will see nothing. One more thing, if you have more statements after this, they will also give you no result.

Note: PHP language gives you the flexibility to use single and double-quotes easily.

于 2015-08-11T13:57:18.780 に答える
2

次のように、単一引用符と単一文字を使用します。

char ch = 'a';

これ'a'はchar定数であり、charaのASCII値と同じです。

次のように文字列で二重引用符を使用します。

char str[] = "foo";

これ"foo"が文字列リテラルです。

使用"a"しても大丈夫ですが、使用しても大丈夫ではありません 'foo'

于 2010-09-10T09:50:04.940 に答える
2

Single quotes are denoting a char, double denote a string.

In Java, it is also the same.

于 2014-08-05T01:07:10.683 に答える
1

While I'm sure this doesn't answer what the original asker asked, in case you end up here looking for single quote in literal integers like I have...

C++14 added the ability to add single quotes (') in the middle of number literals to add some visual grouping to the numbers.

constexpr int oneBillion = 1'000'000'000;
constexpr int binary = 0b1010'0101;
constexpr int hex = 0x12'34'5678;
constexpr double pi = 3.1415926535'8979323846'2643383279'5028841971'6939937510;
于 2021-09-29T20:09:16.813 に答える
0

In C & C++ single quotes is known as a character ('a') whereas double quotes is know as a string ("Hello"). The difference is that a character can store anything but only one alphabet/number etc. A string can store anything. But also remember that there is a difference between '1' and 1. If you type cout<<'1'<<endl<<1; The output would be the same, but not in this case:

cout<<int('1')<<endl<<int(1);

This time the first line would be 48. As when you convert a character to an int it converts to its ascii and the ascii for '1' is 48. Same, if you do:

string s="Hi";
s+=48; //This will add "1" to the string
s+="1"; This will also add "1" to the string
于 2020-11-05T16:47:50.740 に答える