semicolon after a brace in AVR-GCC-> when?

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what is the meaning of semicolons after closing braces?

First, do what you can - then, do what's possible - suddenly, you'll do the impossible. (Thadëus Judas )

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Example?

Leon Heller G1HSM

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

semicolons are used to terminate a command, braces are used to enclose a section of code (eg to mark the boundary of code that's inside an 'if' statement).

I have never used a semicolon after a closing brace when coding AVR-GCC.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I have never used a semicolon after a closing brace when coding AVR-GCC.
Never? Never initialized an array or a structure?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Standard C allows "empty" commands, so you can even place consecutive semikolons, and of course even after closing braces. They are just omitted.

/Martin.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Never? Never initialized an array or a structure?

Also at the end of an enum definition.
Quote:
in AVR-GCC

These things are dictated by the C standard, not avr-gcc. All implementations of C will be the same.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Standard C allows "empty" commands, so you can even place consecutive semikolons, and of course even after closing braces. They are just omitted.

Not always

if(PINB==0)
{
   PORTC=1;
   PORTA=2;
};          //error
else
   PORTC=0;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if(PINB==0)
{
   PORTC=1;
   PORTA=2;
};          //this semikolon terminates the if-statement
else        //else without if is an invalid statement
   PORTC=0;

/Martin.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I explain it to myself this way

if(PINB==0)
{
   PORTC=1;
   PORTA=2;
}
...    //here may not be any command, neither empty one
else   
   PORTC=0;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Question of style, I prefer to format the code like this:

if (PINB==0) {
    PORTC=1;
} else {
    PORTA=1;
}

For me the curly braces and the if/else/while etc. statements belong together.

Markus

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Question of style, I prefer

Me too - always wrap even single if(){} or else{} statements in braces or you can face the classic error:

if(PINB==0) 
{ 
   PORTC=1; 
   PORTA=2; 
}
else
   PORTC=0;

becoming, after a small modification:

if(PINB==0) 
{ 
   PORTC=1; 
   PORTA=2; 
}
else
   PORTC=0;
   PORTA=0;

which is almost certainly not what the programmer intended!

BTW this thread has nothing to do about GCC specifically - it's a question about ISO ANSI C so I won't move this thread to the GCC forum. (just in case anyone thought the mention of "avr-gcc" in the thread title meant it should move ;-))

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Me too - always wrap even single if(){} or else{} statements in braces or you can face the classic error:
Same here. When I was working, we were required to have a coding standards document for each project. I always specified this as a required coding standard.

Don

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ezharkov wrote:
Quote:
I have never used a semicolon after a closing brace when coding AVR-GCC.
Never? Never initialized an array or a structure?

Fair enough for statically initialising an array. I have done it in the past, but so long ago it didn't come to mind :)

structures though, use this syntax:

typedef struct
{
	char data[DATA_BYTES];
	char CRC[CRC_BYTES];
} MessagePayload;

is there an alternate syntax that compiles?

re if/else statements I agree that proper coding standard is to always include the braces to make it explicitly clear what the intended execution is.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
structures though, use this syntax:

No, typedef uses that format. For a struct alone it would be:

struct MessagePayload
{ 
   char data[DATA_BYTES]; 
   char CRC[CRC_BYTES]; 
};

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe the OP means something used like this:(from Dean's USART tutorial)

while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been recieved and is ready to be read from UDR 

The semicolon in this case is used to tell the compiler that the "do nothing" (open braces) command is terminated.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But in that case, the semi-colon is redundant.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I always do explicit braces when there's an empty statement like

volatile BOOL haveInterrupt;
while ( !haveInterrupt )
  {};

just for clarity as compared to the more confusing

while (!haveInterrupt);

in whch case you have to look carefully to see the semicolon.

Some organizations (big) have coding style standards so everyone does it the same way out of sympathy for future code-readers in a team.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Visovian wrote:
Not always

if(PINB==0)
{
   PORTC=1;
   PORTA=2;
};          //error
else
   PORTC=0;

even 'better':

if (PINA == 0)
    if (PINB == 0)
    {
        PORTC = 1;
        PORTA = 2;
    };             // not a syntax error
    else
        PORTC = 0; // muwahahahaha