Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Basic programing doubts in C?

Status
Not open for further replies.

mImoto

Full Member level 4
Full Member level 4
Joined
Feb 21, 2002
Messages
210
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,733
When I place a return inside a case statement like:
..
switch(NODE)
{
CASE 'a': ....
return(something);
break;
CASE 'b': -...

....

Does the return go out of the switch structure or just of the Case, or is it the break executed after the return?


And another one:

If I use typedefs is possible:

typedef unsigned har tByte;

and then in a program.

static BYTE example;

Best regards,

mimoto
 

dainis

Advanced Member level 4
Advanced Member level 4
Joined
May 15, 2001
Messages
1,125
Helped
53
Reputation
106
Reaction score
9
Trophy points
1,318
Activity points
9,268
Return - returned from function ( where SWITCH CASE inside).

It is normal ...
typedef unsigned char BYTE;
BYTE a,b,c;
 

junglejenny

Member level 4
Member level 4
Joined
Nov 25, 2001
Messages
72
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
572
It is not good programming to put a return in the middle of the switch.

Different compilers behave differently an whilst the better established ones on larger processors generally work better, it would be better if lend a hand to the compiler. The problem you encounter is probabely the compiler does not clean up the stack properly when it encounters the return where you've put it.

Put the return thus:

Switch
{


}

return;


JJ
 

dainis

Advanced Member level 4
Advanced Member level 4
Joined
May 15, 2001
Messages
1,125
Helped
53
Reputation
106
Reaction score
9
Trophy points
1,318
Activity points
9,268
This is not usable compiller if it not correctly handle return from SWITCH:CASE ......
Maybe this is not good programming style, but it must working.
This construction allow make programm smaller and faster, because
you do not nead temp variable, where storing returned value.
Also this is normal case for parameter range checking and returning error codes ...
 

mImoto

Full Member level 4
Full Member level 4
Joined
Feb 21, 2002
Messages
210
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,733
Thanks I will change, I will use a temp variable and then a return at the end.
 

dainis

Advanced Member level 4
Advanced Member level 4
Joined
May 15, 2001
Messages
1,125
Helped
53
Reputation
106
Reaction score
9
Trophy points
1,318
Activity points
9,268
You do not nead change this code, is it ok ....
 

btbass

Advanced Member level 5
Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,896
Helped
438
Reputation
880
Reaction score
288
Trophy points
1,363
Location
Oberon
Activity points
12,887
You don't need the break, it will never get there! A return in a switch is quite ok, no problem, returns from the function.
 

cheolim

Full Member level 1
Full Member level 1
Joined
Dec 3, 2002
Messages
97
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
873
Hello, mImoto

It's just a very small tips C-programming.
Maybe it's useful to embeded microcontroller programmings.
Please consider using a global variable in your program like belows.
============================================
unsinged char something;

void main(void)
{

... here you can use variable of something.
...

switch(NODE)
{
CASE 'a': ....
return(something);
break;
CASE 'b': -...

....
}
...
}
============================================

In this case, one things you sould consider carefully.
That's where is changes global variable and its sequences.

Good luck.
:roll:
 

btbass

Advanced Member level 5
Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,896
Helped
438
Reputation
880
Reaction score
288
Trophy points
1,363
Location
Oberon
Activity points
12,887
Globals are not so good.
Better,

int myfunct(int); // function prototype

int main(void)
{
int what, NODE = 2;

what = myfunc(NODE);
return 0;
}

int myfunc(int NODE)
{
int result = 0;

switch(NODE)
{

case 1: result = something;
break;
case 2:
case 3: break;
default: result = somethingElse
return result;
}
return result;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top