I found this paragraph in C17(6.7.9 #1):
initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }
initializer-list:
designation{opt} initializer
initializer-list , designation{opt} initializer
designation:
designator-list =
designator-list:
designator
designator-list designator
designator:
[ constant-expression ]
. identifier
Does this mean that the initializer expands to an assignment expression and we can use it this way?:
#include <stdio.h>
int main(void)
{
if((int n = 9) == 9){}
return 0;
}
If not, why not?
Why is initialization not an expression?
Why is initialization referred to as a declaration?
Why is the assignment expression not an initializer?
What is a declarator?
I'm sorry for such indiscreet questions.I'm new to C and I just want to know.
And if it's not difficult for you, could you provide the relevant paragraphs from the C Standard?
Thank you very much!
Does this mean that the initializer expands to an assignment expression and we can use it this way?
It is actually the opposite: The grammar says you can have an assignment inside an initialization, not an initialization inside an assignment.
If not, why not?
In the grammar rules, the :
in A: B
means A can be a B.
Why is initialization not an expression?
In the grammar, initializer appears in init-declarator (that is, initializer appears on the right side of a :
rule with init-declarator on the left), which appears in init-declarator-list, which appears in declaration. None of those appear in the grammar for expression, except a compound literal has initializers. A compound literal is something you can use in an expression, but it will not be declaring any variables.
Why is initialization referred to as a declaration?
Where do you see that?
Why is the assignment expression not an initializer?
An assignment expression can be an initializer. For example, in int a, b = a = 3;
, the a = 3
is an assignment expression. Its value is 3, so b
is initialized to 3.
What is a declarator?
A declarator is the part of a declaration that goes where the D is here: Type D;
, excluding the = initializer
that could be there with the D. That is, you could have int foo = 3;
, but only the foo
would be the declarator. (There can be additional parts to Type and can be multiple declarators, in a list, but I have used this simplified example for illustration.) That D can be a name (int foo;
), an array declarator (int foo[3];
), a pointer declarator (int *foo;
), a function declarator (int foo(char, float);
), or a parenthesized declarator (int (*foo);
or int (foo);
), and it can be combinations of those (int *foo[3];
, int (*foo)[3]
). (There can be additional parts of those declarators, such as int foo[const 3];
, which I do not cover further here.)