Resolve differences between the C language "character" and "string"

  In the C language, between "character" and "string", there is a difference. This article, we will introduce, in the C language of "character" and "string", the difference between them.

  First of all, a very obvious difference is:

  "Characters", the use of single quotes as delimiters, and "string" is used as a delimiter double quotes.

We could start with a definition of "character" of the program code segments:

  Above the C language program, line 5, is the definition of a "character" variable str, the definition of "character" variable, use the keyword "char", in essence, the keyword "char" is defined by an integer this integer occupies a byte in memory. The ASCII code corresponding appearances. Such as the commonly ASCII code table corresponding relationship is: character numeral 48 '0'; numeral 65 designates the character 'A'; numeral 97 represents a character 'a'

  In the definition of a "character" when the delimiter to be written in single quotes. Print a character on the screen when needed in the printf function, use "% c" placeholder.

  Let us look at the definition of a string of code fragment:

  C language program described above, the first line 5, is the definition of a "string." Using an array of char format. Further, both sides of the delimiter character string constants, using double quotes.

  To be printed on the screen when a character string, in the printf function, using the "% s" placeholder. And "character" of the "% c" The placeholder is different.

 

Between "character" and "string", except for delimiters, there is a difference:

"Character" occupies a byte, but the "string" is occupied by a plurality of bytes. Further, at the end of the "string" is automatically added by the compiler '\ 0' the characters in the ASCII code, '\ 0' is represented by a null character.

If you define a string constant when using single quotes, the program will error. In the definition of the string, the need to add "in brackets" after the variable name, similar to the definition of "array" time format.

 

Guess you like

Origin www.cnblogs.com/mhq-martin/p/11392108.html