关于c++中函数的默认return类型的查询
Query regarding the default return type of a function in c++
一本书说:
If return type is not mentioned, it defaults to int.
为了检查这一点,我编写了以下代码。
#include <iostream>
print() {
return 3.3;
}
int main(void) {
double d = print();
std::cout << d;
return 0;
}
正如预期的那样,我得到了输出 3
。没问题。
我尝试了以下代码,但引起了一些困惑:
#include <iostream>
print() {
char x = 97;
std::cout << x;
return x;
}
int main(void) {
char c = print();
std::cout << c;
return 0;
}
我原以为这里会出现错误,但我得到的输出是 aa
。
这里有两个疑惑:
如果print
函数的return类型默认为int
,而我是return字符变量,为什么我没有'收到任何编译错误?
print()
return 究竟得到了什么?由于没有错误,显然 print()
已 returned 97
。但是 x
正在存储 a
。那么 97
是如何获得 returned 的?
[In Visual Studio C++ since VS2005]
您必须指定 return 类型,否则您将得到一个编译“错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int”.. .其他系统只生成警告。
但是,你故意忽略了第一期。所以让我们假设 print()
returns int
无论如何...
- If the return type of print function is defaults to int, and as I am
returning character variable, why I didn't receive any compilation
error?
- 您可以简单地通过分配给 'int'
来转换 'char' 类型
- What exactly got returned by print()? As there is no error, clearly
print() has returned 97. But x was storing a. Then how 97 got
returned?
- print() returns 一个
integer
,它是一组零和一:具有 97 的十进制表示和 'a' 的 ASCII 表示。任何变量都只是一组零和一,可以用不同的格式表示:十进制、十六进制、ASCII 字符等。
--
如果你想看到9797你必须写:
#include <iostream>
int print() // 1st issue: Add a return-type
{
char x = 97; // The ASCII code of 'a' character
std::cout << static_cast<int>(x); // 2nd issue: Cast to int, or it will print 'a'
return x; // You can convert a 'char' type simply by assigning to an 'int'
}
int main()
{
char c = static_cast<char>(print());
std::cout << static_cast<int>(c); // Same 2nd issue
}
"C++ 编译器将 char
、signed char
和 unsigned char
类型的变量视为具有不同的类型。char
类型的变量是默认情况下提升为 int
就好像它们是 signed char
类型,除非使用 /J 编译选项。在这种情况下,它们被视为类型 unsigned char
并提升为 int
没有符号扩展。” Microsoft Documentation
C++ 不允许没有 return 类型的函数。您最终遇到编译错误。
If the return type of print()
is defaults to int
, and as I am returning character variable, why I didn't receive error?
程序要求用户输入一些东西,它在赋值时被转换成一个整数。换句话说,如果他们输入 a
,则返回 97。因此,当您尝试存储它时:
char c = print();
// ^^^^^^^ returns 97 and assigns to 'c'
D-I-Y: 尝试将值:65、71、98 等存储在 char
类型中,你就会知道。
此外,您可能会得到一个错误:
error: ISO C++ forbids declaration of ‘print’ with no type [-fpermissive]
或者,您会收到来自编译器的警告(取决于您的编译器设置,这就是我的情况):
warning: explicit type is missing ('int' assumed)
What exactly got returned by print()
? As there is no error, clearly print()
has returned 97
. But x
was storing a
. Then how 97
got returned?
这称为隐式转换。当左值的类型为 char.
时,整数将被转换为 char
我认为您正在使用 Dev cpp。在 Dev C++ 中,对于没有 return 类型的函数,您通常不会得到错误,但是在像 Visual Studio 代码这样的编辑器中,如果您的函数没有任何 return 类型,那么您将得到编译错误或 void 将充当默认 return 类型,而不是 int.
一本书说:
If return type is not mentioned, it defaults to int.
为了检查这一点,我编写了以下代码。
#include <iostream>
print() {
return 3.3;
}
int main(void) {
double d = print();
std::cout << d;
return 0;
}
正如预期的那样,我得到了输出 3
。没问题。
我尝试了以下代码,但引起了一些困惑:
#include <iostream>
print() {
char x = 97;
std::cout << x;
return x;
}
int main(void) {
char c = print();
std::cout << c;
return 0;
}
我原以为这里会出现错误,但我得到的输出是 aa
。
这里有两个疑惑:
如果
print
函数的return类型默认为int
,而我是return字符变量,为什么我没有'收到任何编译错误?print()
return 究竟得到了什么?由于没有错误,显然print()
已 returned97
。但是x
正在存储a
。那么97
是如何获得 returned 的?
[In Visual Studio C++ since VS2005]
您必须指定 return 类型,否则您将得到一个编译“错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int”.. .其他系统只生成警告。
但是,你故意忽略了第一期。所以让我们假设 print()
returns int
无论如何...
- If the return type of print function is defaults to int, and as I am returning character variable, why I didn't receive any compilation error?
- 您可以简单地通过分配给 'int' 来转换 'char' 类型
- What exactly got returned by print()? As there is no error, clearly print() has returned 97. But x was storing a. Then how 97 got returned?
- print() returns 一个
integer
,它是一组零和一:具有 97 的十进制表示和 'a' 的 ASCII 表示。任何变量都只是一组零和一,可以用不同的格式表示:十进制、十六进制、ASCII 字符等。
--
如果你想看到9797你必须写:
#include <iostream>
int print() // 1st issue: Add a return-type
{
char x = 97; // The ASCII code of 'a' character
std::cout << static_cast<int>(x); // 2nd issue: Cast to int, or it will print 'a'
return x; // You can convert a 'char' type simply by assigning to an 'int'
}
int main()
{
char c = static_cast<char>(print());
std::cout << static_cast<int>(c); // Same 2nd issue
}
"C++ 编译器将 char
、signed char
和 unsigned char
类型的变量视为具有不同的类型。char
类型的变量是默认情况下提升为 int
就好像它们是 signed char
类型,除非使用 /J 编译选项。在这种情况下,它们被视为类型 unsigned char
并提升为 int
没有符号扩展。” Microsoft Documentation
C++ 不允许没有 return 类型的函数。您最终遇到编译错误。
If the return type of
print()
is defaults toint
, and as I am returning character variable, why I didn't receive error?
程序要求用户输入一些东西,它在赋值时被转换成一个整数。换句话说,如果他们输入 a
,则返回 97。因此,当您尝试存储它时:
char c = print();
// ^^^^^^^ returns 97 and assigns to 'c'
D-I-Y: 尝试将值:65、71、98 等存储在 char
类型中,你就会知道。
此外,您可能会得到一个错误:
error: ISO C++ forbids declaration of ‘print’ with no type [-fpermissive]
或者,您会收到来自编译器的警告(取决于您的编译器设置,这就是我的情况):
warning: explicit type is missing ('int' assumed)
What exactly got returned by
print()
? As there is no error, clearlyprint()
has returned97
. Butx
was storinga
. Then how97
got returned?
这称为隐式转换。当左值的类型为 char.
时,整数将被转换为char
我认为您正在使用 Dev cpp。在 Dev C++ 中,对于没有 return 类型的函数,您通常不会得到错误,但是在像 Visual Studio 代码这样的编辑器中,如果您的函数没有任何 return 类型,那么您将得到编译错误或 void 将充当默认 return 类型,而不是 int.