Jump to content
  • Advertisement
Sign in to follow this  
Phantoms

Question about semicolon

This topic is 3916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In the following simple program, can someone tell me why a ";" is required after the struct statement, but not the void one, why it doesn't matter if it is or isn't placed after the void section and would it be good practice to put it after every section's closing "}"?
#include <iostream>
#include <string>
using namespace std;

struct Wizard {
    string name;
    int age;
};

void wizard_print(Wizard wizard) {
    cout << wizard.name << " is " << wizard.age << " years old.\n";
}

int main() {
    Wizard gandalf, sauron, saruman;

    gandalf.name = "Gandalf";
    gandalf.age = 123;

    sauron.name = "Sauron";
    sauron.age = 234;

    saruman.name = "Saruman";
    saruman.age = 345;

    wizard_print(gandalf);
    wizard_print(sauron);
    wizard_print(saruman);
}


Share this post


Link to post
Share on other sites
Advertisement
When you define a struct or class you can optionally define a variable of that type at the same time. Ex:

struct Foo {
// stuff
} foo;

This, at the same time, defines the struct Foo and defines an object of type Foo named foo. Thus, the semi-colon after the struct definition says that you aren't going to define any variables.

Share this post


Link to post
Share on other sites
Quote:
Original post by Phantoms
In the following simple program, can someone tell me why a ";" is required after the struct statement, but not the void one, why it doesn't matter if it is or isn't placed after the void section and would it be good practice to put it after every section's closing "}"?

*** Source Snippet Removed ***
I don't know the answer to your question for sure, but I feel comfortable making an educated guess...

In C and C++ (and some other languages as well), a semicolon is used to mark the end of a statement of some sort. Syntax such as:
;;;
Is legal and will compile correctly; it is parsed as three empty statements.

This means, more or less, that you can place an extra semicolon or semicolons after any syntactically complete block of code, and they will have no effect. An example of this would be a semicolon after a curly-bracketed block of code (such as a function, or a block of code associated with a control structure).

These semicolons are harmless, but are also superfluous and should not be included in your code.

I'm not sure why the the semicolon after class definitions is required, but my guess is that it's because you can declare a class (or struct or union) and a variable of that type in the same statement:
struct MyStruct { int x; } my_struct;
So, the semicolon lets the compiler know whether you are or are not also going to declare a variable of the specified type.

Share this post


Link to post
Share on other sites
Thanks for clearing it up a little.

So only use it when it's required. It's required in a struct or class as a variable of that type can be declared at the same time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!