Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 591 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can parentheses in C cause implicit cast?

#1
**Background**

The last time I asked about whether parentheses were causing implicit cast ([here][1]), @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][2], 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][3], 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 ...)

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

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

#3
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][1] (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.



[1]:
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through