简体   繁体   English

为什么 main 函数输出 73.4 即使它的返回类型是 int?

[英]Why the main function output 73.4 even if it's return type is int?

I am a beginner to C++ and am confused with the usage of int main();我是 C++ 的初学者,对int main();的用法感到困惑int main(); . . I have looked at some threads on this on the website, but I will be glad if someone could provide insight into my problem.我已经在网站上查看了有关此内容的一些主题,但是如果有人可以提供有关我的问题的见解,我会很高兴。

For the following program, if my input is 23, the output value is 73.4, which is NOT an int value contrary to the fact that my definition of the main() function should return an int value only.对于下面的程序,如果我的输入是 23,则输出值是 73.4,这不是一个 int 值,这与我对 main() 函数的定义应该只返回一个 int 值的事实相反。

What am I missing here?我在这里缺少什么? Thank you!谢谢!

My program is as follows:我的程序如下:

#include <iostream>
using namespace std;

int main(){

  double Celsius, Farenheit; // defining variables celsius and farenheit
  cout << " Input temperatue in celsius and press ENTER" << endl;
  cin >> Celsius;

  //double Farenheit;
  Farenheit = ((Celsius*180)/100) + 32;
  cout << "Value in Farenheit is:";
  cout << Farenheit <<endl ;

  //system("Pause");
  return 0;

  }

Indeed, the output of your program is 73.4 , but output and return values are a different things.事实上,你的程序的输出是73.4 ,但输出和返回值是不同的东西。

A program can write in the console, in fact any function can write to the console.一个程序可以写入控制台,实际上任何函数都可以写入控制台。 But the return value of a function is different than the output it produced.但是函数的返回值与其产生的输出不同。

In your code, the return value of the main function is not 73.4 .在您的代码中, main 函数的返回值不是73.4 But that's the output it produced in the console.但这是它在控制台中产生的输出。 It's return value is what's following the return statement:它的返回值是跟在 return 语句后面的:

return 0;

Yep!是的! That's right!这是正确的! The return value is what value is placed after the return keyword!返回值是放在 return 关键字之后的值!

Here's another example of a function returning an int and producing an output to the console:下面是另一个返回 int 并生成输出到控制台的函数示例:

int calculate() {
    std::cout << "calculating stuff..." << std::endl;
    return 2 * 2 * 4;
}

int main() {
    int return_value = calculate();
    std::cout << "return value of calculate() is: "
              << return_value << std::endl;

    return 0;
}

This produces the output:这会产生输出:

calculating stuff...计算东西...

return value of calculate() is: 16计算()的返回值是:16

Now maybe you wonder why return 0 from main .现在也许你想知道为什么从main返回0 This is usually to indicate to the operating system that the program has executed successfully.这通常是为了向操作系统表明程序已成功执行。

The purpose of main 's return value is to return an exit status to the operating system. main 的返回值的目的是向操作系统返回退出状态。 In standard C, the only valid signatures for main are: int main(void) and int main(int argc, char **argv) The form you're using: int main() is an old style declaration that indicates maintakes an unspecified number of arguments在标准 C 中,main 的唯一有效签名是: int main(void) 和 int main(int argc, char **argv)参数数量

to solve your problem.解决您的问题。 change the data types.aa更改数据类型.aa

int celcius,fahrenheit;

or或者

double celcius; int fahrenheit;

Your definition of Farenheit makes it a variable of type double , and therefore it would store a value of type double .您对Farenheit定义使其成为double类型的变量,因此它将存储double类型的值。 The int of int main() have nothing to do with this.intint main()什么都没有做这个。 It represents the type of the return type of main() , which is 0 because of your last line of the program, return 0 .它表示main()的返回类型的类型,因为程序的最后一行return 0 ,所以它是return 0 In fact, you can actually consider int in int main() completely useless for now.事实上,你其实可以考虑intint main()完全无用的现在。

To make the program output an integer, you should define Farenheit as an int为了使程序输出一个整数,你应该将Farenheit定义为一个int

#include <iostream>
using namespace std;

int main(){

  double Celsius;
  int Farenheit; // defining variables celsius and farenheit
  cout << " Input temperatue in celsius and press ENTER" << endl;
  cin >> Celsius;

  //double Farenheit;
  Farenheit = ((Celsius*180)/100) + 32;
  cout << "Value in Farenheit is:";
  cout << Farenheit <<endl ;

  //system("Pause");
  return 0;

  }

暂无
暂无

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

相关问题 Memory 浪费? 如果 main() 应该只返回 0 或 1,为什么 main 声明为 int 而不是 short int 甚至 char? - Memory waste? If main() should only return 0 or 1, why is main declared with int and not short int or even char? 为什么在 function 'int main()' -&gt; 's' 可以在这个 function 中使用? - Why in function 'int main()' -> 's' may be used unitialized in this function? 为什么有必要在函数定义中包含返回类型,即使它在声明中指定了? - Why is it necessary to include the return type in a function definition even if it's specified in the declaration? 返回 int main() 而不是前一个函数? - Return to int main() instead of to previous function? 无法通过在主函数中添加“返回 0”来解决“缺少类型说明符 - 假设为 int” - "Missing type specifier - int assumed" cannot be solved by adding "return 0" to the main function main()下面的'i'类型。为什么它是int&? - The type of 'i' below in main(). Why is it an int&? 返回 function 的 output 作为 main() 的返回码 - Returning the output of a function as the return code of main() 为什么 isdigit() 的返回类型是 int 而不是 boolean? - Why is the return type of isdigit() int instead of boolean? 为什么这个编译器警告只显示 int 而不是 string? “在 function 返回类型上忽略类型限定符” - Why does this compiler warning only show for int but not for string? "type qualifiers ignored on function return type" 为什么不能推断出主要的返回类型? - Why can the return type of main not be deduced?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM