C Advanced Cultivation Notes - Cultivation Notes 30: Strings in C Language

--The difficulty of things is far less than the fear of things!  

    In this article, let's talk about strings in C language. First, let's take a look at what strings are:

        - A string is a collection of ordered characters

        - Strings are one of the basic elements in a program

- There is no concept of strings in         C language

            -In C language, strings are simulated by special character arrays

            - A string in C language is an array of characters terminated by '\0'

    Character arrays and strings:

        -In the C language, a single or multiple characters quoted in double quotes are a special literal

            - Stored in the program's global read-only memory area

            -The essence is a character array, the compiler automatically adds the  '\0 ' character at the end

    With the above concepts, let's take a look at the definitions of strings in the code below, and see if the output is not as you think.

#include <stdio.h>

intmain()
{
    char ca[] = {'H', 'e', 'l', 'l', 'o'};
    char sa [] = {'W', 'o', 'r', 'l', 'd', '\ 0'};
    char ss[] = "Hello world!";
    char* str = "Hello world!";
    
    printf("ca = %s\n", ca);
    printf("sa = %s\n", sa);
    printf("ss = %s\n", ss);
    printf("str = %s\n", str);
    
    return 0;
}

The execution output is


From the output results, the first output is garbled, and the other three outputs are strings, which is easy to understand, because the ca array does not end with '\0', which does not satisfy the concept of strings, so ca is just a very common array of characters.

    Let's take a look at an interesting question: what does the string literal "Hello" represent?

        - "Hello" is an array of unnamed characters

    Look at a piece of code and think what will be the result of execution?

#include <stdio.h>

intmain()
{
    char b = "abc"[0];
    char c = *("123" + 1);
    char t = *"";
    
    printf("%c\n", b);
    printf("%c\n", c);
    printf("%d\n", t);
    
    printf("%s\n", "Hello");
    printf("%p\n", "World");
    
    return 0;
}

The compilation and execution results are as follows:


To understand the output above, let's start with some properties of string literals:

- The essence of a     string literal is an array

    - String literals can be treated as constant pointers

    - The characters of a string literal are immutable

    - String literal contains at least one character

With the above characteristics, we know that the literal "Hello" can actually be regarded as a constant pointer, which points to the string "Hello", so "Hello"[1] == 'e', ​​* ("Hello" + 1) = 'e'; Do you understand the above output now?

length of string:

   - The length of a string is the number of characters contained in the string

    - String length refers to the number of characters that appear before the first '\0' character

    - Determine the length of the string by the '\0' terminator

    - The function strlen is used to return the length of the string

Below is a simple example to show

#include <stdio.h>
#include <string.h>

intmain()
{
    char s[] = "Hello\0world";
    int i = 0;
    
    for(i=0; i<sizeof(s)/sizeof(char); i++)
    {
        printf("%c\n", s[i]);
    }
    
    printf("%s\n", s);

    printf( "%d\n", strlen(s) );
    printf( "%d\n", strlen("123") );
 
    
    return 0;
}

The execution output is as follows. It should be noted here that sizeof and strlen are different:



Summarize:

- Simulate strings     through character arrays in C language

    - Strings in C language use '\0' as the terminator

    - String literals are essentially character arrays

    - String related functions all depend on the terminator '\0'

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325663567&siteId=291194637