对这个字符串求长度是多少

qq_17707171 2015-07-26 10:56:57
char str1[] = "abc\001\///n";
用strlen求长度是多少。。觉得是3,可程序输出是8,怎么回事。。
...全文
384 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
707wk 2015-08-03
  • 打赏
  • 举报
回复
\001 不是 \0 01
knisinf 2015-08-03
  • 打赏
  • 举报
回复
这涉及到转义字符的一些用法,其中转义字符 ‘\’ 带数字的话共有三种情况。
1)直接带一个数字0 ,那么形如“abs\0mn” 中间的‘\0’部分会被解释成字符串结束符。
2)带一个8进制数(表示法:\ddd),这就是楼主遇到的情况,事实上那个001 被看成了8进制数1.
3)带一个16进制数(表示法:\xdd),十六进制数前需要带标识x 。

转义字符带除 0,双引号外的可打印字符的话可以直接表示该字符本身,那就是"abc\001\///n" 中的‘ \/ ’ 只表示'/' .
综上所述,只有8个字符(除结束符外) 。
ohayao_9270 2015-07-31
  • 打赏
  • 举报
回复
int main() { char str1[] = "abc\001\///n"; printf("%d\n", strlen(str1)); for (int i = 0; i < strlen(str1); i++){ printf("[%c] ", str1[i]); } getchar(); }
「已注销」 2015-07-31
  • 打赏
  • 举报
回复
宽字符算长度算的是宽字符的个数,自然是一样的。
赵4老师 2015-07-27
  • 打赏
  • 举报
回复
C++ String Literals A string literal consists of zero or more characters from the source character set surrounded by double quotation marks ("). A string literal represents a sequence of characters that, taken together, form a null-terminated string. Syntax string-literal : "s-char-sequenceopt" L"s-char-sequenceopt" s-char-sequence : s-char s-char-sequence s-char s-char : any member of the source character set except the double quotation mark ("), backslash (\), or newline character escape-sequence C++ strings have these types: Array of char[n], where n is the length of the string (in characters) plus 1 for the terminating '\0' that marks the end of the string Array of wchar_t, for wide-character strings The result of modifying a string constant is undefined. For example: char *szStr = "1234"; szStr[2] = 'A'; // Results undefined Microsoft Specific In some cases, identical string literals can be “pooled” to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. The/Gf compiler option enables string pooling. END Microsoft Specific When specifying string literals, adjacent strings are concatenated. Therefore, this declaration: char szStr[] = "12" "34"; is identical to this declaration: char szStr[] = "1234"; This concatenation of adjacent strings makes it easy to specify long strings across multiple lines: cout << "Four score and seven years " "ago, our forefathers brought forth " "upon this continent a new nation."; In the preceding example, the entire string Four score and seven years ago, our forefathers brought forth upon this continent a new nation. is spliced together. This string can also be specified using line splicing as follows: cout << "Four score and seven years \ ago, our forefathers brought forth \ upon this continent a new nation."; After all adjacent strings in the constant have been concatenated, the NULL character, '\0', is appended to provide an end-of-string marker for C string-handling functions. When the first string contains an escape character, string concatenation can yield surprising results. Consider the following two declarations: char szStr1[] = "\01" "23"; char szStr2[] = "\0123"; Although it is natural to assume that szStr1 and szStr2 contain the same values, the values they actually contain are shown in Figure 1.1. Figure 1.1 Escapes and String Concatenation Microsoft Specific The maximum length of a string literal is approximately 2,048 bytes. This limit applies to strings of type char[] and wchar_t[]. If a string literal consists of parts enclosed in double quotation marks, the preprocessor concatenates the parts into a single string, and for each line concatenated, it adds an extra byte to the total number of bytes. For example, suppose a string consists of 40 lines with 50 characters per line (2,000 characters), and one line with 7 characters, and each line is surrounded by double quotation marks. This adds up to 2,007 bytes plus one byte for the terminating null character, for a total of 2,008 bytes. On concatenation, an extra character is added to the total number of bytes for each of the first 40 lines. This makes a total of 2,048 bytes. (The extra characters are not actually written to the string.) Note, however, that if line continuations (\) are used instead of double quotation marks, the preprocessor does not add an extra character for each line. END Microsoft Specific Determine the size of string objects by counting the number of characters and adding 1 for the terminating '\0' or 2 for type wchar_t. Because the double quotation mark (") encloses strings, use the escape sequence (\") to represent enclosed double quotation marks. The single 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 string, you must type two backslashes (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.) To specify a string of type wide-character (wchar_t[]), precede the opening double quotation mark with the character L. For example: wchar_t wszStr[] = L"1a1g"; All normal escape codes listed in Character Constants are valid in string constants. For example: cout << "First line\nSecond line"; cout << "Error! Take corrective action\a"; Because the escape code terminates at the first character that is not a hexadecimal digit, specification of string constants with embedded hexadecimal escape codes can cause unexpected results. The following example is intended to create a string literal containing ASCII 5, followed by the characters five: \x05five" The actual result is a hexadecimal 5F, which is the ASCII code for an underscore, followed by the characters ive. The following example produces the desired results: "\005five" // Use octal constant. "\x05" "five" // Use string splicing.
lin5161678 2015-07-26
  • 打赏
  • 举报
回复
\001 不是结束字符 如果是 \000 结果就是3
dustpg 2015-07-26
  • 打赏
  • 举报
回复
C风格字符串是以0结尾的, 所以是8
sqz20 2015-07-26
  • 打赏
  • 举报
回复
'a' 'b' 'c' '\001' '\' '/' '/' 'n'
GKatHere 2015-07-26
  • 打赏
  • 举报
回复
呵呵, 这个个有意思了, 为什么宽字节也是三位, 就让人迷惑了

char str1[] = "abc\1\///n";  //  中间不断
char str1[] = "abc\01\///n";  //  中间不断
char str1[] = "abc\001\///n";  //  中间不断
char str1[] = "abc\0001\///n";  //  中间断开

WCHAR str1[] = L"abc\1\///n";  //  中间不断
WCHAR str1[] = Labc\01\///n";  //  中间不断
WCHAR str1[] = L"abc\001\///n";  //  中间不断
WCHAR str1[] = L"abc\0001\///n";  //  中间断开
paschen 2015-07-26
  • 打赏
  • 举报
回复
他其实判断到的是\001,而不是\0然后再字符01 如果想表示\0后面是字符01可以写成"abc\00001\///n"
内容概要:本文围绕基于深度学习分类的时相关MIMO信道递归CSI量化技术展开研究,提出一种结合深度学习模型的递归式信道状态信息(CSI)反馈优化方法。该方法针对无线通信系统中时变MIMO信道的特点,利用深度学习网络对信道时序特征进行有效提取与分类,实现高精度、低开销的CSI量化与反馈,从而提升大规模MIMO系统的频谱效率与传输性能。研究不仅涵盖了算法设计与模型构建,还提供了完整的Matlab代码实现,便于验证与复现,适用于现代高性能无线通信系统的优化需。; 适合人群:具备通信系统理论基础、熟悉MIMO与信道反馈机制,并掌握Matlab编程技能的研究生、科研人员及从事5G/6G通信、智能信号处理与深度学习在通信中应用的工程技术人员。; 使用场景及目标:①研究MIMO系统中基于深度学习的CSI反馈压缩与重建技术;②探索时序信道建模与递归量化机制的深度融合方法;③复现并改进现有算法,支撑高水平学术论文撰写或通信系统原型开发。; 阅读建议:建议读者结合提供的Matlab代码逐模块调试,深入理解深度学习分类网络与时序递归量化策略的协同工作机制,重点关注特征提取、分类决策与量化更新等关键环节的设计逻辑,并可尝试迁移至不同信道模型或引入更先进网络结构以进一步提升性能。

70,038

社区成员

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

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