当前位置:网站首页>C language ---5 initial string, escape character and comment

C language ---5 initial string, escape character and comment

2022-06-10 21:29:00 Try!

1、 character string

“hello\n” This is made up of double quotation marks (double quote) A string of characters is called a string literal (string literal), Or abbreviated string . The end of a string is a “\0” The escape character of , When calculating the length of a string ,“\0” It's the end sign , It doesn't count as string content .

#include <stdio.h>
int main()
{
    
	char arr1[] = "hello";// The size of the array is not written here , The array initializes it based on the contents of the following string 
	return 0;
}

After writing this code , Press on the keyboard F10 Start debugging , Click on the menu bar “ debugging ”–“ window ”–“ monitor ”, You can see the contents of the array :
 Insert picture description here
Modify the code , View the run results :

int main()
{
    
	char arr1[] = "hello";
	char arr2[] = {
    'h','e','l','l','o'};
	printf("%s\n",arr1);
	printf("%s\n", arr2);
	return 0;
}

 Insert picture description here
Because when the program is running , Is for array 1 And arrays 2 Memory space is allocated separately , For arrays 1 Come on , After printing the string hello when , It has an end identifier ; But for arrays 2 Come on , There is no identifier for adding the end manually , So after printing hello after , Will find the end identifier in memory , Stop running whenever you find it , When the end identifier is not found, the contents in the memory will be printed , And we don't know what's in the memory , So there will be garbled code . We “ monitor ” Let's take a look at the debugging process :
 Insert picture description here
Based on this code , Let's see if the two arrays have the same length ?

int main()
{
    
	char arr1[] = "hello";
	char arr2[] = {
     'h','e','l','l','o' };
	printf("%s\n",arr1);
	printf("%s\n", arr2);
	printf("%d\n",strlen(arr1));
	printf("%d\n", strlen(arr2));
	return 0;
}

The operation results are as follows : The length of the first array is 5, The second is a random value . Because in the array 2 in , We don't know ’h’,‘e’,‘l’,‘l’,'o’ What else will follow ,strlen Will keep looking for the end identifier , It won't stop until you find it .

hello
hello Hot, hot, hot, hot, hot ?p.
5
34

take char arr2[] = {'h','e','l','l','o'}; Change it to char arr2[] = {'h','e','l','l','o','\0'};, Add the end identifier manually , Look at the results , And monitor :
 Insert picture description here
 Insert picture description here

in addition , I have encountered such problems in my study , When monitoring, it will show that the memory cannot be read , Not very good at solving , I hope you can help me ~
 Insert picture description here
 Insert picture description here

2、 Escape character

\? Use... When writing multiple consecutive question marks , Prevent them from being parsed into three letter words
\ ’ Used to represent character constants ’
\" Double quotation marks used to represent the inside of a string
\ Used to indicate a backslash , Prevent it from being interpreted as an escape sequence character
\a Warning characters , Beep
\b Back space
\f Paper in
\n Line break
\r enter
\t Horizontal tabs
\v Vertical tabs
\dddddd Express 1`3 Eight octal digits , Such as \130 Express X
\xdddd Express 2 Hexadecimal numbers , Such as \x30 Express 0
int main()
{
    
	printf("%c\n",'\130');
	printf("%c\n", '\101');
	printf("%c\n", '\x30');
	return 0;
}

Running results :

X
A
0

Octal 130 Expressed as decimal is :1x82+3x81+0x80=88, Capitalization X Of ASCII The code value is 88, So the print result is X. octal 101 The corresponding decimal system is 65,65 Corresponding ASCII The code value is A, So the print result is A.
x30 Expressed as decimal is :3x161=48,48 Corresponding ASCII The code value is a character 0.
practice :
Think about the output of the program ?

int main()
{
    
	printf("%d\n",strlen("abcdefg"));
	printf("%d\n", strlen("c:\test\328\test.c"));
	return 0;
}

The result is 7 and 14. The second reason is 14 Well , because
 Insert picture description here
Be careful :\328 here \32 Is part of ,8 Is part of , Because there is no three digit octal 8(0-7).

3、 notes

Shortcut key :ctrl+c+k,ctrl+k+u

  • There are unnecessary places in the code that can be deleted directly , You can also comment out
  • Some parts of the code are difficult to understand , You can add the annotation text
原网站

版权声明
本文为[Try!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101730495620.html