C language-06 complex data type-02 string,-06-02

Source: Internet
Author: User

C language-06 complex data type-02 string,-06-02

#include <stdio.h>

int main ()
{
    // char name [] = {'i', 't', 'c', 'H', 's', 't', '\ 0'};
    char name [] = "itcast";
    
    name [3] = 'H';
    
    / *
    int size = sizeof (name);
    
    printf ("% d \ n", size);
    * /
    
    printf ("I'm taking a class at% s \ n", name);
    
    return 0;
}

// an initialization of the string
void test2 ()
{
    // \ 0's ASCII value is 0
    // all strings
    char name [8] = "it";
    char name2 [8] = {'i', 't', '\ 0'};
    char name3 [8] = {'i', 't', 0};
    char name4 [8] = {'i', 't'};
    
    // not a string (only a character array)
    char name5 [] = {'i', 't'};
}

/ *
void test ()
{
    // 'a' 'b' 'A'
    // "jack" == 'j' + 'a' + 'c' + 'k' + '\ 0'
    
    char name [10] = "jack888 \ n";
    
    // Passing the array is just a warning
    printf (name);
    
    printf (name);
    
    printf (name);
    
    printf ("57843578435");
} * /
#include <stdio.h>

/ *
 The role of \ 0
 1. End of string tag
 2.printf ("% s", name2);
 Will output characters from the address name2 until it encounters \ 0
 * /

int main ()
{
    char name [] = "itc \ 0ast";
    
    char name2 [] = {'o', 'k'};
    
    // printf ("% s \ n", name2);
    
    printf ("% s \ n", & name2 [1]);
    
    return 0;
}
/ *
strlen function: Calculate the length of a string
 1. Calculate the number of characters, not the number of words. A Chinese character counts as 3 characters
 2. Calculated characters do not include \ 0
 3. Count the number of characters from an address until it encounters \ 0
* /

// strlen function is declared in string.h file
#include <string.h>
#include <stdio.h>

int main ()
{
    // int size = strlen ("哈 haha");

    // printf ("% d \ n", size);
    / *
    char name [] = "itcast";
    char name2 [] = {'0', '\ 0', '6'};
    
    int size = strlen (name2);
    
    printf ("% d \ n", size);
    * /
    
    char name [] = "itcast";
    
    // printf ("% s \ n", name);
    
    printf ("% c \ n", name [3]);
    
    
    return 0;
}
 String array
#include <stdio.h>
int main ()
{
    char name [] = "jack";
    
    char name1 [] = "rose";
    
    char name2 [] = "jim";
    
    char name3 [] = "jake";
    
    char names [2] [10] = {"jack", "rose"};
    
    // printf ("% s \ n", names [0]);
    
    // printf ("% c \ n", names [0] [3]);
    
    char names2 [2] [10] =
    {
        {'j', 'a', 'c', 'k', '\ 0'},
        {'r', 'o', 's', 't', '\ 0'}
    };
    
    
    return 0;
}
 


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.