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

Return - returned from function ( where SWITCH CASE inside).

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

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
 

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

Thanks I will change, I will use a temp variable and then a return at the end.
 

You do not nead change this code, is it ok ....
 

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.
 

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:
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top