strlen関数
コード #include <iostream> #include <cstring> using namespace std; void func1(); void func2(); void main() { //10進数でasciiコードを出力 func1(); func2(); return; } void func1(){ printf("func1 START\n"); char s[10] = "aあ iい"; printf("s = %s//\n", s); printf("strlen(s) = %d\n", strlen(s)); int i; for(i = 0; i < 10; i++) { printf("s[%d] = %d\n", i, (int)s[i]); } printf("func1 END\n\n"); return; } void func2(){ /* 英数半角の10進数でのasciiコードは0〜127 半角スペースは32 */ printf("func2 START\n"); char s = '\0'; printf("s = %d\n", s); s = '~'; printf("s = %d\n", s); s = ' '; printf("s = %d\n", s); printf("func2 END\n\n"); return; }
結果 func1 START s = aあ iい// strlen(s) = 7 s[0] = 97 s[1] = -126 s[2] = -96 s[3] = 32 s[4] = 105 s[5] = -126 s[6] = -94 s[7] = 0 s[8] = 0 s[9] = 0 func1 END func2 START s = 0 s = 0 s = 126 s = 126 s = 32 s = 32 func2 END