Getting to Know C Language (Second Lesson)

1. String

The format characters and escape characters involved here (mentioned later in this chapter, currently only need to understand the string)

1.%c    2.%s    3.\0    4.\n

1. Definition of string: A string of characters enclosed by double quotation marks ("") is called a string literal value , abbreviated as a string (it can also be composed of multiple single characters). Note: The end sign of the string is a "\0", that is, the current program ends when \0 is encountered

2. How to use strings:

Storing or printing strings is generally implemented with arrays (mentioned later), and printf is a printing function used to print data

(1) Indicates a single character : use single quotation marks ('') to define directly. Such as: char a='x';     defines a character variable a and assigns it a value of X

char a = 'X';
printf("%c\n",a);//注意格式

(2 ) Define a one-dimensional array string

The first type : enclosed in double quotes

char arr1[]="abcd";
printf("%s\n", arr1);

Print result:

1) A character (char) one-dimensional array (arr1) is defined, and the data stored in the array is: abcd ;

2) Print operation : Since the string is represented by double quotes , \0 is hidden at the end of the string; when printing a string, \0 will not be printed , that is, only \ will be printed The character preceding the 0 .

3 ) Operation of calculating the length of the string: When performing the operation of calculating the length of the string, \0 will not be included in the length , it is only used as an end mark. That is to say, when the calculation reaches \0, the calculation stops, and the length is the length before \0 .

The second type : composed of multiple single characters

1) Wrong way of writing:

char arr2[] = {'a','b','c','d'};
printf("%s\n", arr2);

Print result:

Printing operation : Why are so many texts printed in addition to printing abcd ? The reason is: this way of expressing a string by a single quotation mark does not have a \0 at the end , which means that the end signal cannot be found, and the program cannot be realized according to the expected result.

Calculate the string length operation : because there is no \0, the result of calculating the length is a random value .

2) Correct way of writing:

char arr3[] = { 'a','b','c','d','\0'};
printf("%s\n",arr3);

Print result:

 

 Print operation : the result printed here is only abcd, because we actively added \0; so the program ends when it prints to \0

 Operation of calculating the length of a string : when \0 is encountered, the calculation ends, and \0 will not be included in the length .

3. Problems to avoid:

1) Pay attention to the character type (char) when defining

2) Pay attention to the usage format of single quotes and double quotes

3) Pay attention to the print format of individual characters and strings

Full code:

#include<stdio.h>
int main()
{
	char a = 'X';//单个字符的打印
	char arr1[] = "abcd";//第一种存储字符串的方式
	char arr2[] = {'a','b','c','d'};//第二种
	char arr3[] = { 'a','b','c','d','\0'};//第二种(注意与arr2的区别)
	printf("%c\n",a);//注意格式
	printf("%s\n", arr1);
	printf("%s\n", arr2);
	printf("%s\n",arr3);
	return 0;
}

Two, escape characters

1. Definition of escape character: a special form of character constant; speaking Kun language means: change the original meaning. The representation is: start with a slash (\)

Note that \, the shape can be recorded as upper left and lower right, which must be distinguished from / (division sign)

The following are some escape characters and their general meanings, we will introduce them one by one below (some are not commonly used or even not tested, we will not explain, add * to be more common)

2. Format characters

Definition: The output form in the printf function.

(1) %d: Print the integer in decimal form. Example: If the value of variable a is 250, the output result is 250

General representation: printf("%d",a); where %d represents the format of the output, and a represents the number of output

(2) %s: print string

(3) %c: print a single character

3. Escape characters

 *(1)\nline    break

Function: Move the current cursor position to the beginning of the next line (not easy to describe in words, just go to the picture above)

Without \n:

 Added \n:

 

Without \n:

Added\n:

Summary: If you do not add \n, the printed data will all be on one line. \n can change the content after it to the next line.

 *(2)\0   End sign

There is \0 at the end of the string under the double quotes, or you can add it at any position. You need to add it yourself under single quotes.

Function: As can be seen from the above, the escape character appears after the string as an end mark. When calculating or printing the string, encounter \0 as the end mark, and no longer calculate and print the content after \0.

It is mentioned in detail above the string, so I won't explain too much here.

 (3) \\    A backslash

Output result: \

Purpose: To prevent the \ that you want to print out from forming an escape character with other symbols and transforming into other meanings.

 (4)\?\?)    three-letter word

Role: placed in ??) is escaped as ]

An older version of the compiler exists

 (5)\,    a comma

The output result:,

 (6)\"    A semicolon

Output result: "

 (7)\    aWarning

Function: The computer will whistle once when the program is running.

 (8) \b   backspace character

Function: When printing the result, overwrite the character before \b

Normal output:

 Abnormal output:

 Summary: When outputting characters, the characters before \b will be directly overwritten, while the output text will be omitted directly, with a small space in between.

 (9)\tHorizontal    tab character

Function: The result is the same as four spaces (it is the same as the Tab key on the keyboard , jumping four spaces).

*(10)\ddd    is the ASCII character corresponding to the octal code

Output result: character

ddd represents three octal digits

For example; \130        converts 130 in octal to 88 in decimal,    and then finds the corresponding character through the ASCII character table and outputs it. The character corresponding to 88 is X, so \130 will output X.

#include<stdio.h>
int main()
{
	printf("%c",'\130');
	return 0;
}

Note: ddd can only be composed of numbers 0-7; if the converted decimal number is not within the range of the ASLII character table, it will be invalid

 (11)\xdd The ASLII character corresponding to the hexadecimal code

Output result: character

x means hexadecimal, dd means two hexadecimal digits

For example: \x30 30 is the expression form of hexadecimal, converted to decimal number is 48, the corresponding character in the ASLII character table is 0;

So the output is 0.


#include<stdio.h>
int main()
{
	printf("%c",'\x30');
	return 0;
}

Note: dd can only be a number from 0-15, and the number converted to decimal is not in the range of ASLII characters and is also invalid

3. Notes

Function: used to explain the code or to prevent the explanation from causing the program to run incorrectly

Two Annotation Methods

1.c language style: /* xxxxxx */ , xxxxxx is the commented out content

2.c++ style: // xxxxxxxx, xxxxxxxx are commented out content

The content in the black frame in the figure is the content that has been commented out, and will no longer participate in the code operation

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/131633091