# what will be the output of c code

Status
Not open for further replies.

#### embed_con

##### Newbie level 1
hiii;
i have some code in that , read the sensor data on switch.
i think logic is ok but confusing about o/p.
tell me the output of this code.
Code:
struct sen
{
U8 s_id;
U8 sed;

}infor;

{
int id;
DS = DIP_SWITCH;

switch (DS)
{
case 0:
id = SENSOR1;
break;

case 1:
id = SENSOR2;
break;
case 2:
id = SENSOR3;

default:
id = -1;
break;
}

return id;
}
{

return curr_id;
}

##### Member level 3
i think you have done reverse, arguments have to be assigned to the variables and processed. but you are assigning some value of variables to the argument.so no use in argument.
If possible post your actual requirement

#### bigdogguru

hiii;
i have some code in that , read the sensor data on switch.
i think logic is ok but confusing about o/p.
tell me the output of this code.

Without a main() there will be no output, please post program in its entirety.

#### andre_luis

##### Super Moderator
Staff member
Seems like an input selector, based on hardware switch selector.

+++

#### bigdogguru

Hi Andre,

It would appear so.

However the Read_current_id() is rather perplexing:

Code:
int Read_current_id(int curr_id)
{

return curr_id;
}

What is the purpose of curr_id? Not much.

The following serves the same purpose:

Code:
int Read_current_id(void)
{

}

Maybe the intent was to pass the structure infor?

#### andre_luis

##### Super Moderator
Staff member
hi bigdogguru,

You´re absolutelly right.
I didn´t noticed that redundance.

Maybe the argument purpose was to define a default input parameter.
Due at Read_id funcion, the default statement at switch instruction performs that atribution, the sugested manner you posted, will not generate an unnused code assembled. However, in most compilers, that waste is warned and maybe it was the reason of the doubt in the thread.

+++

#### Iuri

##### Member level 2
Code:
	case 2:
id = SENSOR3;
//you have to put a break here
default:
id = -1;
break;
}

return id;
}

you forgot to put a break in case 2, if you dont when you come to case 2 you will set the id to SENSOR3 and also execute the default case and so on, until find the break line, setting id to -1.

you also have redundancy in your code, as bigdogguro told you.

Status
Not open for further replies.