Single Quotes Vs. Double Quotes in C or C++

Single quotes vs. double quotes in C or C++

In C and in C++ single quotes identify a single character, while double quotes create a string literal. 'a' is a single a character literal, while "a" is a string literal containing an 'a' and a null terminator (that is a 2 char array).

In C++ the type of a character literal is char, but note that in C, the type of a character literal is int, that is sizeof 'a' is 4 in an architecture where ints are 32bit (and CHAR_BIT is 8), while sizeof(char) is 1 everywhere.

Why do 'char' values have to have single quotation marks instead of double? [duplicate]

They are needed because 'C' and "C" represent completely different types - the first is an integer value, while the second is an array of two characters (the letter 'C' plus an implicit null-terminator). Both are useful, and you need some way of saying which one you want, which is what the different kinds of quotes do.

What is the significance of single quotes vs double quotes in comparisons? [duplicate]

Single-quotes denote a character literal. Double-quotes denote a string literal.

So '-' is of type char1, whereas "-" is of type const char[2] (which typically decays to const char *).


1 int in C.

C - Difference between quotation and apostrophe ( vs ' ) [duplicate]

The elements in this array are characters (1 byte each):

char test[5] = {'c', 'o', 'o', 'l', '\0'};

This is a null-terminated C string. It's represented exactly the same way in memory. It consists of exactly 5 bytes: the letters "cool", and the terminating null character:

char test2[5] = {"cool"};

And this consists of two bytes:

char test3[] = "c";

Your original example is an array of 2-byte strings. Unlike the previous examples, it's actually a 2-level array. You must declare it as such:

char *test[] = {"c", "o", "o", "l", "\0"};

usage of two characters in single quotes in c

The type of a single-quoted literal is int. So the size is typically large enough for more than one character's worth of bits. The exact way the characters are interpreted is, as far as I know, implementation-dependent.

In your case, you're getting a little-endian ordering:

  • The ASCII value for 'a' is 97 (0x61)
  • The ASCII value for 'g' is 103 (0x67)

Your value is 24935 = 0x6167, so you're getting the 'a' in the higher byte and the 'g' in the lower.

What are the functional differences between single-quoted vs double-quoted html attributes?

There is no functional difference. Quoting the W3C on SGML and HMTL:

By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa.

...

In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). We recommend using quotation marks even when it is possible to eliminate them.

What's the difference between single and double quotes in Objective-C?

The difference between single and double quotes is that double quotes declare a string, and single quotes declare a single character. Try doing this, you will get a syntax error:

'More than one character'

The reason that your code outputted a bunch of random characters is that strings are not integers like most other data types, but pointers. This means that when you type "A string", the result of the expression is the memory location that the characters are stored at. This could be anywhere in memory, depending on when you start the program. So, when you added random() to the string, it gave you a random memory address! The statement was equivalent to this in English:

Store the characters "A" in memory, and then give me the memory
address a random amount of cells later.

The random amount of cells later could be anything else in your program. The pointer was interpreted as an character (because of %c), but it wasn't meant to, giving you seemingly random output.



Related Topics



Leave a reply



Submit