请教一道基础C语言,帮我看一下, 有点不理解

c_lusir 2013-01-15 06:13:56
#include<stdio.h>
void main()
{
char s[]="012xy\08s34f4w2";
int i,n=0;
for(i=0;s[i]!=0;i++)
if(s[i]>='0'&&s[i]<='9')n++;
printf("%d\n",n);
}
为什么结果是3 ?
char s[]="012xy\08s34f4w2";
定义的字符串这是个什么一个意思
...全文
371 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-01-17
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 VC调试时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 (Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
疯狂的红豆 2013-01-16
  • 打赏
  • 举报
回复
\0 这种字符怎么能出现在字符串的中间呢?会引起判断错误或是截断的。
赵4老师 2013-01-16
  • 打赏
  • 举报
回复
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 "\01" "23" \01,2,3,\0 "\0123" \012,3,\0 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.
常如意 2013-01-16
  • 打赏
  • 举报
回复
C语言中字符串是以\0为结束的 遇到\0后 以后的就不在输出
lm_whales 2013-01-16
  • 打赏
  • 举报
回复
#include<stdio.h> void main() { char s[]="012xy\08s34f4w2"; int i,n=0; for(i=0;s[i]!=0;i++) if(s[i]>='0'&&s[i]<='9')n++; for(i++;s[i]!=0;i++) if(s[i]>='0'&&s[i]<='9')n++; printf("%d\n",n); }
lm_whales 2013-01-16
  • 打赏
  • 举报
回复
#include<stdio.h> void main() { char s[]="012xy\08s34f4w2"; int i,n=0; for(i=0;s[i]!=0;i++) if(s[i]>='0'&&s[i]<='9')n++; for(i=0;s[i]!=0;i++) if(s[i]>='0'&&s[i]<='9')n++; printf("%d\n",n); }
xxb249 2013-01-16
  • 打赏
  • 举报
回复
C语言中字符串是以\0为结束的 遇到\0后 以后的就不在输出
DyanWang 2013-01-16
  • 打赏
  • 举报
回复
s[i]!=0 //s[i]!='\0' not s[i]!='0' s[i]!=0 =>3 s[i]!='0' =>0
guotianyu2000 2013-01-16
  • 打赏
  • 举报
回复
引用 楼主 c_lusir 的回复:
#include<stdio.h> void main() { char s[]="012xy\08s34f4w2"; int i,n=0; for(i=0;s[i]!=0;i++) if(s[i]>='0'&&s[i]<='9')n++; printf("%d\n",n); } 为什么结果是3 ? char s[]="012xy\……
因为你的判定条件就是到'\0'结束的而字符串内部又含有该字符,所以就是3了
#include<stdio.h>

void main()
{
	char s[] = "012xy\08s34f4w2";
	int i, n=0;
	for(i=0; s[i] != 0/*此条件等价于s[i] != '\0'*/; i++)
		if(s[i]>='0' && s[i]<='9')
			n++;
	printf("%d\n",n);
}
永远的霸者 2013-01-16
  • 打赏
  • 举报
回复
引用 楼主 c_lusir 的回复:
#include<stdio.h> void main() { char s[]="012xy\08s34f4w2"; int i,n=0; for(i=0;s[i]!=0;i++) if(s[i]>='0'&&s[i]<='9')n++; printf("%d\n",n); } 为什么结果是3 ? char s[]="012xy\08s34f4w2"; ……
+1
「已注销」 2013-01-15
  • 打赏
  • 举报
回复
因为字符串前面012 是在s[i]>='0'&&s[i]<='9'于条件之内 n加了三次。
JackyRao 2013-01-15
  • 打赏
  • 举报
回复
\0表示一个字符串的结束 012xy 只有3个数字,所以结果是 3
AnYidan 2013-01-15
  • 打赏
  • 举报
回复
引用 2 楼 c_lusir 的回复:
明白了, 那char s【5】=char s[]="012xy\08s34f4w2";吗
编译错误
c_lusir 2013-01-15
  • 打赏
  • 举报
回复
明白了, 那char s【5】=char s[]="012xy\08s34f4w2";吗
氰客 2013-01-15
  • 打赏
  • 举报
回复
\0表示一个字符串的结束 012xy 只有3个数字,所以结果是 3

69,381

社区成员

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

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