0

I am studying on how to make a bytecode interpreter (the language i am studing is clox at the site https://craftinginterpreters.com/). In it a valid clox program is defined as a list of declarations. A declaration is defined as either a class, function or variable declaration OR as a statement.

Now in C, i know there are different kinds of declarations and there are different kinds of statements but none of the types of declarations are a statement and none of the type of statements are a declaration. I think any possible line of C code is either one or the other so how do the standard define a C program ?

A list of lines that can be either a definition or a statement ?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Mazarin64
  • 177
  • 1
  • 1
  • 4
  • 2
    I mean in a nutshell a C program is statements and expressions but if you want to know the full details then you got to read the standard for yourself. Even just glancing through cppreference.com/w/c you can get a good understanding. – Brady Dean Apr 26 '20 at 03:21

2 Answers2

0

A C program is defined by its grammar and the details of the implementation defined in the standard. Get yourself a copy of the C standard, any version will do for the basics, and look at the grammar. A summary of the grammar can be found in Annex A.

Section 6.8 defines a statement as being one of any number of specific types of statements.

statement:
  labeled-statement
  compound-statement
  expression-statement
  selection-statement
  iteration-statement
  jump-statement

It goes on to provide more detail, for example, it tells us the order in which statements are executed.

A statement specifies an action to be performed. Except as indicated, statements are executed in sequence.

And you drill down deeper. For example, 6.8.4 defines a selection-statement which covers the basic control structures if, else, and switch.

selection-statement:
  if (expression) statement
  if (expression) statement else statement
  switch (expression) statement

Note how statements can be build of more statements.

And in this way the grammar of the language is built up.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Nice answer, maybe you could also answer my question on loops – Ardent Coder Apr 26 '20 at 03:37
  • You can refer to the standard for details on the other kinds of statements. – Raymond Chen Apr 26 '20 at 04:12
  • @ArdentCoder Which question are you referring to? – Schwern Apr 26 '20 at 04:55
  • @Schwern You can see a question on "number of loops in C++" in my profile – Ardent Coder Apr 26 '20 at 05:25
  • 2
    @ArdentCoder I'd have to read through the C++ grammar, but you can do that as well as I can. I wouldn't expect that sort of question in an interview; you don't need to memorize language trivia to be a good programmer. If I were to ask this in an interview, there would be no right answer. I'd want to hear your process of working it out, just like in Ayxan's answer. – Schwern Apr 26 '20 at 08:22
  • @Schwern Oh I see, thanks – Ardent Coder Apr 26 '20 at 08:24
  • This answer discusses only *statements* and fails to discuss or distinguish *declarations*, which the question asks about and which are distinct from statements in the C standard. – Eric Postpischil Apr 26 '20 at 09:40
0

The C standard defines a translation unit (effectively the source file being compiled, including all the files it includes with #include) as a list of external-declaration items, and each external-declaration is either a function-definition or a declaration (C 2018 6.9 1).

So, fundamentally, each source file of a C program is a list of declarations, including function definitions.

Each function-definition has, after the part that declares the type(s) of the function and its parameter(s), a compound-statement (6.9.1 1). A compound-statement is a list of declaration or statement items enclosed in { and } (6.8.2 1).

Each declaration tells us about (usually1) one or more identifiers (6.8 2 and 5). A statement is some sort of instruction for the computer to “do something,” such as iterate a loop or evaluate an expression. In the C grammar, there is no overlap between declarations and statements.

Further information on declarations is in clause 6.7 of the C 2018 standards, and further information on statements is in clause 6.8.

Footnote

1 In the C grammar, a static_assert-declaration is also a declaration. It does not declare any identifier but creates a compile-time assertion that is tested and that produces an error message if the assertion fails.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312