简体   繁体   English

使用“while”打印字符数组

[英]Printing an array of characters with “while”

So here is the working version: 所以这是工作版本:

#include <stdio.h>

int main(int argc, char const *argv[]) {
    char myText[] = "hello world\n";
    int counter = 0;
    while(myText[counter]) {
        printf("%c", myText[counter]);
        counter++;
    }
}

and in action: 并在行动:

Korays-MacBook-Pro:~ koraytugay$ gcc koray.c
Korays-MacBook-Pro:~ koraytugay$ ./a.out 
hello world

My question is, why is this code even working? 我的问题是,为什么这个代码甚至可以工作? When (or how) does 何时(或如何)

while(myText[counter])

evaluate to false? 评价为假?

These 2 work as well: 这两个也有效:

while(myText[counter] != '\0')
while(myText[counter] != 0)

This one prints garbage in the console: 这个在控制台中打印垃圾:

while(myText[counter] !=  EOF)

and this does not even compile: 这甚至没有编译:

while(myText[counter] != NULL)

I can see why the '\\0' works, as C puts this character at the end of my array in compile time. 我可以看到为什么'\\0'有效,因为C在编译时将这个字符放在我的数组的末尾。 But why does not NULL work? 但是为什么NULL不起作用? How is 0 == '\\0' ? 0 == '\\0'怎么样?

AS for your last question, AS的最后一个问题,

But why does not NULL work? 但是为什么NULL不起作用?

Usually, NULL is a pointer type. 通常, NULL是指针类型。 Here, myText[counter] is a value of type char . 这里, myText[counter]char类型的值。 As per the conditions for using the == operator, from C11 standard, chapter 6.5.9, 根据使用==运算符的条件,从C11标准,第6.5.9章,

One of the following shall hold: 以下其中一项应持有:

  1. both operands have arithmetic type; 两个操作数都有算术类型;
  2. both operands are pointers to qualified or unqualified versions of compatible types; 两个操作数都是指向兼容类型的限定或非限定版本的指针;
  3. one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void; 一个操作数是指向对象类型的指针,另一个是指向合格或非限定版本的void的指针; or 要么
  4. one operand is a pointer and the other is a null pointer constant. 一个操作数是一个指针,另一个是空指针常量。

So, it tells, you can only compare a pointer type with a null pointer constant ## ( NULL ). 因此,它告诉您,您只能将指针类型与空指针常量 ##NULL )进行比较。

After that, 之后,

When (or how) does while(myText[counter]) evaluate to false? 何时(或如何) while(myText[counter])评估为false?

Easy, when myText[counter] has got a value of 0 . 很简单,当myText[counter]的值为0

To elaborate, after the initialization , myText holds the character values used to initialize it, with a "null" at last. 详细说明,在初始化之后myText保存用于初始化它的字符值,最后使用“null”。 The "null" is the way C identifies the string endpoint. “null”是C标识字符串端点的方式。 Now, the null, is represented by a values of 0 . 现在,null表示值0 So, we can say. 所以,我们可以说。 when the end-of-string is reached, the while() is FALSE . 到达字符串结尾时, while()FALSE

Additional explanation: 补充说明:

  • while(myText[counter] != '\\0') works, because '\\0' is the representation of the "null" used as the string terminator. while(myText[counter] != '\\0')有效,因为'\\0'是用作字符串终止符的“null”的表示。

  • while(myText[counter] != 0) works, because 0 is the decimal value of '\\0' . while(myText[counter] != 0)有效,因为0'\\0'的十进制值。

Above both statements are equivalent of while(myText[counter]) . 以上两个语句都相当于 while(myText[counter])

  • while(myText[counter] != EOF) does not work because a null is not an EOF . while(myText[counter] != EOF)不起作用,因为null不是EOF

Reference: (#) 参考:(#)

Reference: C11 standard, chapter 6.3.2.3, Pointers , Paragraph 3 参考: C11标准,第6.3.2.3章, 指针 ,第3段

An integer constant expression with the value 0 , or such an expression cast to type void * , is called a null pointer constant . 值为0的整型常量表达式或类型为void *的表达式称为空指针常量

and, from chapter, 7.19, 并且,从章节7.19开始,

NULL 空值
which expands to an implementation-defined null pointer constant 它扩展为实现定义的空指针常量


Note: In the end, this question and realted answers will clear the confusion, should you have any. 注意:最后,如果您有任何问题这个问题和实际答案将清除混乱。

When (or how) does this work? 什么时候(或如何)这项工作?

while(myText[counter])

Any built-in with value zero will evaluate to false in a boolean context. 任何值为零的内置函数都将在布尔上下文中计算为false。 So this while(myText[counter]) is false when myText[counter] is '\\0' , which has the value 0 . 因此while(myText[counter])myText[counter]'\\0'while(myText[counter])为false,其值为0

How is 0 == '\\0'? 0 =='\\ 0'怎么样?

It is defined that way in the language. 它是用语言定义的。 '\\0' is an int literal with value zero. '\\0'是一个值为零的int文字。 0 is also an int literal with value zero, so both compare equal, and they both evaluate to false in a boolean context 0也是一个值为零的int文字,因此两者都比较相等,并且它们在布尔上下文中都计算为false

In C, any non-zero value evaluates to true. 在C中,任何非零值的计算结果为true。 C strings are null-terminated. C字符串以空值终止。 That is, there is a special zero-value character after the last character in the string. 也就是说,在字符串中的最后一个字符后面有一个特殊的零值字符。

And so when the null terminator is reached, the zero value evaluates to false, and the loop terminates. 因此,当到达null终止符时,零值的计算结果为false,并且循环终止。

I can see why the '\\0' works, as C puts this character at the end of my array in compile time. 我可以看到为什么'\\ 0'有效,因为C在编译时将这个字符放在我的数组的末尾。 But why does not NULL work? 但是为什么NULL不起作用? How is 0 == '\\0'? 0 =='\\ 0'怎么样?

0 has the same value as '\\0' because '\\0' is a character with the value zero. 0与'\\ 0'具有相同的值,因为'\\ 0'是值为零的字符。 (Not to be confused with '0', which is the zero digit and has a value of 48.) (不要与'0'混淆,这是零数字,值为48.)

Regarding NULL , that actually can work since it also evaluates to zero. 关于NULL ,实际上可以工作,因为它也评估为零。 However, NULL is a pointer type so you may have to type cast to avoid errors. 但是, NULL是指针类型,因此您可能必须键入强制转换以避免错误。 (Hard to say for certain since you didn't post the error that you got.) (很难确定,因为你没有发布你得到的错误。)

How is 0 == '\\0'? 0 =='\\ 0'怎么样?

All characters have an 8-bit numeric value, for example, 'a' is 97 (decimal). 所有字符都有一个8位数字值,例如,'a'是97(十进制)。 The backslash in the character literal '\\0' introduces an "escape" to directly specify the character through its numeric value. 字符文字'\\ 0'中的反斜杠引入了一个“转义”,通过其数值直接指定字符。 In this case, the numeric value 0. 在这种情况下,数值为0。

  1. The termination of a string is \\0 字符串的终止是\\0
  2. NULL is used to initialise a pointer to a determined value NULL用于初始化指向确定值的指针

while(myText[counter]) evaluates to false as soon as counter points to the zero byte. while(myText[counter])只要counter指向零字节就计算为false。

In the end there is nothing different than a zero byte at the end of the string. 最后,字符串末尾的零字节没有什么不同。 Actually NULL would mean the same but it is used for notation purposes only in the context of pointers. 实际上, NULL意味着相同,但仅在指针的上下文中用于表示法。

If something is not 100% clear from a coding perspective, you might want to look inside your debugger watch window, what are the bits and bytes actually during program execution. 如果从编码角度看不清楚某些事情,您可能需要查看调试器监视窗口内部,程序执行期间实际的位和字节是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM