C语言:我不明白为什么它有效

时间:2021-11-15 21:48:53

Does someone knows what happens when a char is returned when int is expected?

有人知道在预期int时返回char时会发生什么吗?

char testunc1(char a)
{
    return a;
}

void main()
{

    int x1;
    x1 = testfunc1(7);
    printf("%d\n",x1);
}

3 个解决方案

#1


5  

The char is implicitly converted to an int.

char被隐式转换为int。

In section 6.5.16.1 paragraph 2 of the C99 standard:

在C99标准第6.5.16.1节第2段中:

In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.

在简单赋值(=)中,右操作数的值将转换为赋值表达式的类型,并替换存储在左操作数指定的对象中的值。

The type of an assignment expression is defined in section 6.5.16 paragraph 3:

赋值表达式的类型在6.5.16第3节中定义:

The type of an assignment expression is the type of the left operand unless the left operand has qualified type, in which case it is the unqualified version of the type of the left operand.

赋值表达式的类型是左操作数的类型,除非左操作数具有限定类型,在这种情况下,它是左操作数类型的非限定版本。

Since the variable is of type int, the returned char value is converted to type int as specified in section 6.5.16.1 of the C99 standard.

由于变量的类型为int,因此返回的char值将转换为类型int,如C99标准的6.5.16.1节中所述。

In this case, the value 7 can be fully represented by an int, so no loss of precision occurs as the 7 is stored in your int variable.

在这种情况下,值7可以用int完全表示,因此不会发生精度损失,因为7存储在int变量中。

#2


4  

When the function is called int is implicitly cast to char. As char can fit values up to 127, this works just fine. Later the result returned from the function is cast back to int, which of course again works as int fits all values that can fit in char.

当函数被调用时,int被隐式转换为char。由于char可以容纳最多127的值,这可以正常工作。稍后,从函数返回的结果将被转换回int,当然这也适用于int适合所有可以适合char的值。

#3


2  

There is an implicit conversion between all arithmetic types in C. So here the int value 7 is implicitly converted to char when passed to the testunc1 function. Same when testfunc1 return value is assigned to x1 (the char return value is converted to int).

在C中的所有算术类型之间存在隐式转换。因此,当传递给testunc1函数时,int值7被隐式转换为char。将testfunc1返回值赋给x1(char返回值转换为int)时也一样。

#1


5  

The char is implicitly converted to an int.

char被隐式转换为int。

In section 6.5.16.1 paragraph 2 of the C99 standard:

在C99标准第6.5.16.1节第2段中:

In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.

在简单赋值(=)中,右操作数的值将转换为赋值表达式的类型,并替换存储在左操作数指定的对象中的值。

The type of an assignment expression is defined in section 6.5.16 paragraph 3:

赋值表达式的类型在6.5.16第3节中定义:

The type of an assignment expression is the type of the left operand unless the left operand has qualified type, in which case it is the unqualified version of the type of the left operand.

赋值表达式的类型是左操作数的类型,除非左操作数具有限定类型,在这种情况下,它是左操作数类型的非限定版本。

Since the variable is of type int, the returned char value is converted to type int as specified in section 6.5.16.1 of the C99 standard.

由于变量的类型为int,因此返回的char值将转换为类型int,如C99标准的6.5.16.1节中所述。

In this case, the value 7 can be fully represented by an int, so no loss of precision occurs as the 7 is stored in your int variable.

在这种情况下,值7可以用int完全表示,因此不会发生精度损失,因为7存储在int变量中。

#2


4  

When the function is called int is implicitly cast to char. As char can fit values up to 127, this works just fine. Later the result returned from the function is cast back to int, which of course again works as int fits all values that can fit in char.

当函数被调用时,int被隐式转换为char。由于char可以容纳最多127的值,这可以正常工作。稍后,从函数返回的结果将被转换回int,当然这也适用于int适合所有可以适合char的值。

#3


2  

There is an implicit conversion between all arithmetic types in C. So here the int value 7 is implicitly converted to char when passed to the testunc1 function. Same when testfunc1 return value is assigned to x1 (the char return value is converted to int).

在C中的所有算术类型之间存在隐式转换。因此,当传递给testunc1函数时,int值7被隐式转换为char。将testfunc1返回值赋给x1(char返回值转换为int)时也一样。