C语言字符串长度

99-107 2016-03-16 08:57:57

#include<stdio.h>
void main()
{
int length;
char a[] = "\\\065a,\n";
length = strlen(a);
printf("%d\n",length);
}

如上:a 的长度为啥是5?
还有一个问题,C中是不是没有字符串这个类型。所谓的字符串末尾有个’\0‘是指的C++?
...全文
1498 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-03-17
  • 打赏
  • 举报
回复
C++ Character Constants Character constants are one or more members of the “source character set,” the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the “execution character set,” the character set on the machine where the program executes. Microsoft Specific For Microsoft C++, the source and execution character sets are both ASCII. END Microsoft Specific There are three kinds of character constants: Normal character constants Multicharacter constants Wide-character constants Note Use wide-character constants in place of multicharacter constants to ensure portability. Character constants are specified as one or more characters enclosed in single quotation marks. For example: char ch = 'x'; // Specify normal character constant. int mbch = 'ab'; // Specify system-dependent // multicharacter constant. wchar_t wcch = L'ab'; // Specify wide-character constant. Note that mbch is of type int. If it were declared as type char, the second byte would not be retained. A multicharacter constant has four meaningful characters; specifying more than four generates an error message. Syntax character-constant : 'c-char-sequence' L'c-char-sequence' c-char-sequence : c-char c-char-sequence c-char c-char : any member of the source character set except the single quotation mark ('), backslash (\), or newline character escape-sequence escape-sequence : simple-escape-sequence octal-escape-sequence hexadecimal-escape-sequence simple-escape-sequence : one of \' \" \? \\ \a \b \f \n \r \t \v octal-escape-sequence : \octal-digit \octal-digit octal-digit \octal-digit octal-digit octal-digit hexadecimal-escape-sequence : \xhexadecimal-digit hexadecimal-escape-sequence hexadecimal-digit Microsoft C++ supports normal, multicharacter, and wide-character constants. Use wide-character constants to specify members of the extended execution character set (for example, to support an international application). Normal character constants have type char, multicharacter constants have type int, and wide-character constants have type wchar_t. (The type wchar_t is defined in the standard include files STDDEF.H, STDLIB.H, and STRING.H. The wide-character functions, however, are prototyped only in STDLIB.H.) The only difference in specification between normal and wide-character constants is that wide-character constants are preceded by the letter L. For example: char schar = 'x'; // Normal character constant wchar_t wchar = L'\x81\x19'; // Wide-character constant Table 1.2 shows reserved or nongraphic characters that are system dependent or not allowed within character constants. These characters should be represented with escape sequences. Table 1.2 C++ Reserved or Nongraphic Characters Character ASCII Representation ASCII Value Escape Sequence Newline NL (LF) 10 or 0x0a \n Horizontal tab HT 9 \t Vertical tab VT 11 or 0x0b \v Backspace BS 8 \b Carriage return CR 13 or 0x0d \r Formfeed FF 12 or 0x0c \f Alert BEL 7 \a Backslash \ 92 or 0x5c \\ Question mark ? 63 or 0x3f \? Single quotation mark ' 39 or 0x27 \' Double quotation mark " 34 or 0x22 \" Octal number ooo — \ooo Hexadecimal number hhh — \xhhh Null character NUL 0 \0 If the character following the backslash does not specify a legal escape sequence, the result is implementation defined. In Microsoft C++, the character following the backslash is taken literally, as though the escape were not present, and a level 1 warning (“unrecognized character escape sequence”) is issued. Octal escape sequences, specified in the form \ooo, consist of a backslash and one, two, or three octal characters. Hexadecimal escape sequences, specified in the form \xhhh, consist of the characters \x followed by a sequence of hexadecimal digits. Unlike octal escape constants, there is no limit on the number of hexadecimal digits in an escape sequence. Octal escape sequences are terminated by the first character that is not an octal digit, or when three characters are seen. For example: wchar_t och = L'\076a'; // Sequence terminates at a char ch = '\233'; // Sequence terminates after 3 characters Similarly, hexadecimal escape sequences terminate at the first character that is not a hexadecimal digit. Because hexadecimal digits include the letters a through f (and A through F), make sure the escape sequence terminates at the intended digit. Because the single quotation mark (') encloses character constants, use the escape sequence \' to represent enclosed single quotation marks. The double quotation mark (") can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a character constant, you must type two backslashes in a row (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.)
fefe82 2016-03-17
  • 打赏
  • 举报
回复
引用 7 楼 clarkchenhot 的回复:
[quote=引用 5 楼 fefe82 的回复:] [quote=引用 楼主 clarkchenhot 的回复:]

#include<stdio.h>
void main()
{
     int length;
     char a[] = "\\\065a,\n";
     length = strlen(a);
     printf("%d\n",length);
}
如上:a 的长度为啥是5? 还有一个问题,C中是不是没有字符串这个类型。所谓的字符串末尾有个’\0‘是指的C++?
C 没有,C++ 也没有“字符串”这个类型。(但是 C++ 有 std::string,这个跟 "abcdef" 不是一回事) “字符串末尾有个’\0‘” 同样适用与 C 与 C++ 。这个可以被称作“以零结尾的字符串” "abcdef" 在 C 与 C++ 里都称作 string literal ,类型都是 char[] (字符数组,长度由字符串长度决定,包括最后的 '\0',"abcdef" 的类型是 char[7]),最后都会自动补充一个 '\0' 。 strxxx 系列的函数,会接受一个 (const) char * 输入,并已 '\0' 作为字符串的结束。 以零为结束的字符串内部,不能包含 '\0' 。 std::string 不是以零未结束的字符串。std::string 内部可以包含 '\0' 。[/quote] 您好,有几个问题 1、以0结尾的字符串,‘0’字符是字符0的ASCII码不是00000000,为何不能包括‘\0’。我理解的是字符数组在内存中存的末尾都有一个'\0‘作为结束。 2、我这个字符数组在内存中存储的时候最后是不是有个'\0'。但是strlen()函数不包括’\0‘所占的1B的空间?[/quote] 1. 因为遇到 '\0' 字符串就结束了,其后内容不会被认为是字符串的一部分。所以’\0' 不能出现在以零为止的字符串的内部。 同时 '\0' 本身也不是字符串的一部分(只是一个结束符号)。所以以零为止的字符串实际不能包含 '\0' 。 2. 最后会有一个 '\0'。但是由于它并不是以零为止的字符串的一部分,所以 strlen 得到的长度是不包括这个 '\0' 的。
paschen 2016-03-17
  • 打赏
  • 举报
回复
引用 7 楼 clarkchenhot 的回复:
[quote=引用 5 楼 fefe82 的回复:] [quote=引用 楼主 clarkchenhot 的回复:]

#include<stdio.h>
void main()
{
     int length;
     char a[] = "\\\065a,\n";
     length = strlen(a);
     printf("%d\n",length);
}
如上:a 的长度为啥是5? 还有一个问题,C中是不是没有字符串这个类型。所谓的字符串末尾有个’\0‘是指的C++?
C 没有,C++ 也没有“字符串”这个类型。(但是 C++ 有 std::string,这个跟 "abcdef" 不是一回事) “字符串末尾有个’\0‘” 同样适用与 C 与 C++ 。这个可以被称作“以零结尾的字符串” "abcdef" 在 C 与 C++ 里都称作 string literal ,类型都是 char[] (字符数组,长度由字符串长度决定,包括最后的 '\0',"abcdef" 的类型是 char[7]),最后都会自动补充一个 '\0' 。 strxxx 系列的函数,会接受一个 (const) char * 输入,并已 '\0' 作为字符串的结束。 以零为结束的字符串内部,不能包含 '\0' 。 std::string 不是以零未结束的字符串。std::string 内部可以包含 '\0' 。[/quote] 您好,有几个问题 1、以0结尾的字符串,‘0’字符是字符0的ASCII码不是00000000,为何不能包括‘\0’。我理解的是字符数组在内存中存的末尾都有一个'\0‘作为结束。 2、我这个字符数组在内存中存储的时候最后是不是有个'\0'。但是strlen()函数不包括’\0‘所占的1B的空间?[/quote] 1、‘\0’与'0'是两码事 2、是的,strlen的说明参看:http://en.cppreference.com/w/c/string/byte/strlen
99-107 2016-03-17
  • 打赏
  • 举报
回复
引用 5 楼 fefe82 的回复:
[quote=引用 楼主 clarkchenhot 的回复:]

#include<stdio.h>
void main()
{
     int length;
     char a[] = "\\\065a,\n";
     length = strlen(a);
     printf("%d\n",length);
}
如上:a 的长度为啥是5? 还有一个问题,C中是不是没有字符串这个类型。所谓的字符串末尾有个’\0‘是指的C++?
C 没有,C++ 也没有“字符串”这个类型。(但是 C++ 有 std::string,这个跟 "abcdef" 不是一回事) “字符串末尾有个’\0‘” 同样适用与 C 与 C++ 。这个可以被称作“以零结尾的字符串” "abcdef" 在 C 与 C++ 里都称作 string literal ,类型都是 char[] (字符数组,长度由字符串长度决定,包括最后的 '\0',"abcdef" 的类型是 char[7]),最后都会自动补充一个 '\0' 。 strxxx 系列的函数,会接受一个 (const) char * 输入,并已 '\0' 作为字符串的结束。 以零为结束的字符串内部,不能包含 '\0' 。 std::string 不是以零未结束的字符串。std::string 内部可以包含 '\0' 。[/quote] 您好,有几个问题 1、以0结尾的字符串,‘0’字符是字符0的ASCII码不是00000000,为何不能包括‘\0’。我理解的是字符数组在内存中存的末尾都有一个'\0‘作为结束。 2、我这个字符数组在内存中存储的时候最后是不是有个'\0'。但是strlen()函数不包括’\0‘所占的1B的空间?
paschen 2016-03-17
  • 打赏
  • 举报
回复
黑底字符 \\ 实际只表示 \ ,只算一个字符,同理其他 字符串不能作为一种类型,他只是一个以0结尾的字符数组
fefe82 2016-03-17
  • 打赏
  • 举报
回复
引用 楼主 clarkchenhot 的回复:

#include<stdio.h>
void main()
{
     int length;
     char a[] = "\\\065a,\n";
     length = strlen(a);
     printf("%d\n",length);
}
如上:a 的长度为啥是5? 还有一个问题,C中是不是没有字符串这个类型。所谓的字符串末尾有个’\0‘是指的C++?
C 没有,C++ 也没有“字符串”这个类型。(但是 C++ 有 std::string,这个跟 "abcdef" 不是一回事) “字符串末尾有个’\0‘” 同样适用与 C 与 C++ 。这个可以被称作“以零结尾的字符串” "abcdef" 在 C 与 C++ 里都称作 string literal ,类型都是 char[] (字符数组,长度由字符串长度决定,包括最后的 '\0',"abcdef" 的类型是 char[7]),最后都会自动补充一个 '\0' 。 strxxx 系列的函数,会接受一个 (const) char * 输入,并已 '\0' 作为字符串的结束。 以零为结束的字符串内部,不能包含 '\0' 。 std::string 不是以零未结束的字符串。std::string 内部可以包含 '\0' 。
小灸舞 版主 2016-03-17
  • 打赏
  • 举报
回复
引用 3 楼 clarkchenhot 的回复:
[quote=引用 1 楼 qq423399099 的回复:] 分析下:"\\\065a,\n" 第一个字符\\,第一个\表示转义,第二个\代表该字符 第二个字符\065,八进制表达(有点凑巧啊) 第三个字符a 第四个字符, 第五个字符\n 所以答案是5 如果LZ把"\\\065a,\n"改为"\\\0a65a,\n"使\065不能组成一个特殊转义字符,那么strlen结果将是1
哦,明白了,如果改为"\\\8065a,\n"其中的第三个\就无意义了吧,strlen结果是8,编译器是这么个意思吧?[/quote] 对的
99-107 2016-03-17
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
分析下:"\\\065a,\n" 第一个字符\\,第一个\表示转义,第二个\代表该字符 第二个字符\065,八进制表达(有点凑巧啊) 第三个字符a 第四个字符, 第五个字符\n 所以答案是5 如果LZ把"\\\065a,\n"改为"\\\0a65a,\n"使\065不能组成一个特殊转义字符,那么strlen结果将是1
哦,明白了,如果改为"\\\8065a,\n"其中的第三个\就无意义了吧,strlen结果是8,编译器是这么个意思吧?
starytx 2016-03-17
  • 打赏
  • 举报
回复
C语言中所谓的字符串实际上是字符数组。C风格字符串通常是指以'\0'字符为结尾的字符数组,所以一边表示一个字符串可以给出首字符的地址(指针),然后以遇到结束符为止
小灸舞 版主 2016-03-16
  • 打赏
  • 举报
回复
分析下:"\\\065a,\n"
第一个字符\\,第一个\表示转义,第二个\代表该字符
第二个字符\065,八进制表达(有点凑巧啊)
第三个字符a
第四个字符,
第五个字符\n
所以答案是5
如果LZ把"\\\065a,\n"改为"\\\0a65a,\n"使\065不能组成一个特殊转义字符,那么strlen结果将是1
liehu232 2016-03-16
  • 打赏
  • 举报
回复
printf(”%s”,a)就知道了

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧