C程序输出说明[重复](Explanation of output of a C program [duplicate])

这个问题在这里已有答案:

为什么这些构造使用前后增量未定义的行为? 14个答案 c 1答案 中前缀和后缀的优先级和关联性

以下程序给出了输出:

hffltgpshfflt

有人可以解释一下,postfix ++,前缀++和dereference(*)运算符的运算符优先级如何决定这个输出?

#include<stdio.h> int main() { char arr[] = "geeksforgeeks"; char *ptr = arr; while(*ptr != '\0') ++*ptr++; printf("%s %s", arr, ptr); getchar(); return 0; }

This question already has an answer here:

Why are these constructs using pre and post-increment undefined behavior? 14 answers Precedence and associativity of prefix and postfix in c 1 answer

The following program gives output:

hffltgpshfflt

Can somebody explain this how does operator precedence of postfix++,prefix++ and dereference(*) operators decide this output?

#include<stdio.h> int main() { char arr[] = "geeksforgeeks"; char *ptr = arr; while(*ptr != '\0') ++*ptr++; printf("%s %s", arr, ptr); getchar(); return 0; }

最满意答案

一旦学会了运算符优先级和关联性规则,就很容易了。

表达式++*ptr++相当于++*(ptr++) ,相当于++(*(ptr++)) 。

所以操作的顺序是

后增量运算符(返回指针ptr的旧值) 指针ptr的解除引用(在指针递增之前) 取消引用结果的前缀增量,增加ptr指向的值,将例如'g'变为'h'等。 指针ptr递增(实际上是步骤1的一部分)

It's easy once one learn the operator precedence and associativity rules.

You expression ++*ptr++ is equivalent to ++*(ptr++) which is equivalent to ++(*(ptr++)).

So the order of operations is

The post-increment operator (which returns the old value of the pointer ptr) The dereferencing of the pointer ptr (before the pointer is incremented) The prefix-increment of the result of the dereference, increasing the value pointed to by ptr, turning e.g. 'g' to 'h' etc. The pointer ptr is incremented (actually part of step 1)C程序输出说明[重复](Explanation of output of a C program [duplicate])

这个问题在这里已有答案:

为什么这些构造使用前后增量未定义的行为? 14个答案 c 1答案 中前缀和后缀的优先级和关联性

以下程序给出了输出:

hffltgpshfflt

有人可以解释一下,postfix ++,前缀++和dereference(*)运算符的运算符优先级如何决定这个输出?

#include<stdio.h> int main() { char arr[] = "geeksforgeeks"; char *ptr = arr; while(*ptr != '\0') ++*ptr++; printf("%s %s", arr, ptr); getchar(); return 0; }

This question already has an answer here:

Why are these constructs using pre and post-increment undefined behavior? 14 answers Precedence and associativity of prefix and postfix in c 1 answer

The following program gives output:

hffltgpshfflt

Can somebody explain this how does operator precedence of postfix++,prefix++ and dereference(*) operators decide this output?

#include<stdio.h> int main() { char arr[] = "geeksforgeeks"; char *ptr = arr; while(*ptr != '\0') ++*ptr++; printf("%s %s", arr, ptr); getchar(); return 0; }

最满意答案

一旦学会了运算符优先级和关联性规则,就很容易了。

表达式++*ptr++相当于++*(ptr++) ,相当于++(*(ptr++)) 。

所以操作的顺序是

后增量运算符(返回指针ptr的旧值) 指针ptr的解除引用(在指针递增之前) 取消引用结果的前缀增量,增加ptr指向的值,将例如'g'变为'h'等。 指针ptr递增(实际上是步骤1的一部分)

It's easy once one learn the operator precedence and associativity rules.

You expression ++*ptr++ is equivalent to ++*(ptr++) which is equivalent to ++(*(ptr++)).

So the order of operations is

The post-increment operator (which returns the old value of the pointer ptr) The dereferencing of the pointer ptr (before the pointer is incremented) The prefix-increment of the result of the dereference, increasing the value pointed to by ptr, turning e.g. 'g' to 'h' etc. The pointer ptr is incremented (actually part of step 1)