Home > Backend Development > C++ > Why Do Parentheses in C Variable Declarations Not Change the Type?

Why Do Parentheses in C Variable Declarations Not Change the Type?

Barbara Streisand
Release: 2024-11-01 02:09:02
Original
542 people have browsed it

Why Do Parentheses in C   Variable Declarations Not Change the Type?

Parentheses in C Variable Declarations

Consider the following code:

<code class="cpp">int main() {
    int(s);
}</code>
Copy after login

The use of parentheses around the variable declaration int(s); may be unexpected, as it creates a valid variable named s. This behavior stems from a specific part of the C standard known as the "Meaning of Declarators Rule" found in [dcl.meaning].

According to this rule, when parentheses enclose a declaration, the type of the variable being declared remains unchanged. In your example, s is a declarator, and placing it within parentheses does not alter its type or meaning.

This feature allows for more complex declarations to be constructed. For instance, it can distinguish between an array of pointers and a pointer to an array:

<code class="cpp">int * a [10];     // a is an array of ten pointers to int.
int ( * b ) [10]; // b is a pointer to an array of ten ints.</code>
Copy after login

The above is the detailed content of Why Do Parentheses in C Variable Declarations Not Change the Type?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template