Consider I'm still a newbie with C, so I made a lot of confusion. This code works fine
int error(int a, int b, int c)
{
if (( a < 0 ) || (a > 255)) {
printf("Number is over 255 or under 0, exit \n");
exit(1); }
if ((a < 0)|| (a > 255)) {
printf("Number is over 255 or under 0, exit \n");
exit(1);
}
else if ((b < 0)|| (b > 255)) {
printf("Number is over 255 or under 0, exit \n");
exit(1);
}
else if ((c < 0)|| (c > 255)) {
printf("Number is over 255 or under 0, exit \n");
exit(1);
}
else {
true;
}
}
But is too long and I don't like it, I want to create an array which take the values of a b and c, and compare it with 255 or 0 (separately: for example a=256 failed, b=23 ok, c=33 ok), and if is over 255 or under 0, exit. I have tried this code but I have failed
int array[] = {a, b, c};
if( array[a] >= MAX_SIZE){
printf("Number is over 255 or under 0, exit \n");
exit(1);
}
For starters it is not a godd design when the program exits from the function.
Using an array your function can look the following way
Though as the function is named
errorthen it should returntruewhen the numbers do not satisfy the condition that is it is better to rewrite the return statement likeAlso pay attention to that the function should not output any message. It is the caller of the function based on the return value will decide whether to output a message. So I would define the function the following way
and in the caller you can write
If you want to use an array of values then the function can look the following way
The values of
lowandhighcan be for example1and255.