[英]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.该
int
的int 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.事实上,你其实可以考虑
int
在int 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.