ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
root@qa73 ~# gcc -o test test.c
test.c:5: warning: no semicolon at end of struct or union
root@qa73 ~# cat test.c
#include
struct test {
int a;
char b
};
int main()
{
struct test t1;
printf(" %d %c ",t1.a,t1.b);
return 0;
}
root@qa73 ~# ./test
10476704 ¼ root@qa73 ~#
Hi, PLEASE notice, there is not semicolon after char b in declaration of "test" structure. Yet GCC doesnot give any compile time error?
WHy is it so? Do not counter question for gcc version ... It's RedHAt 4.0.0
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783
Rep:
Quote:
Originally Posted by vkmgeek
Hi, PLEASE notice, there is not semicolon after char b in declaration of "test" structure. Yet GCC doesnot give any compile time error?
Why should it ? It already gave a warning about it and this missing last colon doesn't prevent the compiler to understand the code.
Use the gcc -Werror option to have this warning abort compilation.
Of course, as already stated, gcc should have failed because of the bogus include.
Finally, don't compile or do anything as root unless there is no other choice.
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783
Rep:
Quote:
Originally Posted by vkmgeek
This is a parsing error which should be reflected at compile time in any case...
There is no parsing error but a code error. Please clarify.
Quote:
Please be clear....
The compiler detects the error and is trying to be helpful by accepting the code as is.
Despite the missing semicolon, there is no doubt about what the code should have been.
If it is not parsing error and compiler is trying to be helpful. Then it is giving me the "wrong warning". It gives warning as "no semicolon at end of struct" which is wrong... Semicolon is there at the end but semicolon is not at the end of one last variable declaration. So, it parses wrong...
HOwever, what if write
#include <stdio.h>
void main()
{
int i
}
It gives parsing error. It should not in this case also.
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783
Rep:
Quote:
Originally Posted by vkmgeek
If it is not parsing error and compiler is trying to be helpful. Then it is giving me the "wrong warning". It gives warning as "no semicolon at end of struct" which is wrong... Semicolon is there at the end but semicolon is not at the end of one last variable declaration. So, it parses wrong...
You are misinterpreting the warning message.
The compiler is complaining about the missing semicolon after the last structure field. It hasn't parsed yet the semicolon after the closing brace, which is by the way mandatory.
Quote:
HOwever, what if write
#include <stdio.h>
void main()
{
int i
}
It gives parsing error. It should not in this case also.
This has nothing to do with your previous example, a function block is not a structure.
Its not an error what the compiler said, just a convention in error handling .
Basically the compiler see this
char b};
And theres mulitude of errors it could throw back.
Invalid identifier, expected semicolon after b, no semicolon at end of struct or union
It throws errors based on the order of the keyword. Theres two key words that expect ';' at the end of statement.
struct came before the char variable. so the compiler gives an error message related to the struct keyword. which is true. There really is no semilcolon at the the end of struct. because }; denotes the end of a struct. Not char b};.
If it is not parsing error and compiler is trying to be helpful. Then it is giving me the "wrong warning". It gives warning as "no semicolon at end of struct" which is wrong... Semicolon is there at the end but semicolon is not at the end of one last variable declaration. So, it parses wrong...
Compilers are somewhat complicated tools. <satire>If you see a problem with this behaviour, then I suggest you write a patch for it and submit it to the GCC team.</satire>
Last edited by taylor_venable; 11-05-2006 at 11:02 AM.
Reason: Satire doesn't carry well in virtual space.
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783
Rep:
It's hard to write a patch for a problem that doesn't exist.
The compiler is correct in telling there is a missing semicolon on line 5 at the end of the structure.
The fact another semicolon is also present in this very same line but outside of the structure doesn't change anything.
Just to put things a bit clear... In C, any instruction should be terminated by a semicolon. The semicolon is not mandatory for block structures ( i.e {} and all between it). Notice also that the instructions within a block follow the normal rule again.
For that reason :
- a complain about no semicolon in line 5 ("c:5" in the message) is logical.
- semicolon after } is optional and treated as an empty statement. Every string, ending with a semicolon is treated as statement by the parser.
- The fact that the "}" separates the two statements is not to the point : the parser doesn't know anything about separators.
- patching this behaviour would violate the standard for C and for C++
I hope I helped to clarify out something. Enjoy programming !!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.