"C Language" string module

Source: Internet
Author: User

I. Introduction of Strings

* In Java, a string type can be used to store

"MJ"; 

There is no string of this type in the C language. In fact, the string is a sequence of characters, consisting of multiple characters, so in C, we can use a character array to store strings.

* String can be regarded as a special character array, in order to distinguish from the normal character array, should be added at the end of the string to stop the sign "\" . ' \ s ' is an ASCII character with a value of 0 and is an empty operator that means nothing. Therefore, the character array is used to hold the string, and the assignment should include the end flag ' \ s '.

* The storage of the string "MJ" is as follows (assuming the character array Char a[] to store):

Notice that there is a ' s ' on the tail, if there is no end tag, it means that the character array is not a string

second, the initialization of the string
1Char a[3] = {‘M‘,‘J‘,‘/‘};23Char b[3];4 b[0] =‘M‘;5 b[1] =‘J‘;6 b[2] =‘\0 ; 7  8 char c[3] =  "mj" ; 9 10 char d[] =  "mj "; One 12 char e[20 "=  "mj";         

When we use an initialization method like the 8th line, the system automatically appends the end of the string to the

three, the output of the string

We can use two functions in stdio.h to output strings, namely printf and puts functions.

1.printf function

* This function has been used many times, using the format symbol%s to indicate the need to output a string

Char a[3] = {'m'J' mm '};p rintf ("%s", a);    

Output:, the last side of the zero is impossible to output, it is just a null character, but the end of the string tag.

* Speaking of this , some people may think: so it seems that the last of the elimination also does not affect it, the output should be the same ah, are "MJ".

We can try to remove the last surface, then output:

Char a[3] = {'m'J'};p rintf ("%s", a);        

Output: The result is the same as the output added to the above.

Don't be too happy to be too early, I can only say that you are lucky, a bit lucky.

* Let's look at one more example

1Char a[3] = {‘M‘,‘J‘,‘/‘};//Added Terminator23Char b[] = {‘I‘,‘S "; // Suppose you forget to add the Terminator \04 5 printf ( "", a ); // output string A6 7 printf ( "\n"); // newline 8 9 printf ( " string B:%s", B. ); // output string b         

As you can see , the character array B of line 3rd does not add a terminator, so B is not an authentic string.

According to your guess, the output of string B should be "is", but the output is:, as you can see, when we try to output B, we also output a.

To figure out why, first look at the memory address of A and B:

printf ("A's address:%x", a);p rintf ("\ n");p rintf ("address of B:%x", b);  

Output: From this data we can analyze the memory storage of A and B as follows:

It can be seen that the memory addresses of arrays B and a are contiguous. Let 's go back to the code for Output B:

printf (" string b:%s// output string b   

%s indicates that a string is expected to be output, so the printf function will output the characters sequentially from the first address of B, up to the \ s character, because the. \ is the end tag of the string.

So, if you want to create a string, remember to add the Terminator, or the result is very serious, will access some garbage data.

2.puts function
"MJ";  Puts (a); 4 puts ("LMJ");       

Looking at the 2nd line of code, the puts function starts the output character from the first address of a, up to the \ s character.

Output: You can see that the puts function outputs a string that wraps itself.

* The puts function outputs only one string at a time, and the printf function can output multiple strings at the same time

printf ("%s-%s"MJ"lmj");      
Iv. input of strings

There are 2 functions in stdio.h that can be used to receive user input strings, scanf and gets.

1.scanf function
Char a[];scanf ("%s", a);    

The scanf function will store the user input characters from the first address of a, and after the storage is complete, the system will automatically add an end tag to the tail

Note, do not write scanf ("%s", &a), because a already represents the address of the array, there is no need to add & this address operator.

2.gets function
Char a[];gets (a); 

Get the same as scanf, will start from the first address of a user input characters, after the storage is complete, the system will automatically add an end tag in the tail.

* Gets only one string at a time, scanf can read multiple strings at the same time

* Gets can read into a string containing spaces and tabs until it encounters a carriage return; scanf cannot be used to read spaces, tab

v. Array of strings1. Introduction to String Arrays

* One-dimensional character array holds a string, such as a name char name[20] = "MJ"

* If you want to store multiple strings, such as the names of all students in a class, you need a two-dimensional character array, char names[15][20] can hold 15 students ' names (assuming the name does not exceed 20 characters)

* If you want to store two classes of student names, then you can use a three-dimensional character array Char names[2][15][20]

2. Initialization of string arrays
Char names[2][10] = {{‘J‘,‘A‘,‘Y‘,‘/‘}, {‘J‘,‘I‘,‘M ",  \0 ' 

You can think of a string array as a one-dimensional array whose elements are strings. The string array names is composed of the string "Jay" and the string "Jim".

"C Language" string module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.