Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

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

e shade

Member level 3
Joined
Dec 31, 2010
Messages
59
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Activity points
1,581
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

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,508
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_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,200
Helped
1,142
Reputation
2,303
Reaction score
1,119
Trophy points
1,403
Location
Brazil
Activity points
53,585
Seems like an input selector, based on hardware switch selector.

+++
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,508
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?
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,200
Helped
1,142
Reputation
2,303
Reaction score
1,119
Trophy points
1,403
Location
Brazil
Activity points
53,585
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
Joined
May 14, 2011
Messages
50
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Activity points
1,630
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

Top