First, determine your machine characters occupy a few bytes
#include <stdio.h> int main() { char str[] = "中"; int length, i; length = sizeof(str) / sizeof(str[0]); printf("length of str: %d\n", length); for (i = 0; i < length; i++) { printf("str[%d] = %d\n", i, str[i]); } return 0; }
#include <stdio.h> #define MAX 1024 int main () { char STR [MAX]; char * = target STR; char CH; int length = 0; the printf ( "Enter a character:"); fgets (STR , MAX, stdin); the while (. 1) { CH = * target ++; IF (CH == '\ 0') { BREAK; } IF ((int) CH <0) { target + =. 1; } length ++; } the printf ( "you have entered% d characters",. 1-length); }