The number of characters in a string mixed statistics

Because Chinese characters occupy more than one byte, how the statistics have a character string of letters both do?

ASCII characters in the computer is a negative number other ordinary ASCII characters distinguishable.

#include<stdio.h>
int main()
{
    char buf [ 256 ] = " Hello World " ;
    printf("%d\n",buf[0]); //-60
    return 0; 
}

So you want to count the number of characters in a mixed string, you should use this feature.

#include<stdio.h>

int main ()
{
    char buf[256] = "你好abc";
    int iLen = 0;
    int bufSize = 0;
    while(buf[iLen])
    {
        if (buf [to] < 0 )
        {
             with ++ ;
            // continue; 
        }
        with ++ ;
         bufSize++;
    }
    printf("%d",bufSize);
}

Output: 5

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11030212.html