In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to use single and double quotes in C language. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Single quotation marks and double quotation marks
Single quotation marks in C language are used to indicate the literal amount of characters.
Double quotation marks in C language are used to indicate the literal amount of strings.
'a 'represents the literal amount of characters, accounting for 1 byte in memory, and' axiom 1 'represents the ASCII code of' a 'plus 1, resulting in' b'.
"a" represents the literal amount of the string, accounting for 2 bytes in memory, and "a" + 1 indicates a pointer operation, which points to the "a" Terminator'\ 0'.
Let's take a look at a code of the nature of single and double quotes:
# include int main () {char* p1 = 1; char* p2 = '1regions; char* p3 = "1"; printf ("% s,% s,% s", p1, p2, p3); printf ('\ n'); printf ("\ n"); return 0;}
There will be a warning after compilation, and a segment error will occur if you continue to run, as follows:
What exactly is the problem? Let's comment out the print statement and run it:
# include int main () {char* p1 = 1; char* p2 = '1regions; char* p3 = "1"; / / printf ("% s,% s,% s", p1, p2, p3); / / printf ('\ n'); / / printf ("\ n"); return 0;}
Although there are warnings in the compilation run, there are no segment errors, so the segment error must be in the print statement.
Uncomment the print statement
# include int main () {char* p1 = 1; char* p2 = '1regions; char* p3 = "1"; printf ("% s,% s,% s", p1, p2, p3); / / printf ('\ n'); / / printf ("\ n"); return 0;}
Sure enough, a segment error occurred after the compilation was run.
What on earth is going on? Keep looking down.
2. Tips
The literal amount of characters is compiled to the corresponding ASCII code
The literal amount of the string is compiled to the corresponding memory address
The first parameter of printf is treated as a string memory address
The low address space of memory cannot be accessed at will in the program.
Third, program example analysis 1
Note: the ASCII code corresponding to the character'1' is the hexadecimal 0x00000031
0x08048000 this memory address is a very special memory address, all memory space below this address can not be accessed casually, once accessed, a segment error will occur. The compiled address of the string in the program is greater than 0x08048000, so it can be accessed in the program.
Therefore, neither the memory address of 1 nor'1' can be accessed, and a segment error will occur when accessed. What happens if you print the following statement with printf ("% s,% s,% s", p1, p2, p3); comment?
# include int main () {char* p1 = 1; char* p2 = '1regions; char* p3 = "1"; / / printf ("% s,% s,% s", p1, p2, p3); printf ('\ n'); printf ("\ n"); return 0;}
If the output is as follows, a segment error still occurs:
IV. Program example analysis 2
The literal amount of the'\ n' character is still in the range of address values, so a segment error occurs when accessing this address.
If you comment out printf ('\ n');
# include int main () {char* p1 = 1; char* p2 = '1regions; char* p3 = "1"; / / printf ("% s,% s,% s", p1, p2, p3); / / printf ('\ n'); printf ("\ n"); return 0;}
The compilation will not go wrong, and there will be a new line:
5. Confusing code
The original meaning of this code is that if the character c is "\ t" or "\ n", enter the character.
# include int main () {char c = ""; while ((c = = "\ t") | | (c = = "\ n") {scanf ("% c", & c);} return 0;}
After compiling and running, we found that the program did not let us enter:
Before you explain, take a look at what happens:
Analysis:
1) the memory address of the compiled string "string" is assigned to the variable c
2) the memory address occupies 4 bytes, while the variable c only takes 1 byte
3) truncation occurs after assignment due to different types
So char c = ""; truncation occurs after assignment, then the statement in while will not be executed, this program confuses the concept of character and string.
You can change it like this:
# include int main () {char c =''; while ((c = ='\ t') | (c = ='\ n') | (c = ='\ n') {scanf ("% c", & c);} return 0;}
So you can get it right:
These are all the contents of the article "how to use single and double quotes in C language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.