1-3. String, escape character, comment

1. String type

"hello boys and girls. \n"
This string of characters enclosed by double quotes is called String Literal, or simply string.
As follows:

   	• Example 1
    1 //String type
    2 #include <stdio.h>
    3 int main()
    4 {
    5     "abcdef";
    6     "";//Empty string
    7     return 0;
    8 }
    
    • Example 2
	Code 1
    1 #include <stdio.h>
    2 int main()
    3 {
    4     char arr1[] = "abc";//If you want to save this string - array
    5     char arr2[] = { 'a','b','c' };//Characters are enclosed in single quotes
    6     printf("%s\n", arr1);
    7     printf("%s\n", arr2);//Some strange characters will be printed after abc
    8     return 0;
    9 }
	Code 2
    1 #include <stdio.h>
    2 int main()
    3 {
    4     char arr1[] = "abc";
    5     //"abc" -- {'a','b','c',''a', 'B', 'C', '\ 0'}, '\ 0''},''a', 'B', 'C', '\ 0'}, '\ 0'' - end flag of string
    6     char arr2[] = { 'a','b','c' ,0};
    7     //0 can also be replaced with '\ 0'. If it is not added, random characters will be printed
    8     printf("%s\n", arr1);
    9     printf("%s\n", arr2);
   10     return 0;
   11 }

Note: the end flag of the string is an escape character of \ 0. When calculating the string length (strlen function), \ 0 is the end flag and is not counted as a string
Content.
When data is stored on the computer, it is binary, #ah $how can these characters be stored in memory and become binary—— Assign a number to a character, for example, a corresponds to 97, A - 65,... The encoding method of assigning a value to each character is called ASCII encoding, and the value of the character pair is called ASCII code value. These values are summarized into an ASCII table, where \ 0 corresponds to 0 (\ 0 is a character)

	• Example 3
	    1 #include <stdio.h>
	    2 int main()
	    3 {
	    4     char arr1[] = "abc";//3
	    5     char arr2[] = { 'a','b','c' };//15 - this is a random value
	    6     printf("%d\n", strlen(arr1));
	    7     //Strlen string length - to calculate the length of a string
	    8     printf("%d\n", strlen(arr2));
	    9     return 0;
   		10 }

2. Escape character

  • Example 1
    1 #include <stdio.h>
    2 int main()
    3 {
    4     printf("abcn");//abcnPress any key to continue......
    5     return 0;
    6 }

When a \ is added to b and n (line breaks when printed)

    1 int main()
    2 {
    3     printf("abc\n");//abc
    4     system("pause");
    5     return 0;
    6 }
  • Example 2 (there is a problem with the printed □ here, which should have been → but I didn't find the reason. I hope to see the little partner in this place know something and leave a message for guidance.), Because when \ 32 is converted to octal, it is 26, which is a "→".
    1 #include <stdio.h>
    2 int main()
    3 {
    4     printf("c:\test\32\test.c");
    5     //\t -- Horizontal tab
    6     return 0;
    7 }

I have to mention the escape character here. As the name suggests, escape character is to change its meaning, change its original meaning or become its original meaning.
Let's look at some escape characters

"dd means two hexadecimal digits. For example: \ x30"

Application of escape characters:

	• Application 1
    1 int main()
    2 {
    3     printf("c:\\test\\32\\test.c");
    4     return 0;
    5 }
	• Application 2
	Print out a
    1 int main()
    2 {
    3     printf("%c\n", 'a');
    4     return 0;
    5 }
	//However, an error will be reported when a is replaced with 'because it will form a pair with the previous single quotation mark, so it is impossible to print the single quotation mark'. At this time, this problem can be solved by using the escape character "\ '"
    1 int main()
    2 {
    3     printf("%c\n", '\'');
    4     return 0;
    5 }

The same applies to double quotes.
A written test question: what should the length of the string output by the following code be—— 13 (about the escape character of "\ ddd")

    1 #include <stdio.h>
    2 int main()
    3 {
    4     printf("%d\n", strlen("c:\test\32\test.c"));
    5     //\32 -- 32 is an octal number, in which \ 382 cannot be put in, because there is no 8 in octal
    6     //32 as the decimal number represented by octal, as the ASCII code value, the corresponding character
    7     //32 is converted to hexadecimal: 26, and then 26 is used as the character represented by ASCII code value, that is, \ 32 is used as an escape character to calculate a length
    8     return 0;
    9 }

The same applies to \ xdd, such as \ x61: convert to decimal 97, and the corresponding character in ASCII table is a.
Question: the output of the following two codes is the same as a. what is the difference between them?

	• Code 1
    1 int main()
    2 {
    3     printf("\x61\n");
    4     return 0;
    5 }
	• Code 2
    1 int main()
    2 {
    3     printf("%c\n", '\x61');
    4     return 0;
    5 }

3. Notes

  1. Unnecessary codes in the code can be deleted directly or commented out
  2. Some codes in the code are difficult to understand. You can add notes
    • C language style notes / xxxxxx/
    Defect: comments cannot be nested (i.e. end at * /)
    • C + + style comments / / xxxxxxxx
    You can annotate one line or multiple lines

Added by RHolm on Thu, 17 Feb 2022 11:32:55 +0200