LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-04-2006, 02:12 AM   #1
vkmgeek
Member
 
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185
Blog Entries: 2

Rep: Reputation: 31
Is it GCC bug?


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

Can someone shed light on it.......please?

Thanks,
 
Old 11-04-2006, 02:15 AM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
this compiles even with nothing after the #include?
some weird code
 
Old 11-04-2006, 02:39 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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.
 
Old 11-04-2006, 02:46 AM   #4
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143
Blog Entries: 8

Rep: Reputation: 15
It's looking for the semicolon .It doesn't matter if you put it on the 4th line of the 100th line as long as it's the next thing it sees.

99 posts hehe.
 
Old 11-04-2006, 03:25 AM   #5
vkmgeek
Member
 
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185
Blog Entries: 2

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by cyb0rg777
It's looking for the semicolon .It doesn't matter...
Well I know that. But you see that there is a "}" in between....

One more thing #include is proper... It was just tyoe error.... I just wanted to concetrate n below code fragment only...


Quote:
Why should it ?
what does it mean? This is a parsing error which should be reflected at compile time in any case...

Please be clear....
 
Old 11-04-2006, 04:51 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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.
 
Old 11-04-2006, 05:04 AM   #7
vkmgeek
Member
 
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185
Blog Entries: 2

Original Poster
Rep: Reputation: 31
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.
 
Old 11-04-2006, 06:40 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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.
 
Old 11-04-2006, 08:54 AM   #9
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
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};.
 
Old 11-04-2006, 09:02 AM   #10
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
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...
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 12:02 PM. Reason: Satire doesn't carry well in virtual space.
 
Old 11-05-2006, 02:54 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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.
 
Old 11-05-2006, 08:50 AM   #12
ronald-be
Member
 
Registered: Aug 2004
Location: Belgium
Distribution: debian 5.02
Posts: 73

Rep: Reputation: 15
meaning of a semicolon in C

Hello all,

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 !!!

Greetings,

Ronald
 
Old 11-05-2006, 09:06 AM   #13
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Quote:
- semicolon after } is optional and treated as an empty statement. Every string, ending with a semicolon is treated as statement by the parser.
No it isn't. A semi-colon is mandatory after a structure definition, as I believe jlliagre has already pointed out.

Last edited by soggycornflake; 11-05-2006 at 09:08 AM.
 
Old 11-05-2006, 09:24 AM   #14
ronald-be
Member
 
Registered: Aug 2004
Location: Belgium
Distribution: debian 5.02
Posts: 73

Rep: Reputation: 15
A semi-colon is mandatory after a structure definition

Checked my own code... It seems to be so, yes... Sorry for the mistake. Not mandatory for functions although

Greetings,

ronald
 
Old 11-05-2006, 09:53 AM   #15
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Rep: Reputation: 46
Hi,
but I still dont get why wouldn't this give an error?
I mean "expected ; at line 4" should be error,

Last edited by duryodhan; 11-05-2006 at 10:01 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bug reports on glibc or gcc tvburger Linux From Scratch 1 12-06-2004 01:43 AM
gcc wont install, 'failed dependencies: glibc-devel is needed by gcc-3.3.3-41' TdlSnare Suse/Novell 3 11-29-2004 03:13 PM
Free86 bug or nVidia bug?? ProtoformX Linux - Software 2 05-12-2004 03:38 AM
Extremely strange bug in gcc/g++ Maidros Linux - Software 3 05-08-2004 07:28 AM
gcc bug? fab12 Programming 2 02-07-2004 08:44 AM


All times are GMT -5. The time now is 11:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration