Structure of functions

R

Richard Bos

Christian Christmann said:
is this code valid according to ANCI C99?

No. It is in C89, but not for the reason you think.
void foo( void )
{
int a = 10;
}; // semicolon allowed here?

int main( void )
{
foo();
return 0;
}

My question is if the semicolon is permitted at the end of a function

That's not a semicolon at the end of a function. That's a function
definition followed by an empty declaration. In C99, a declaration must
always include a specifier of one sort or another. In C89, this is a
declaration which declares no identifiers to have the implicit type int;
but implicit int was removed from C99.

Richard
 
K

Keith Thompson

Christian Christmann said:
is this code valid according to ANCI C99?

You misspelled "ISO". (ISO C99 is also an ANSI standard.)
void foo( void )
{
int a = 10;
}; // semicolon allowed here?

int main( void )
{
foo();
return 0;
}

My question is if the semicolon is permitted at the end of a function (see
foo) or does it have to be omitted like for the main function. I was
trying to get some information on that in the standard ISO/IEC 9899:1999
but could not find an appropriate paragraph. Maybe you have more luck.

Take a look at Annex A, the language syntax summary.

No, the semicolon is not legal. A trailing semicolon is not allowed
by the syntax of a function-definition. So the semicolon isn't part
of the function definition for foo. What you really have is:

void foo(void)
int a = 10;
}

; /* lone semicolon */

int main(void)
{
foo();
return 0;
}

A lone semicolon is not allowed by the grammar at that point. (A lone
semicolon is a valid null statement, but a statement isn't allowed
outside a function definition; there's no null declaration.)
 
R

Richard Heathfield

Richard Bos said:
No. It is in C89, but not for the reason you think.

No, it is not valid in C89.
That's not a semicolon at the end of a function. That's a function
definition followed by an empty declaration.

Here's the grammar, from 3.5 of the C89 draft:

++++
declaration:
declaration-specifiers init-declarator-list<opt> ;

declaration-specifiers:
storage-class-specifier declaration-specifiers<opt>
type-specifier declaration-specifiers<opt>
type-qualifier declaration-specifiers<opt>

init-declarator-list:
init-declarator
init-declarator-list , init-declarator

init-declarator:
declarator
declarator = initializer
++++

Please explain how an "empty" declaration is possible, in the light of the
above grammar, or demonstrate that the grammar for declarations changed in
a relevant way between the draft and the final Standard.
 
C

Christian Christmann

Hi,

is this code valid according to ANCI C99?

void foo( void )
{
int a = 10;
}; // semicolon allowed here?

int main( void )
{
foo();
return 0;
}

My question is if the semicolon is permitted at the end of a function (see
foo) or does it have to be omitted like for the main function. I was
trying to get some information on that in the standard ISO/IEC 9899:1999
but could not find an appropriate paragraph. Maybe you have more luck.

Thank you.

Chris
 
R

Roberto Waltman

Christian Christmann said:
is this code valid according to ANCI C99?

void foo( void )
{
int a = 10;
}; // semicolon allowed here?

int main( void )
{
foo();
return 0;
}

My question is if the semicolon is permitted at the end of a function (see
foo) or does it have to be omitted like for the main function. I was
trying to get some information on that in the standard ISO/IEC 9899:1999
but could not find an appropriate paragraph. Maybe you have more luck.

It is not allowed, regardless if it is immediately after the main
function or not. In a quick search in the standard, I was unable to
find a clear statement about this either. It is implied (I believe) in
the syntax rules summarized in "Annex A (informative) Language Syntax
Summary"
I traced back from "A.2.4 External definitions" (6.9)
translation-unit" to declarators, etc. and did not see anything that
will allow extra semicolons.
(Interestingly the word "semicolon" appears only once in the 554 pages
document + once in the Index)

For a quick check, assuming gcc handles this particular item properly,
compiling the following with "gcc -std=c99 -ansi -Wall -pedantic"
produces the warning "ISO C does not allow extra `;' outside of a
function" 3 times for each extra semicolon on line 3 and twice for
line 13.

#include <stdlib.h>

int i; ;;; /* line 3 */

int main()
{
;;
i = 0 ;;;
;;;;
return EXIT_SUCCESS ;;;;
}

;; /* line 13 */
 
A

aegis

Richard said:
Richard Bos said:


No, it is not valid in C89.


Here's the grammar, from 3.5 of the C89 draft:

++++
declaration:
declaration-specifiers init-declarator-list<opt> ;

declaration-specifiers:
storage-class-specifier declaration-specifiers<opt>
type-specifier declaration-specifiers<opt>
type-qualifier declaration-specifiers<opt>

init-declarator-list:
init-declarator
init-declarator-list , init-declarator

init-declarator:
declarator
declarator = initializer
++++

Please explain how an "empty" declaration is possible, in the light of the
above grammar, or demonstrate that the grammar for declarations changed in
a relevant way between the draft and the final Standard.

The portion of the grammar that you have pasted does not make your
case.
Any of the non-terminals specified by the declaration-specifiers rule
can still possibly yield Epsilon and likewise for the declarator
non-terminal.

You would have to show all the terminals that constitute those specific
rules mentioned above exclude the possibility of Epsilon.
 
R

Richard Heathfield

aegis said:

The portion of the grammar that you have pasted does not make your
case.
Any of the non-terminals specified by the declaration-specifiers rule
can still possibly yield Epsilon and likewise for the declarator
non-terminal.

Which one did you have in mind? Feel free to chase them down if you wish.
 
J

Jordan Abel

Richard Heathfield said:
Richard Bos said:


No, it is not valid in C89.


Here's the grammar, from 3.5 of the C89 draft:

++++
declaration:
declaration-specifiers init-declarator-list<opt> ;

declaration-specifiers:
storage-class-specifier declaration-specifiers<opt>
type-specifier declaration-specifiers<opt>
type-qualifier declaration-specifiers<opt>

init-declarator-list:
init-declarator
init-declarator-list , init-declarator

init-declarator:
declarator
declarator = initializer

declarator:
pointer<opt> direct-declarator

direct-declarator:
identifier
( declarator )
direct-declarator [ constant-expression<opt> ]

direct-declarator ( parameter-type-list )
direct-declarator ( identifier-list<opt> )

This part makes your point - you didn't follow it all the way to
'identifier'.
 
R

Richard Heathfield

Jordan Abel said:

declarator:
pointer<opt> direct-declarator

direct-declarator:
identifier
( declarator )
direct-declarator [ constant-expression<opt> ]

direct-declarator ( parameter-type-list )
direct-declarator ( identifier-list<opt> )

This part makes your point - you didn't follow it all the way to
'identifier'.

Yes, acknowledged. I was perhaps a little *too* lazy on this occasion (if
that's possible).

Thanks, Jordan.
 
F

Frank Silvermann

Keith said:
You misspelled "ISO". (ISO C99 is also an ANSI standard.)


Take a look at Annex A, the language syntax summary.

No, the semicolon is not legal. A trailing semicolon is not allowed
by the syntax of a function-definition. So the semicolon isn't part
of the function definition for foo. What you really have is:

void foo(void)
int a = 10;
}

; /* lone semicolon */

int main(void)
{
foo();
return 0;
}

A lone semicolon is not allowed by the grammar at that point. (A lone
semicolon is a valid null statement, but a statement isn't allowed
outside a function definition; there's no null declaration.)

#include <stdio.h>
#include <stdlib.h>


#define MIN_WORD_LENGTH 9
#define MAX_WORD_LENGTH 15
#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)


/* subroutine for random permutation */
void permute(char *, int);

int main(void)
{
char p[] = "abcdefghijklmnopqrstuvwxyz";
char q[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char r[] = "0123456789";

return 0;
}


void permute(char *m , int n)
{ ; }
/* end source */
Is it possible that I just made the error to which you refer? frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top