'for' loop initial declarations are only allowed in C99 mode - skywalking

skywalking-php-sdk安装中make提示如下:
/root/skywalking-5.0.1/src/sky_core_log.c:66:5: error: 'for' loop initial declarations are only allowed in C99 mode

Related

Is a block a statement?

In C11 standard ( I don't have the latest standard):
6.8 Statements and blocks
A block allows a set of declarations and statements to be grouped into one syntactic unit.
The initializers of objects that have automatic storage duration, and the variable length
array declarators of ordinary identifiers with block scope, are evaluated and the values are
stored in the objects (including storing an indeterminate value in objects without an
initializer) each time the declaration is reached in the order of execution, as if it were a
statement, and within each declaration in the order that declarators appear.
Is a block a statement?
Simply put, yes. In its simplest interpretation, if we look at the grammar production before your quote. The most obvious block is a compound statement.
statement:
labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement
The production of compound-statement describes blocks. And even says this explicitly in a paragraph following that
6.8.2 Compound statement
compound-statement:
{ block-item-list }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
2 A compound statement is a block.
And other statements in the following sections are also designated as blocks. For instance
6.8.4 Selection statements
3 A selection statement is a block whose scope is a strict subset of
the scope of its enclosing block. Each associated substatement is also
a block whose scope is a strict subset of the scope of the selection
statement.
Nothing except the statements described in 6.8 is ever called a block. So blocks are always a statement of some sort. The rule of thumb is that a statement with { } always means a block.
From wikibooks,
A block is a program region containing definitions of variables and that delimits the regions where these definitions apply. In C programming language, a block is created using a pair of curly braces. The beginning of the block is denoted by an open curly brace '{' and the end is denoted by a closing curly brace '}'. The block collects statements together into a single compound statements.
The C code bellow shows a block where the block is used only to define a scope of a variable and it is not a statement.
#include <stdio.h>
int main() {
int n = 1;
{
int n = 2;
printf("%d\n", n);
}
printf("%d\n", n);
}
EDIT:
As already mentioned I have made a mistake saying that block is not always a statement but the correct answer is that block is ALWAYS a statement.

lone declaration in if statement?

I did not understand why this works:
if(1)
{
int i;
}
and this not:
if(1)
int i;
error: expected expression before int
If you could supply some standard reference.
In C, declarations can only occur as part of a compound-statement, not as part of any of the other kinds of statement (see C11 6.8 and 6.8.2).
This is due to C grammar. Specifically, the if statement is defined as:
if ( expression ) statement
According to C11 6.8 Statements and blocks, the statement is one of:
labeled-statementcompound-statementexpression-statementselection-statementiteration-statementjump-statement
The declaration can only directly appear in either compound-statement (i.e. delimeted by { ... }) or as a special case as first expression in for iteration-statement.
Declaration must be a part of a block-item1.
The block-item-list contains a block-item2.
And the block-item-list can only be inside brackets, as part of a compound-statement3.
This is different in C++, as a declaration-statement is included in statement (the former allows, via a block-statement, defining variables).
(Quoted from ISO/IEC 9899:201x 6.8.2 Compound statement 1)
1
block-item:
declaration
2
block-item-list:
block-item
block-item-list block-item
3
compound-statement:
{ block-item-list opt }
As you can see from section 6.8.2p1 which covers the { ... }-style example, a declaration is permitted within a compound-statement.
Section 6.8.4p1, however, which covers the syntax for selection statements (i.e. if (...) ...) doesn't explicitly permit any declarations. In addition, this notation requires an expression, as hinted by the error message, "expected expression ..."
The first code example declares an int visible only inside an otherwise empty scope ... hard to see the utility! The second apparently attempts to conditionally? declare an int visible in the enclosing (function?) scope. Hard to imagine the utility of such a conditional declaration ... would it require run-time re-compilation?

Error: Declaration of non-variable 'strlen' in 'for' loop initial declaration - C

The title is the exact error my compiler(geany on ubuntu) gives me when I try to compile. The part of code in question is the following:
for(int q=strlen(stringA)-1;q>0;q--)
{
//do stuff
}
I've already set the C99 mode and the initialization of variables in the 'for' header is accepted. Is what I did simply illegal in C?
I assume you are missing an include. Try:
#include <string.h>

Can't have label on mid-stream declaration in C?

So this in C99:
label:
int ret = function(of, stuff);
gives a compile-time error, whereas this:
label:
;
int ret = function(of, stuff);
works just fine.
Is this a compiler bug? Or is this a bug in the definition of the C standard? Or if this is part of the C99 standard, perhaps someone would rise to the defense of the C standard to claim that this makes perfect sense?
Labels, which are defined in N1256 6.8.1 Labeled statements, can only contain statements.
Syntax
1 labeled-statement:
identifier : statement
case constant-expression : statement
default : statement
int ret = function(of, stuff); is an declaration, which is defined in N1256 6.7 Declarations and isn't a statement.
Statements are defined below in N1256 6.8 Statements and blocks:
Syntax
1 statement:
labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement
compound-statement is so-called blocks, which is 0 or more declarations and statements surrounded by {}.
expression-statement is zero or one expression defined in N1256 6.5 Expressions, followed by a semicolon like i++;. The expression in the syntax is defined in N1256 6.5.17 Comma operator.
selection-statement is if and switch statement.
iteration-statement is while, do-while and for statement.
jump-statement is goto, continue, break and return statement.
As you see, declarations are not a statement, so you cannot put labels to declarations.
Arguably a bug in the spec -- when it was changed to allow statements and declarations to be mixed in a block (rather than requiring all declarations before statements), it should also have been changed to allow labels on a declaration, but it was not. An artifact of how the language evolved over time.
Its not a major problem as you discovered, as you can work around it trivially by putting the label on an empty statement before the declaration.

C statement between function declaration and curly brace [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is useful about this C syntax?
C variable declarations after function heading in definition
What weird C syntax is this?
I'm trying to understand some code and it has something like the following:
int getr(fget)
FILE *fget;
{
/* More declarations and statements here */
return (1);
}
Is there any difference between the above and:
int getr(fget)
{
FILE *fget;
/* More declarations and statements here */
return (1);
}
If so, how do they differ?
Both functions are declared in the old-style (non-prototype) form. Old-style function declarations are obsolescent in the current C standard and their use is strongly discouraged by the C Standard.
In the second form there is no mention of the fget parameter type which is assumed to be an int. Then another object fget of type FILE * is declared and it shadows the parameter variable with the same name.
With gcc the -Wshadow warning option would get you a warning in your second example because of the shadowing of the parameter:
-Wshadow
Warn whenever a local variable shadows another local variable,
parameter or global variable or whenever a built-in function is shadowed.
The first one is the K & R style of function definition, it is an obsolescent featureRef 1.
The second is popularly known as Implicit int feature prior to c99 standard.
Prior to c99 If a function returned no explicit type or didn't specify a type in declaration, then the type was assumed to be a int.
Both of the methods have been deprecated and find a mention in the c99 Standard.
References:
C99 Standard: Foreword Para 7:
Major changes in the second edition included:
— remove implicit int
— remove implicit function declaration
Ref 1
6.11.7 Function definitions
The use of function definitions with separate parameter identifier and declaration lists
(not prototype-format parameter type and identifier declarators) is an obsolescent feature.

Resources