How to display (printf) selective parameter based on conditions
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to display (printf) selective parameter based on conditions
Hi,
I am working on messaging quite a large amount of parameters.
I would like to simplify it and only display those parameters that are in active status.
For example,
I have 10 switches. (A1, A2, A3, ... ..., A9, A10)
In the message, I only display the label of the switches that are active and its status code (0-inactive, 1-active)
e.g. If only switch 1 and switch 8 is active.
The message I would like to display is as following:
A1_1, A8_1
The other will not appear until they are activated.
How can I do this in the function that is specially created to display these parameters. Other function will pass the parameter value to this function.
Currently, I only manage to display all the parameters which is a large amount of parameters and is not good to display all.
I am programming for limit switches.
By default, value of limit switches is 0. When it is activated, the value will change to 1.
Let say I have 10 limit switches right now. I denoted them as following:
Switch 1 S1_
Switch 2 S2_
Switch 3 S3_
Switch 4 S4_
Switch 5 S5_
Switch 6 S6_
Switch 7 S7_
Switch 8 S8_
Switch 9 S9_
Switch 10 S10_
After the labeling is to show the status 0 or 1.
However, I only want the whole thing appear when that switch's status is 1.
I means that when Switch 1, 5 and 8 is activated, the display screen will only display as following:
S1_1, S5_1, S8_1
Currently, all the switches will appear no matter what is the status that is
S1_1, S2_0, S3_0, S4_0, S5_1, S6_0, S7_0, S8_1, S9_0, S10_0
Ok the below code is probably not perfect but it should give you an idea. That what you do is have you have an char** and an integer value. If you want to know wether a switch you do bitwise and. So first value is 2**0, second is 2**1 and so forth. If its set the bitwise and returns 1 therefore you can operate on it. I decided for this to format the string so I copied it into a new char** holding just the values I need. Then i could process it at the end making the code simpler and easier to reuse. That would be my advice anyway if you can do it as then you wouldn't have 20 variables to worry about
Loop through the array, testing which are closed/open, and only print the ones of interest:
Code:
#define CLOSED 0
#define OPEN 1
int i;
for( i = 0; i < 10; i++ ){
if( switch[i] == CLOSED ){
printf( "S%d_%d\n", i, switch[i] );
}
}
You can be thrifty with your data and store the state of the switches as bits, as suggested by pgpython, but the extra code to display the bits will much more than make up for your saving.
To pgpython, the code is really advanced. I cannot understand how it works.
To theNbomr, we tried to think to put the switches as array before. But if we would do so, we need to change many parts in many source code files and it would be rather much more works and easily miss out something. Thus, we used the most fundamental one.
In the function that is to display the result after checking the switches, we put all switches one by one and add a condition for its display.
An example of the code is as follow:
if (S1==1)
{
strcat(buffer,",S1_");
sprintf(cS,"%d",S1);
strcat(buffer,cS);
}
I would urge you to at least adopt the idea of the #defines that theNbomr had in the sample code, that is:
Code:
#define INACTIVE 0
#define ACTIVE 1
And use ACTIVE rather than 1 in your code. I will make it so much more readable,particularly in three months time when you need to look at the code again.
Code:
if (S1==ACTIVE)
{
strcat(buffer,",S1_");
sprintf(cS,"%d",S1);
strcat(buffer,cS);
}
With regards to arrays. What will happen if you ever have to add an additional switch (or two or three or ...). All over the show you have to check the code and copy blocks of code. Forget one and the compiler will probably not warn you because the code is valid.
The simple example that you gave (modified to add a few more switches)
Code:
if (S1==1)
{
strcat(buffer,",S1_");
sprintf(cS,"%d",S1);
strcat(buffer,cS);
}
if (S1==1)
{
strcat(buffer,",S2_");
sprintf(cS,"%d",S2);
strcat(buffer,cS);
}
...
...
if (S10==1)
{
strcat(buffer,",S10_");
sprintf(cS,"%d",S10);
strcat(buffer,cS);
}
As you can see, I already forgot to change the second S1 to S2 in an if statement during a copy/paste operation.
Your code is simply not maintainable. So you have to suffer once to get it maintainable. How many source files are we talking about?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.