LinuxQuestions.org
Review your favorite Linux distribution.
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 04-29-2004, 12:12 PM   #1
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Rep: Reputation: 30
why gcc permit this code to compile? is there a reason?


should GCC 3.4 permit this code to COMPILE??
I don't understand why that can't be catched before compilation. It's a common typo
Code:
main()
{
    if(0);
    {
         printf("abc\n");
     }
}
./a.out
abc


what's the point in adding
{
}

when not really meaning something useful?

thanks

ps. Or at least one could just add a check in gcc that would say
if you have if and then ; {
then issue AT LEAST A WARNING!

thx
 
Old 04-29-2004, 12:15 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
b/c plenty of people do this:
Code:
while(do_some_shit) ;
 
Old 04-29-2004, 12:25 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Why should it though?
What if you just wanted to put some code in blocks or something...
like
{
char c = 'a';
somefunc(c);
}
printf("Blah");
{
int c=0;
c += 1;
}
 
Old 04-29-2004, 12:46 PM   #4
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
what's the point in adding brackets in your code if thereis no if/when/do or sth like it?
thank you


and infamous41md, I'm talking about

while(do_some_shit);
{
this_one_can_think_that_it_belongs-toWHILE
}

Last edited by zeppelin; 04-29-2004 at 12:47 PM.
 
Old 04-29-2004, 01:26 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Because while() needs to do something. And the way to make a while() loop without having it do anything outside the parentheses is by doing:

Code:
while(do_everything_useful_here)
  ;
Since whitespace doesn't matter in C, a semi-colon following immediately after while() is the same thing as what I wrote in the code block.

The reason for having braces that aren't part of a loop or condition statement is to localize a block of code. Like 95se showed, you can do this:

Code:
void my_func1(void)
{
  int a;

  a = 5;

  {
    int a;

    a = 3;
    printf("%d\n", a);
  }

  printf("%d\n", a);
}
This will print:
3
5

Anyway, it's perfectly legitimate C syntax and definitely has its purposes. I believe 'lint' will pick up on errors like this. You might want to look into that.
 
Old 04-29-2004, 02:55 PM   #6
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
itsme86 I was talking about while(do_something);{

ok {} can be used to localize vars. But i never saw them get used that way.
php will probally give a parse error
 
Old 04-29-2004, 03:28 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by zeppelin
what's the point in adding brackets in your code if thereis no if/when/do or sth like it?
thank you
Here's an example of where I often use brackets w/o having an if/while/for/etc... To me it just makes it more readable...

Code:
glBegin(GL_TRIANGLES);
{
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(0.0, 1.0, 0.0);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(-1.0, -1.0, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(1.0, -1.0, 0.0);
}
glEnd();
 
Old 04-29-2004, 04:02 PM   #8
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
deiussum, ok now that can make sense because C allows it

because you could easyily do it the way most people do it.
just ident those gl* lines.

matter of fact what you're doing doesn't remind C.
it would if it was
glBeing() //no ; here
{
abc
doremi
}
glEnd();

or am I wrong.

thank you itsme86. I'm trying splint
 
Old 04-29-2004, 08:29 PM   #9
duncanbojangles
Member
 
Registered: Jul 2003
Posts: 34

Rep: Reputation: 15
zeppelin, that's OpenGL code. glBegin() is a function indicating that you are about to be making primitve shapes. glEnd() says that you are done. This is what I usually do:
Code:
glBegin(GL_POINTS);
     glVertex3f(0.0, 1.0, 0.0);
     glVertex3f(1.0, 0.0, 0.0);
     glVertex3f(0.0, 0.0, 1.0);
glEnd();
 
Old 04-29-2004, 10:12 PM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I tend to use the brackets because when you are using editors that auto-indent, then it will automatically indent the next lines. Just a personal preference, though. And yes, you need the ; after glBegin() because it is a function call.
 
Old 04-30-2004, 04:54 AM   #11
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
i know glBegin() is a function call and I know that is OpenGL.
In fact I write GL code via the Tao Framework [C#]


I know that every function call has to end with ;
but as I ALREADY said you can ident rather than putting { }
and I added that this could look really C only if it was
glBegin() //mising ;
{
glSTH
}
glEnd();


ok. So you use them because some editors don't have auto-identation.
Well I sometimes wonder if that was the case when think allow everywhere the use of brackets. Only the local variable thing makes sense, but I never saw it being used, because it's bad habid to write such ugly code

Anyways, Thanks for contributing in my question. And i'm not attacking anyone, I just thought I had made clear what I meant.

splint is a good tool for C
www.splint.org
 
  


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
Strange GCC compile error with C code exvor Programming 16 08-23-2005 06:05 PM
Can gcc cross-compile C code to M68K Assembly? lowpro2k3 Programming 1 03-25-2005 07:50 AM
gcc warning compiling C code vose Programming 2 09-29-2004 10:58 AM
Return code from main() using gcc Meatwad Programming 13 01-27-2004 06:36 PM
For some reason I can't compile a .cpp file with kdevelop, the button is greyed out. Manyguns Programming 1 07-16-2001 10:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:11 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration