C编程中的幂函数 [英] power function in c programming

查看:281
本文介绍了C编程中的幂函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<math.h>
int main()
{
    int a=2,c;
    c=pow(++a,a++);
    printf("%d",c);
}




输出为16




output is 16

推荐答案

如果阅读此内容,您将看到通常如何从右侧开始将函数参数压入堆栈.

http://www.csee.umbc.edu/~chang/cs313.s02/stack. shtml [ ^ ]

看来您的编译器就像ms c编译器一样使用此约定

对于pow(x,y),先推y,然后推x.

现在,在函数调用中,最右边的表达式是a ++(后缀),因此a == 2的值先被压入然后递增.
现在变量a的值为3.
下一个表达式是++ a,因此a再次增加为4,然后将值压入堆栈.

因此,对于调用pow的函数,堆栈现在将y的值保留为2,将x的值保留为4.

4 ^ 2 = 16.
If you read this you will see how function parameters are commonly pushed onto the stack starting from the right hand side.

http://www.csee.umbc.edu/~chang/cs313.s02/stack.shtml[^]

It appears you compiler uses this convention as do ms c compilers

For pow(x,y) y would be pushed first then x.

Now in your function call the rightmost expression is a++ (postfix) so the value of a == 2 is pushed first then incremented.
The variable a now has the value 3.
The next expression is ++a so a is incremented again making it 4 and the value is then pushed on the stack.

Thus for the function call to pow the stack now holds the value of 2 for y and 4 for x.

4^2=16.


根据语言规范,函数调用参数的评估为未排序".
因此,对多个参数的副作用(如前/后递增/递减)是未定义的行为".

IE.编译器可以在执行函数主体之前自由按任何顺序评估函数参数-不能保证从左至右,从右至左或任何其他顺序对其进行评估.

唯一的解决方案:不要在一个完整表达式中对同一对象产生(多个)副作用.

例如.以下也是未定义的行为:
Evaluation of function call parameters is "unsequenced" according to the language specification.
Side effects (like pre-/post- -increment/-decrement) on multiple arguments is therefore "undefined behaviour".

I.e. a compiler is free to evaluate function arguments in any order before executing the function body - no guarantee that it is evaluated from left to right, nor from right to left, nor any other sequence.

The only solution to this: don''t do (multiple) side effects on the same object in one full expression.

E.g. the following are also undefined behaviour:
int  a = ...
int *b = ...
a = b[a++];  // undefined behaviour
a = a++ + 1; // undefined behaviour



语言规范具有几种未定义的行为".未定义的行为是:本国际标准对此没有要求的行为" .

干杯
Andi



The language specification has several "undefined behaviour". Undefined behaviour is: "behavior for which this International Standard imposes no requirements".

Cheers
Andi


如果您想知道为什么它是16而不是27,这是为什么:

首先,它将a ++压入函数堆栈中,从而压入2.
然后它将a ++递增后将其变为3.
然后它会预先递增a ++使其变为4.
然后将4压入堆栈.

4的2的乘方(平方)是16.

奇怪的是,在.NET中,它的值为27,因为在.NET中,它的行为略有不同.首先,它先将其递增至3,然后将3推入堆栈,然后将其递增至4,然后等式变为4立方或27.
If you are wondering why its 16 and not 27, here''s why:

First, it pushes a++ on the function stack, which pushes 2.
Then it post-increments a++ turning it into 3.
Then it pre-increments a++ turning it into 4.
Then it pushes 4 onto the stack.

4 raised to the power of 2 (squared) is 16.

Oddly enough, in .NET, this gives you a value of 27, because in .NET it behaves a little differently. First it pre-increments a turning it into 3, pushes 3 onto the stack then increments it to 4, then the equation becomes 4 cubed or 27.


这篇关于C编程中的幂函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆