![]() |
|
|
|
|
1
14th April 03:04
External User
Posts: 1
|
I recently found out that I have no real understanding of the use of
semicolons in the C++ language. I know that a "statement" ends with a semicolon, but that only means that I don't understand what a "statement" is. I recently found out that I am still confused about this by looking at a function declaration and definition: int add(int iNumber1, int iNumber2) { return (iNumber1 + iNumber2); } //look, no semicolon I used to believe that there should be a semicolon after this last brace. But NO, you do not have to put a semicolon here. If I had decided to prototype my "add function" earlier in the code, I could proceed as follows int add( int iNumber1, int iNumber2); //Prototype declaration. Yes, semicolon needed here. //...some code.... //definition of function prototyped earlier on: int add(int iNumber1, int iNumber2) { return (iNumber1 + iNumber2); } //look, no semicolon My thinking used to be: In case I prototype a function, the semicolon doesn't have to show up in the definition because it showed up in the prototype declaration. But if I declare and define together, then the semicolon has to show up after the definition. My understanding of the semicolon was something like a "Do it!" or "Create!" symbol, something that has to happen for every entity created in the program at some time. But in my first example -which is apparently legal- the semicolon simply doesn't show up! So that theory goes out the window... What's the REAL definition of the semicolon? Thanks in advance, Sebastian. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
|
|
2
15th April 11:39
External User
Posts: 1
|
On 28 Nov 2004 18:54:12 -0500, sebastian@donnersmarck.com (Sebastian)
The semicolon is a "punctuator", a term not defined in great detail by the standard. The C++ grammar is based on the C grammar, and it is specified directly, not derived from rules. Furthermore, in the early days of C, it appears that it was developed in a rather ad-hoc manner, not by applying a pre-defined strategy. In many cases, semicolons are either required or not based whether their presence simplified coding of the compiler. In the early days of C, before it inherited prototypes from C++, function definitions included "identifier lists", for example looked like this: double some_func(x) int x; { return x * 2.0; } So you can see the problem is a function declaration did not end with a semicolon and was followed by an object definition. Likewise, the semicolon is required in places where a sequence up to that point could be complete, or could be part of a larger overall declaration or statement. Consider: struct fred { int x; long y }; main() { return 0; } Note that the 'implicit int' definition of the return type of main() is illegal in both C and C++ today, but many of the grammar rules date from days when it was not (at least in C). Minus the semicolon at then end of the structure definition, the source would define the function main() as returning a 'struct fred', not an int. What it all boils down to is that semicolons are required where the grammar specifies they are required, and this is not based on some overriding principle. Among the types of statements are 'expression-statement' and 'compound-statement'. An 'expression-statement' is defined as: expression(opt) ; ....that is an optional expression followed by a ';' (the expression is optional to allow for the null statement). A 'compound-statement' is defined as: { statement-seq (opt) } ....where 'statement-seq' is defined as: statement statement-seq statement A function declaration falls under the type 'simple-declaration', and the grammar specifies that such declarations require a terminating semicolon. A function definition ends with a function body, and a function body is defined as a compound statement, and a compound statement is defined as being terminated by the closing '}' and not by a semicolon. You can't really deduce the places where a semicolon is required or forbidden from any abstract first principle. You perhaps could if you tried to implement a compiler for the early version of C defined in the first edition of Kernighan & Ritchie's C Programming Language, based on the grammar included in that book. Of course, both C and itself and C++ much more so have extended the language and therefore the grammar since then. The grammar for ISO C++ in included in appendix A of ISO 14882, but it will not spell out the "whys". -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
3
15th April 11:40
External User
Posts: 1
|
The issue is not the meaning of semicolon. The semicolon is just punctuation.
The issue is you don't seem to understand the definition of a statement. Not all statements end in semicolons. Not all lines of C++ code are statements. Some are declarations. Prototype is a C term. They're just function declarations in C++. You'll just have to learn the grammer: What you have here is a function definition. It doesn't end in semicolon because the function definition grammer is (essentially) declarator comppund-statement Where compound-statemnt is a pair of braces with zero or more more statements inside. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
4
15th April 11:40
External User
Posts: 1
|
In article <b013ae1a.0411280754.4ba194d8@posting.google.com>, Sebastian
<sebastian@donnersmarck.com> writes A raised point over a comma :-) What is the real meaning of a full stop in English? In C++ ';' occurs in various parts of the grammar, mostly in clause 6 of the Standard which tells you what a statement is. -- Francis Glassborow ACCU Author of 'You Can Do It!' see http://www.spellen.org/youcandoit For project ideas and contributions: http://www.spellen.org/youcandoit/projects [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
5
18th April 22:03
External User
Posts: 1
|
Semicolon in C++ is the delimiter which separate statements. Other
computer languages use other delimiters, for example Basic uses New Line ( return and feed) as the delimiter. In C++, Directives such as #include use New Line as the delimiter. A good thing for Semicolon is that you can write a statement in multi lines. y = x*x *x + 3*x*x + 4*x + 5; But in the case of New Line, you have to write a special mark to show a continuity of a statement for multi lines. #define TOWN_NAME \ // \ shows continuity Kokubunji_shi {} brace is used for multi statements (a composite of statements) and form a scope, and need no following semicolon, probably because {} brace itself is a kind of delimiter. You cannot write {y = x + 1} but { y = x + 1; }. I think this is because the semicolon and {} have a deferent meaning: the semicolon is a sequence point for evaluation, and {} is a scope. "{};" is possible, but I advise you not to write the semicolon after {} in this case. However you have to write a semicolon after {} in the case of class or struct definition. class A{}; // If you drop the semicolon, a complier error will be issued. This is a heritage from C struct, which allow you a tag syntax , which is sometimes used in C++ conveniently. struct A{} tag; Therefore, "struct A{};" means no tag. I often forget to write the required semicolon after {}, and the semicolon is bothering for me to write, but I can easily find the end of a class or struct definition by the semicolon, even if it is long and complicated. Any correction is welcomed. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
6
4th May 01:39
External User
Posts: 1
|
INCORRECT! This is exactly the bogus thinking that lead to the
original poster's confusion. Semicolon, is the a syntactic punctuation that is required in declearations, and certain definitions and statemnts in C++. In many cases braces denote a compound statement, which by the language grammar has no semicolon. So yes, braces are "alternate punctuation" in this use. No, the reason the semicolon is present inside the braces is that a compound statement is a set of braces with zero or more statements inside. The semicolon here doesn't divide anything (nor is it a sequence point which is a completely distinct concept). It is the required end of the expression-statement (esseintially an expression followed by a semicolon is an expression-statement, perhaps the simplenst form of statement). It might be possible, but it would be WRONG in almost all cases. The ";" following a compound statement just makes another statement. It would only be allowed by the compiler in the case that another statement would be allowed there. if(condition1) { // compound statement }; This is just an if, followed by a null statement. if(condition1) { }; else foo(); would be ill-formed, as the ; inserts a statement between the if and the else and hence, the else isn't part of the if(). Here the braces aren't a compound statement, their part of a class definitions. These require the semicolon. Not writing the ";" here is bad and will lead to confusion as the language expects the next identifier encountered to be something that you want to declare of type A. You need the ; to terminate the class A definition/declaration. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
7
4th May 01:40
External User
Posts: 1
|
In article <49c1da0b.0411292013.128cf91c@posting.google.com>, Tokyo Tomy
<hosoda@jtec.or.jp> writes I don't think it is either good or bad, just a property of the way C++ handles whitespace (which includes new line characters) Ironically, by adding the comment you break the mechanism. The backslash bust come immediately before a newline character, nothing, not even a space is allowed between the two if it is to be a continuation character. It is because, in an appropriate context braces form a compound statement. No, a semicolon does not provide a sequence point though it often terminates a full expression, and it is that which has an implicit sequence point. But there are full expressions that are not expression statements and so do not have a following semicolon. There are also full expressions that are followed by a semicolon but are not expression statements. The various elements of a for loop contains several examples. And is sometimes an error according to the strict rules of the grammar (e.g. after a brace closing a namespace scope) Indeed it is an inheritance from C but your terminology is wrong. A tag is a name between struct, union or enum and the opening brace of the definition. Such names are type names in C++ but not in C (something that can be a catch). The point is that the inherited syntax allows the declaration of entities whose type is being defined to immediately follow the closing brace of the definition (and the same is true in C++). So struct X {int i; int j;} a, *a_ptr, foo(int i); though horrible, is a perfectly valid construct in both languages. The difference being that X is a typename in C++ but just a tag (that must be preceded by the appropriate keyword when used) in C. No it means that we are not declaring anything of type A at this point in the program. To declare such things later C++ aloows A a; But C requires (and C++ allows) struct A a; Yes, in general grep for }; which if you stick to necessary uses of semicolons will usually identify the end of a definition of a udt. I hope things are a little clearer now. -- Francis Glassborow ACCU Author of 'You Can Do It!' see http://www.spellen.org/youcandoit For project ideas and contributions: http://www.spellen.org/youcandoit/projects [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
|
|
8
4th September 11:13
External User
Posts: 1
|
In message <41ac7967$0$1634$9a6e19ea@news.newshosting.com>, Ron Natalie
<ron@sensor.com> writes <snip> The idea that semicolons are separators and that { .. } is not itself a statement appears to have become an urban myth. I've even seen it in a textbook written by Computer Science professors :-( Is there no cure for this? John -- John Harris [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
9
4th September 11:13
External User
Posts: 1
|
In C++, declarations are statements. For some strange reason, this is
not the case in C. More generally, of course, except for the preprocessor, lines don't mean a thing to the compiler. You can write your entire function in one line, even if it is a hundred statements, or you can write each token on a separate line. (Just don't ask me to maintain your code if you follow one of these policies. It's a good policy for the human reader to put just one statement per line.) As a general rule, I think it is safe to say that all statements end with either a ';', a '}' or contained statement. (I can't think of any exceptions off-hand.) Also, a statement will never end with more than one of these. Think about it for a minute: the grammar of a while statement is: `while' `(' condition `)' statement If a ';' were necessary to terminate a while statement, then you would need two ';' in: while ( x > 0 ) ++ x ; ; the first to terminate the expression statement ++ x, and the second to terminate the while. -- James Kanze GABI Software http://www.gabi-soft.fr Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
10
4th September 11:13
External User
Posts: 1
|
A semicolon in C++ is NOT a separator. It terminates (some)
statements. It does not separate anything. More generally, some languages use terminators, like C++; others like Pascal, use separators. Grammars using separators tend to look more elegant (IMHO), but result in more user errors, than grammars using terminators. The end of line is significant in the preprocessor, but not later. That's probably the justification. In fact, the grammar is such that it is easy to unambiguously locate the end of each statement. Both for the compiler and more importantly for the reader. Normally, a statement will end with a '}', a ';' or an embedded statement (which in turn ends with a '}', a ';' or an embedded statement, ad inifinitum). Because ';' is a terminator, not a separator. And the grammar of an expression statement requires a ';' as terminator. In Pascal, if you replace { and } by BEGIN and END, both forms are legal; the second defines a block with two statements: an expression statement and an empty statement. That's the difference between a separator and a terminator. (And I know: Pascal doesn't have expression statements, but assignment statements, and = is the comparison operator, you need := for assignment.) Technically speaking, the semicolon is NOT a sequence point. The end of a full expression is a sequence point. But since an expression cannot span several statements, and a semicolon always terminates a statement, there will always be a sequence point at a semicolon. Whether it is possible or not depends on the context. Consider: void f() {} // No semicolon allowed. struct C { void g() {} // Semicolon optional. } ; // Semicolon required. In general, a function definition is terminated by the closing }, a class definition no (because more may follow). Within a function, and empty statement is allowed, however (so extra semicolons may be tolerated), and the grammar explicitly allows an optional semicolon after the definition of a function in a class. And don't ask me why they didn't just allow an empty statment anywhere, and be done with it. That's because you can also write: class A {} someVariable ; The compiler has to know where to stop, and in this case, the closing } is not sufficient to indicate the end of a statement. Normally, the "tag" in the above would be A -- in your first line above, you have defined a class type named A and a variable of type A in the same line. -- James Kanze GABI Software http://www.gabi-soft.fr Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|