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.

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

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.
 

Seems like an input selector, based on hardware switch selector.

+++
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top