C language String

Source: Internet
Author: User

There is a terminator at the end of the string in C, which takes up a position, so when you represent a string in a character array, its actual length is declared length-1, so be aware that the array is out of bounds during use.
1#include <stdio.h>2#include <string.h>3 4 intMain ()5 {6      Charname[8] ="Itcasasa";//error C2117: ' Itcasasa ': array bounds overflow7      CharName2[] = {'o','k'};8printf"%s\n", name);9 Ten     return 0; One}
Using strlen to calculate the length of a string is his actual length, that is, the length after which it is removed.
#include <stdio.h><string.h>int  main () {    char'  123\0";    printf ("%d\n", strlen (str));        // Output 3    return 0 ;}
Be sure to add a terminator to the string when using a character array, or you will get unexpected results. The storage of strings in memory is stored sequentially and is addressed from the big end, and the output of the string is stopped after the Terminator is encountered.

C language String

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.