C 中的括号会导致隐式强制转换吗?

发布于 2024-12-04 20:34:55 字数 1227 浏览 3 评论 0原文

背景

上次我询问括号是否导致隐式转换(这里),@pmg 很好地指出“C 中在 int 下面没有做任何事情”但是,讨论是关于按位的运算符,而括号只是一种干扰。

简介

下面,括号是主要吸引力。或者,更无聊但更精确的是,我看到的唯一运算符是括号和赋值运算符。

在有关 C 括号运算符的参考文献中,我没有看到任何有关括号的内容更改类型(在类型转换语法之外,但不是这种情况)。

同时,这里有一个参考,提醒< em>是赋值时的自动类型转换,但我认为这不能解释我将在此处描述的静态分析工具行为。

正如我之前的问题,“OK”意味着静态分析工具没有警告隐式类型转换,“NOT OK”意味着它警告了。

int main(void)
{
    unsigned int  ui;
    int i;

    ui = (256U); // NOT OK (*) (1)
    i = (256U); // NOT OK (*)  (2)

    i = 256; // OK
    i = 256U; // NOT OK
    ui = 256U; // OK   (3)
    ui = 256; // NOT OK

 return(0);
}

除了前两个之外,我都能理解它们 - 括号有什么作用?如果他们以隐式类型转换的方式不做任何事情,那么我希望(1)可以,(2)不行。如果他们对小于 int 到 int 的类型进行自动类型提升,那么我预计 (1) 不行,(2) 可以。但这个工具说两者都不行。

这是静态分析工具错误,还是该工具正确,并且我还需要了解有关 C 中隐式类型转换的其他内容?

(顺便说一句,我希望值 256 足够小,不会在我的机器上造成溢出......)

Background

The last time I asked about whether parentheses were causing implicit cast (here), @pmg was nice enough to point out that "Nothing in C is done below int" But there, the discussion was about bitwise operators, and the parentheses turned out to be just a distraction.

Introduction

Below, the parentheses are the main attraction. Or, to be more boring but precise, the only operators I see are the parentheses and assignment operators.

At this reference about the C parentheses operator, I do not see anything about parentheses changing the type (outside of typecast syntax, which is not this case).

Meanwhile, here's a reference that reminds that there is automatic type conversion on assignment, but I don't think that will explain the static analysis tool behavior I will describe here.

As in my previous question, "OK" means that the static analysis tool did not warn about an implicit type conversion, and "NOT OK" means that it did.

int main(void)
{
    unsigned int  ui;
    int i;

    ui = (256U); // NOT OK (*) (1)
    i = (256U); // NOT OK (*)  (2)

    i = 256; // OK
    i = 256U; // NOT OK
    ui = 256U; // OK   (3)
    ui = 256; // NOT OK

 return(0);
}

I can understand them all except the first two - what do the parentheses do? If they do nothing in the way of implicit typecasting, then I would expect (1) to be OK and (2) to be NOT OK. If they do automatic type promotion of types smaller than int up to int, then I would expect (1) to be NOT OK and (2) to be OK. But this tool says that both are NOT OK.

Is this a static analysis tool error, or is the tool correct and there's something else I need to learn about implicit type conversions in C?

(BTW I hope that the value 256 is small enough not be causing overflow on my machine ...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-12-11 20:34:55

首先,让我们澄清一些术语。没有什么可以导致“隐式强制转换”,因为不存在这样的事情。 cast 是一个显式运算符,由表达式前面的括号中的类型名称组成,例如 (double)42;它指定一个转换。转换可以是显式的(由强制转换运算符指定),也可以是隐式的,如double x = 42;。所以你真正要问的是括号是否会导致隐式转换。

至少在您向我们展示的代码中,答案是否定的。

引用 C99 标准 (3.7 MB PDF) ,第 6.5.1p5 节:

带括号的表达式是主要表达式。它的类型和值
与未加括号的表达式相同。它是一个
左值、函数指示符或 void 表达式(如果
不带括号的表达式分别是左值、函数
指示符或 void 表达式。

而且由于 256U 已经是一个主表达式,因此括号根本没有区别;括号通常表示优先级,但在本例中没有优先级可表示。

您使用什么静态分析工具?您可能应该提交一份错误报告。

First, let's clear up some terminology. Nothing can cause an "implicit cast", because there is no such thing. A cast is a explicit operator, consisting of a type name in parentheses preceding an expression, such as (double)42; it specifies a conversion. Conversions can be either explicit (specified by a cast operator) or implicit, as in double x = 42;. So what you're really asking is whether parentheses can cause an implicit conversion.

And the answer, at least in the code you've shown us, is no.

Quoting the C99 standard (3.7 MB PDF), section 6.5.1p5:

A parenthesized expression is a primary expression. Its type and value
are identical to those of the unparenthesized expression. It is an
lvalue, a function designator, or a void expression if the
unparenthesized expression is, respectively, an lvalue, a function
designator, or a void expression.

And since 256U is already a primary expression, the parentheses make no difference at all; parentheses generally indicate precedence, but in this case there is no predecence to indicate.

What static analysis tool are you using? You should probably submit a bug report.

话少心凉 2024-12-11 20:34:55

该工具不知何故感到困惑。这里没有选角。这些括号仅表示优先级。

The tool is confused somehow. There's no casting here. Those parentheses just indicate precedence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文