* C中的Vs ++优先级

我无法理解以下C代码的输出:

#include main() { char * something = "something"; printf("%c", *something++); // s printf("%c", *something); // o printf("%c", *++something); // m printf("%c", *something++); // m } 

请帮忙 :)

 // main entrypoint int main(int argc, char *argv[]) { char * something = "something"; // increment the value of something one type-width (char), then // return the previous value it pointed to, to be used as the // input for printf. // result: print 's', something now points to 'o'. printf("%c", *something++); // print the characer at the address contained in pointer something // result: print 'o' printf("%c", *something); // increment the address value in pointer something by one type-width // the type is char, so increase the address value by one byte. then // print the character at the resulting address in pointer something. // result: something now points at 'm', print 'm' printf("%c", *++something); // increment the value of something one type-width (char), then // return the previous value it pointed to, to be used as the // input for printf. // result: print 's', something now points to 'o'. printf("%c", *something++); } 

结果:

 somm 

有关详细信息,请参见http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

 printf("%c", *something++); 

获取*的东西,然后递增它(’s’)

 printf("%c", *something); 

只需获取char(现在是第二个,因为最后一个语句中的增量(’o’)

 printf("%c", *++something); 

递增然后得到新位置的字符(’m’)

 printf("%c", *something++); 

获取*的东西,然后递增它(’m’)

这很简单。

 char * something = "something"; 

指针的分配。

 printf("%c\n", *something++);//equivalent to *(something++) 

指针递增但增量前的值被取消引用,而后递增。

 printf("%c\n", *something);//equivalent to *(something) 

指针现在在前一个语句中增加后指向’o’。

 printf("%c\n", *++something);//equivalent to *(++something) 

指针递增指向’m’并在递增指针后取消引用,因为这是预递增。

 printf("%c\n", *something++);//equivalent to *(something++) 

与第一个答案相同。 另请注意printf中每个字符串末尾的'\n' 。 它使输出缓冲区刷新并使行打印。 始终在printf的末尾使用\n

您可能也想看看这个问题

始终使用顺时针顺时针规则

 printf("%c\n", *something++); 

根据你将首先遇到的规则*所以得到值然后++意味着增量

在此处输入图像描述

在第三种情况下printf("%c\n", *something++);

在此处输入图像描述

所以根据图像增量值++然后得到值*