what will be the output of c code

Status
Not open for further replies.

embed_con

Newbie level 1
Joined
Jun 6, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,290
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 Read_id(int DS)
{
	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;
}
int Read_current_id(int curr_id)
{
	curr_id =  Read_id(infor.s_id);
   
	return curr_id; 
}
 

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
 

Hi Andre,

It would appear so.

However the Read_current_id() is rather perplexing:

Code:
int Read_current_id(int curr_id)
{
	curr_id =  Read_id(infor.s_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)
{
	return  Read_id(infor.s_id);
    
}

Maybe the intent was to pass the structure infor?
 

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.

+++
 

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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…