Is there a rational reason to use double quotes instead of single quotes to output a single character in C / C ++? For example, why is the space set " " and not ' ' ?"

  • because these types are different - strangeqargo
  • @strangeqargo why put kada can be hacker
  • one
    I will answer the question, because the person became interested, and I never thought about it. A similar question on inglesh stack all in the pros. stackoverflow.com/questions/3683602/… - strangeqargo
  • one
    Specify who and in what cases? For example, I fputc(' ', out); rather write fputc(' ', out); rather than fputs(" ", out); (although it is quite likely that puts(" "); but not putchar(' '); (because of laziness ...)) / If you are interested in rational reasons, rather, on the contrary, it is more rational to transfer the symbol directly to the function ( ie, ""), not its address (in the case of ""). - avp

3 answers 3

  1. because when you write code in which spaces are inserted by the programmer’s hands (this is usually debug / hardcode), you will eventually have to change single quotes to double quotes if you want to add something inside,

    warning from gcc:

     multi-character character constant [-Wmultichar] std::cout << "abc " << 'h ' << "abc"; 

    I don’t need this Vorning, and I don’t need to choose between " and ' during the debug of the program either. And in the production version of the programs, no debugging / hardcoded output is needed.

  2. Besides, "." creates a literal .\0 , a '.' - char '.', and if you mix arrays of characters with \0 and without it, you will be asking for problems with the security and stability of the code.

  • I adore minusators, which instead of giving your answer / improve the existing / comment - strangeqargo
  • one
    Not minus, but after all it is clear that the essence is that these are different types. And the rest (whether to change to double if you need to add something inside) is already a consequence. - Vladimir Gamalyan
  • one
    I will add about the second item in C ++ there is a type std :: string which is not NULL-terminated. - perfect

because these types are different - After all, the person said absolutely ABSOLUTELY.

'' is the type of char , and "" is the type of char* ! And where semantics requires the type char (for example, putc(' ', file); ), apostrophes are used there. And where the semantics require the char* type (for example, fputs(" ", file); ), quotation marks are used there.

  • 2
    " " this is char[2] , not char* - ixSci
  • 2
    @ixSci I would even say that const char[2] . - αλεχολυτ
  • one
    <this is char [2], not char *> - it seems to me. that I have stated my idea quite clearly - the use of one or another variant depends on the semantics of the surrounding CONTNEXT. If the function is described as int input (const char * s, FILE * stream); then the word 'char *' is written, and not 'char [2]'. So here it is necessary to use "". Is there something incomprehensible here ?! - Sergey
  • 2
    @Sergey You confuse the true type of an object with the type to which it is implicitly cast. The fact that an array type is implicitly cast to a pointer type does not make the array a pointer. - αλεχολυτ
  • one
    object - region of data storage - in other words, a piece of memory. I meant the holy trinity C ++ (inheritance, encapsulation and polymorphism). I assume that "region of data storage" does not have these properties? By the way, the authors of the language speak of basic data types and AGGREGATED values ​​(arrays and structures). Without naming values ​​as types. Here in strictly typed languages ​​(Pascal, modula, hell ...), there the array is a type. And even arrays of the same dimension and consisting of elements of the same type will be different types if they differ in the number of these elements themselves. - Sergey

To understand the question, let's compare two variable definitions:

 a) char c = ' '; b) char const* s = " "; 

First you need to understand that the space is the same symbol as the rest. You also need to know that C / C ++ languages ​​are strongly typed , that is, each type has its own size!

Now let's look at how the compiler will act in case (а) . At first he looks to the left side of this expression, an instruction is given there that it is necessary to allocate memory for one character. Then he looks to the right side and checks if it is a single character. To specify a single character, a одинарная кавычка and it is this that he finds first. He says: OK, that's right, the type matches, you need to read one character and check if the other type is looking for a closing single quote. At this point we’ll dwell more: what happens if suddenly there are more than one character between two single quotes? For example, like this: 'аб' . And this will tell the compiler that an error has occurred in the code. Apparently the programmer was planning something else instead of a single character. After checking, he found the symbol put in memory.

Now consider case (б) . First, the compiler looks to the left side of this expression, it gives instructions that you need to allocate a certain size of memory for a set of characters coming one after another. Then he looks to the right side and checks if it is a character set. To specify a character set, a двойная кавычка and he finds it first. He says: OK, that's right, the type is the same, you need to read the characters until they run out. A sign of this end is a двойная кавычка . And according to this principle, the compiler will determine the size of the necessary memory and place the found characters in it.

From all this we conclude that the character and character set are different data types, that is, they have different sizes. In addition, the compiler works with them in different ways.

  • you need to allocate an indefinite memory size - in fact, everything is somewhat more complicated. In this case, the compiler allocates 4 bytes (for 32-bit systems) for the INDEX char * in the stack of the block and an indefinite number of bytes on the heap for the DATA referenced by the pointer. Moreover, if the compiler is optimizing, then it may not allocate a place in the heap at all if this text constant has already been encountered. - Sergey
  • is it a single character? It is also somewhat more complicated here ... I remind you of the existence of such characters as '\ n', '\ 0100', etc. - Sergey
  • @Sergey, you are right in saying, but I didn’t specifically complicate what could capture the essence of the process, besides, I didn’t become attached to specific sizes of data types because string and character data types change from implementation to implementation and are implemented differently in other languages, and that is, binding to a language can be detrimental to the transition to another language. - perfect
  • one
    @Sergey, "выделяет ...и неопределённое количество байт в куче для ДАННЫХ..." is wrong . The compiler does not allocate anything in the heap (never). In this case, it initializes the selected variable in the stack (pointer) with the address of the symbolic constant (right side), which it places in the static data area. More precisely, the constant (physically located in the command area) loaded into this pointer will become the correct address after the linker works on the layout of the load module, but these are already redundant parts for this question. - avp
  • @avp hello, with a constant it is clear that it will be on the stack, and where will the character array lie (for example, char ca[5] = "abcd"; )? - perfect