Quick C related question.

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

I did a few searches but I'd just like to quickly confirm with the Guru's here:

Is there EVER a time where a semicolon ';' is put after a closing brace '}' ?

I ask only because I use CodeVision AVR and it has some "library" statement prototypes that you can click on to have pasted into your code. Some of these have semicolons after the closing brace. This is confusing me. I've always thought the closing brace was sufficient.

Go electric!
Happy electric car owner / builder

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

sgomes wrote:
I did a few searches but I'd just like to quickly confirm with the Guru's here:

Is there EVER a time where a semicolon ';' is put after a closing brace '}' ?

I ask only because I use CodeVision AVR and it has some "library" statement prototypes that you can click on to have pasted into your code. Some of these have semicolons after the closing brace. This is confusing me. I've always thought the closing brace was sufficient.

yes. When declaring a struct or union, or an initialized array.

unsigned char my_array[4] = {1,2,3,4};

struct my_struct {
    unsigned char a,b;
};

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

A colon after a brace doesn't hurt, but it is superfluous. I might be a hold over from a very old C style.

Edit: I guess your right, glitch. I hadn't considered those cases.

Regards,
Steve A.

The Board helps those that help themselves.

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

It always pays to ask even the dumbest of questions! Thanks!

Wouldn't the semicolon in this code cause a "no corresponding IF" error during compile?

if(a==b) {
    do stuff
    };
else {
    do other stuff
    }

In other words the semicolon de-couples the 'IF" from the "ELSE" . I could be wrong.... it's been known to happen... :lol:

Go electric!
Happy electric car owner / builder

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

Yes, sgomes. The compiler should issue an error as else is no valid start of any C statement.

AVR-GCC says this:

../main.c:17: error: parse error before "else"

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

In 'C', the semicolon is a statement terminator

Also, a null statement - eg, just a semicolon - is legal in 'C'

Thus you can put as many semicolons as you like anywhere that any statement would be allowed - but no statements are allowed between an 'if' clause and its 'else' clause.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well said!

Go electric!
Happy electric car owner / builder