Jump to content
Sign in to follow this  
chedzoy

If Within Case Bug

Recommended Posts

I was using 5.4, but have upgraded to 5.5.1. Bug exists in both.

 

This is a minimal code example to demonstrate..

 

#include <system.h>

main()
{
char i = 3;

switch (i)
{
 case 3:
 {
	 if (i == 3)
	 {
   break;
	 }
 }
}
}

I'm using the PIC16F876 as a target.

 

The error is bug.c(13) Error: Invalid 'break'

But the break is valid, and should break from the case clause.

 

I noticed that if you leave off the extra '{' '}' arround the case statment (which is valid C, but less readable) it works fine...

Regards

 

Chris.

Edited by chedzoy

Share this post


Link to post
Share on other sites
Guest Joe
I noticed that if you leave off the extra '{' '}' arround the case statment (which is valid C, but less readable)

Is it really valid (ANSI) C ?

 

I've never seen it written like that before, consider a situation that uses a drop-thru condition:

 

case x:
  expression;
  expression;
  expression;
// drop-thru:
case y:
  expression;
  expression;
  expression;
break;

case z:
  expression;
  expression;
  expression;
break;

 

case x:
{
  expression;
  expression;
  expression;
}
// drop-thru:
case y:
{
  expression;
  expression;
  expression;
break;
}
case z:
{
  expression;
  expression;
  expression;
break;
}

 

 

 

The latter just doesn't look correct, and i cant find it like that in any of the ANSI C books i have foating around.

Share this post


Link to post
Share on other sites

Hi Joe,

 

(This may be a duplicate, something went wrong just now!)

 

The extra brackets are valid, but they are purely asthetic. The switch statement works in exactly the normal way (break breaks from the switch statement, if no break is used then drop through to the next case statement).

 

I guess its just a 'style' thing. I think its more readable, but then thats me.

 

I've written thousands of lines of C using this, on various compilers under various operating systems (Unix, linux, DOS and Windows), many of which are running in commercial Telecoms applications.

 

I guess it just through me, if I compiled this style of case statement on any other C compiler I've used it will work fine.

 

Regards

 

Chris.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...