Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can parentheses in C cause implicit cast?

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 ...)

like image 638
talkaboutquality Avatar asked Sep 17 '11 18:09

talkaboutquality


2 Answers

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.

like image 166
Keith Thompson Avatar answered Oct 27 '22 22:10

Keith Thompson


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

like image 25
David Heffernan Avatar answered Oct 27 '22 22:10

David Heffernan