String, Character

Source: Internet
Author: User
Tags first string

String, Character

 

String: a char array ending with an empty character \ 0

How to define a string in a program

1. string constant (string text): refers to any character in a pair of double quotation marks. The characters in double quotation marks plus the ending sign \ 0 characters automatically provided by the compiler are stored in memory as a string. It is often used as a parameter for printf and puts.

2. String Array: when defining a string array, the compiler must know how much space it needs. One way is to specify an array that is large enough to accommodate strings. In general, it is more convenient for the compiler to determine the array size. (If the array size is omitted during initialization declaration, the size is determined by the compiler ).

Common Methods for defining strings

① # Define MSG "this is a string"

 

② Char msg [50] = "this is a string"; char msg [] = "this is a string ";

Const char msg [50] = "this is a string"; const char msg [] = "this is a string ";

 

③ Char * msg = "this is a string"; const char * msg = "this is a string ";

 

④ Char * msg [3] = {"this is the first string", "this is the second string", "this is the third string "};

Const char * msg [3] = {"this is the first string", "this is the second string", "this is the third string "};

 

A String constant belongs to the static storage class. Even if a String constant is used for multiple times, the string is only stored in one copy throughout the entire running process of the program. The content in the quotation marks serves as a pointer to the location where the string is stored. This is similar to using an array name as a pointer to an array storage location, that is, using a string as a pointer.

For example, for the string "We are the champion ",

If % s is used in printf, all contents in quotation marks are output, that is, "We are the champion ".

If % p is output, the first character of the output string is the address of 'W.

If * "We are the champion" is output in % c, the first character in the output string "We are the champion" is W. Because "We are the champion" is the address, which is equal to the address of the first character (W) of the string, * "We are the champion" is the value in the address, that is, W.

 

Compare char * msg and char msg []

In the array form, msg is an address constant. You cannot change the msg because it means changing the location (address) of the array storage ).

Msg + 1 can be used to represent the next element in the array (= msg [1]), but the usage of ++ msg or msg ++ is not allowed, because the incremental operator can only be used before the variable name, And here msg makes an address constant.

In pointer form, msg points to the first character of the string, and its value can be changed. Therefore, you can use the incremental operator for it. For example, ++ msg points to the second character.

In short, array initialization refers to copying the string to the array from the static storage area, while pointer initialization only assigns the address of the string (= the address of the first character of the string ).

 

Compare char * msg [SIZE] and char (* msg) [SIZE]

The priority of [] in the expression is higher than *, so whether or not parentheses are very different.

 

Char * msg [SIZE] indicates that msg is an array containing SIZE elements, and each element in the array is a pointer to the char value. This form can be used to declare a string array.

The msg array stores the string address, but the string actually exists in the memory where the program is used to store constants. You can regard msg [0] as a pointer to the start of the first string. * msg [0] indicates the first character of the first string. Due to the relationship between the array symbol and the pointer, it is equivalent to msg [0] [0], although msg is not defined as a two-dimensional array

 

Char (* msg) [SIZE] indicates that msg is a pointer, which points to an array containing SIZE char values. This form can be used to declare pointer variables pointing to two-dimensional arrays.

 

String Input

To read a string to a program, you must first reserve space for storing the string and then use the input function to obtain the string.

How to Create a bucket

1. To use the char array, You need to allocate a large enough storage area to store the strings you want to read. You need to specify the array size in the Declaration. For example, char msg [50];

2. functions used to allocate buckets in the C library. For example, char * ptd; ptd = (char *) malloc (SIZE * sizeof (char); this code requests the space of SIZE char values, and point ptd to the location of the space.

Input Function

The gets () function reads all the characters before the line break (excluding the line break), adds an empty character \ 0 after these characters, and then delivers the string to the program that calls it. It uses an address as a parameter, which can be an array name or an initialized (allocated memory) pointer. If the end of the file is reached, gets returns a NULL pointer.

Note: gets does not check whether the target array can accommodate the input! Common Format: char name [50]; gets (name );

 

Fgets () function, which requires you to specify the maximum number of input characters and the file to read (the keyboard is stdin ). If the value of this parameter is n, fgets will read a maximum of n-1 characters or end the input after reading a line break, which is the most satisfying of the two. It reads the linefeed and does not discard it like gets, but stores it in a string. Common Format: char name [50]; fgets (name, 50, stdin );

 

The scanf () function is more based on obtaining words than strings. It is read in % s format and starts with the first non-white space character, read (but does not include) The next white space character. If the field width is specified, for example, % 10 s, it will read 10 characters or until the first white space character is encountered. Only the one that meets the first of the two is entered. Common Format: char name [50]; scanf ("% s", name );

 

Output Function

The parameter of the puts () function is the string address. It automatically adds a line break after the string when it is displayed.

Common formats: puts (name); puts ("this is a string! ");

 

Fputs () function, which is a file-oriented version of puts. The second parameter is required to indicate the file to be written, and fputs does not automatically add line breaks to the output.

Common Format: fputs (name, stdout );

 

Printf () function, which is not as convenient as puts, but can format multiple data types, so it is more common. It makes it easier to output multiple strings on one line.

Common formats: printf ("Well, % s \ n", name, MSG );

 

Related Article

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.