← Back to context

Comment by 9rx

5 days ago

> The defining characteristic of a programming "statement" is that it can perform some action

Given a declaration "statement" such as:

     int x;

What is the expected action? Memory allocation... I guess? Does any compiler implementation actually do that? I believe, in the age of type systems being all the rage, one would simply see that as a type constraint, not as a command to act upon.

Expressing an idea (the values used henceforth should be integers) seems more applicable, no?

> Given a declaration "statement" such as:

> int x;

> What is the expected action?

In a language that requires you to declare variables before you use them, it clearly does something - you couldn't do "x = 5;" before, and now you can. If you're trying to understand the program operationally (and if you're not then why are you thinking about statements?) it means something like "create a variable called x", even if the implementation of that is a no-op at the machine code level.

> I believe, in the age of type systems being all the rage, one would simply see that as a type constraint, not as a command to act upon.

But in that case by definition one would not be seeing it as a statement! Honestly to me it doesn't really matter whether "int x;" is an expression, a statement, or some mysterious third thing (in the same way that e.g. forward declaring a function isn't a statement or an expression). When we're talking about the distinction between statements and expressions we're talking primarily about statements like "x = x + 1;", which really can't be understood as a statement in the everyday English sense.

> Memory allocation... I guess? Does any compiler implementation actually do that?

Toy/research or embedded compilers do yes.

That looks like C. In C, declarations aren't statements: https://en.cppreference.com/w/c/language/statements.html

  • It's Hnlang, where declarations are statements. But perhaps the earlier comment was about C specifically? I admittedly missed it, if so.

    • In most programming languages, declarations aren't statements, because they don't have any effect at runtime. Maybe you can find some exceptions. Like in BASIC the DIM statement is sort of a declaration, but it does have a runtime effect; it changes the dimensions of an array. (Though some would say that in BASIC no statement is bright.)

      6 replies →