In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "C language string example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "C language string example analysis" it!
Take a look at the following code that we are familiar with and the first to learn
"Hello world!"
This pile of letters is the literal value of the string, the abbreviated string, each letter is a character, the string needs to be enclosed in "" double quotes, and the characters need to be enclosed in''single quotation marks, like the following
"Hello world!" / / string
'a' / / character
'!' / / character
A string is also a constant, and the above three items are all literal constants. This is an example given before when we talked about constants.
So what's the use of strings?
Suppose we want to save the following string, how do we need to save it?
"abcdef"
We can put it in a string array in two forms:
# includeint main () {char arr1 [10] = "abcdef"; / / form-char arr2 [10] = {'a 'axiomagy,' arr2']; / / form 2 / / Let's print these strings printf ("% s\ n", arr1); printf ("% s\ n", arr2); return 0;}
The result of the operation:
Abcdef
Abcdef
Store all "abcdef" in the array arr [], and you can save as many characters as you have in [].
Arr [10] can save 10 characters, but it must be no less than the characters you want to save.
You can also not write the number in [], it will adjust itself according to the string you want to save, like this.
# includeint main () {char arr [] = "abcdef"; / / char- character type, no string type oh return 0;}
So what's the difference between the two forms?
We don't write the numbers in arr []. Let's take a look.
# includeint main () {char arr1 [] = "abcdef"; char arr2 [] = {'axiaqiang [] = "return"; / / Let's print these strings printf ("% s\ n", arr1); printf ("% s\ n", arr2); return 0;}
The result of the operation:
Abcdef
Abcdef ironing abcdef
Hey, have you found that they are different, so why did this happen?
Think of a question first, on what basis does it stop printing?
(Ah, you ask: there's nothing behind them. How do I know what they stopped on?)
It's true that there's no sign there, but that sign is invisible. The end of the string is\ 0, which is an escape character that we'll learn soon.
Char arr1 [] = "abcdef"
Once in this form, the string is followed by a\ 0 by default.
Char arr2 [] = {'axiaqingjinyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongzhuangzhuangzhuangyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyangzhongyuanzhongyuanzhongyangzhongyuanzhongyuanzhongzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhuangzhongyuanshou (char arr2)
There is no\ 0 after this form 2.
You see, when printf goes to print, it will not end until it is recognized to\ 0.
The end of the last character of the first form is recognized to\ 0, and then ends.
The second form was identified to the final f, but it was not recognized until\ 0, so it was printed all the time, and finally the result just appeared.
So what if we add a\ 0 to the second form as the closing mark? don't just think about it, let's try it.
# includeint main () {char arr1 [] = "abcdef\ 0"; / / Let's also try to add a\ 0char arr2 [] = {'a's\ n ", arr2) to it; / / Let's print these strings printf ("% s\ n ", arr1); / /% s is the printf of the printed string ("% s\ n ", arr2). / / s is the return 0;} of the printed string
The result of the operation:
Abcdef
Abcdef
As a result, it worked normally.
So does\ 0 count as part of the length of the string?
Let's introduce another good thing, strlen, which can calculate a function of string length, but it is still the same as before. You need to say hello to another header file, string.h, before you can use it. Let's practice it:
# include#includeint main () {char a [] = "abc"; char b [] = "abc\ 0"; printf ("% d", strlen (a)); printf ("% d", strlen (b)); return 0;}
Running result:
three
three
"abc"
The length of this string is 3, so we can know that the invisible\ 0 is not included in the length, even if you add\ 0 after it.
You can try to calculate the length of the string without adding\ 0 in the second form, be curious and try it yourself.
In this way, we have stored the array "abcdef". What if we use the characters in it? We need to know one more thing:
To call the characters in arr, you can call c with arr [2] and a with arr [0]. Their corresponding relationship is:
A b c d e f
0 1 2 3 4 5
Let's experiment.
# includeint main () {char arr [] = "abcdef"; printf ("% c", arr [2]); return 0;}
Running result:
A
# includeint main () {char arr [] = "abcdef"; printf ("% c", arr [0]); return 0;}
Running result:
A
At this point, I believe that everyone has a deeper understanding of the "C language string example analysis", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.