【问题标题】:C++: 1-What is a different between define a variable to keep and print the answer ,2-and just print the answer?C++:1-定义一个变量来保留和打印答案,2-和只打印答案有什么不同?
【发布时间】:2019-02-15 21:53:37
【问题描述】:

有什么区别:

#define PI 3.14
using namespace std;

int main()
{
    int r;
    float area;
    cout << "Enter the radius:";
    cin>>r;
    area=r*r*PI;
    cout << area << endl;
}

#define PI 3.14
using namespace std;

int main()
{
    int r;
    cout << "Enter the radius:";
    cin>>r;
    cout << r*r*PI <<endl;
}

【问题讨论】:

  • 请将任何代码作为问题中的文本而不是图像发布。
  • 请每个问题回答一个问题。请发布一个实际问题,而不仅仅是一个链接

标签: c++ printing difference


【解决方案1】:

答案是在你的情况下它不会改变任何东西。

您总是可以使用 Godbolt 来查看 asm。

https://godbolt.org/z/wkFHM5

如你所见

area=r*r*PI;
cout << area << endl;

产生与

相同的代码
cout << r*r*PI << endl;

中间变量是一种存储您想要重用的数据的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 2021-07-17
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    相关资源
    最近更新 更多