30th Lesson in C-language strings

Source: Internet
Author: User

1. The concept of strings

(1) A string is a collection of ordered characters

(2) A string is one of the basic elements in a program

(3) C does not have the concept of a string, but rather a special character array to emulate the string, which is the character array ending with ' /'.

2. character arrays and strings

(1) in the C language, single or multiple characters quoted by double quotation marks are a special literal, stored in the program's global read-only store , which is essentially an array of characters , and the compiler automatically adds the ' + ' character at the end.

"Instance parsing" character array and string

#include <stdio.h>intMain () {CharCa[] = {'H','e','L','L','0'};//stack, character array, note No.    CharSa[] = {'W','o','R','L','D',' /'};//Stack        CharSs[] ="Hello world!";//stack. Semantically, use "Hello world!" This//character Array to initialize the SS array, the compiler will directly//with each element of this character array, go to initialize the stack//the elements in the SS array. As an optimization, "Hello world!"//This string does not have to be saved in the global read-only area,//and only appear on the stack. So ss[0] = ' h '//this statement is legal.         Char* str ="Hello world!";//Global read-only storage, str[0]= ' h ' is illegalprintf ("%s\n", CA);//outputs the contents of hello and later until it is encountered in memoryprintf"%s\n", SA);//Output Hello world!printf"%s\n", SS);//Output Hello world!printf"%s\n", str);//Output Hello world!ss[0] ='h';//Stack, legalprintf"%s\n", SS);//Output Hello world! //str[0] = ' h ';//Global read-only storage, illegalprintf"%s\n", str);//Output Hello world!     return 0;}

3. Little-known secrets

(1) The essence of a string literal is an array , such as "Hello world! " is an unnamed array of characters

(2) string literals can be treated as constant pointers

(3) character in string literal cannot be changed

(4) The string literal contains at least one character , which is ' + '

The essence of the "instance analysis" string literal

#include <stdio.h>intMain () {//essentially a string literal is a character array, shaped like "Hello world!" is a//an array of nameless characters.     Charb ="ABC"[0];//valid, pointing to the No. 0 element of a character array, a    CharC = * ("123"+1);//Legal, remove the 2nd element of the character array, i.e. 2    Chart = *"";//string literal at least one of the packets, ie t= 'printf ("%c\n", b);//' A 'printf"%c\n", c);//' 2 'printf"%d\n", t);//0printf ("%s\n","Hello"); printf ("%p\n"," World");//address of the output string literal (in the global Zone)        return 0;}

4. Length of the string

(1) The length of the string is the number of characters contained in the string

(2) The length of the string refers to the number of characters that appear before the first ' + ' character, and the length of the string is determined by the '% ' terminator.

(3) The function strlen is used to return the length of the string (without '% ').

The use of the "programming Experiment" strlen

5. Summary

(1) simulating strings in C language by character array

(2) The string in C is used as a Terminator

(3) The nature of the string literal is a character array

(4) string-related functions are dependent on the Terminator ' \ '.

30th Lesson in C-language strings

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.