1

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);
     } 
2
  • 1
    The statement true; doesn't really do anything. And if you declare a function to return a non-void value, then you must have an explicit return statement. Commented Nov 24, 2023 at 18:22
  • Yes I know, I have inserted it for "exit" from the loop, I already tried this code and work I only want to convert it to an array because is too long Commented Nov 24, 2023 at 18:23

3 Answers 3

2

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

bool error(int a, int b, int c)
{ 
    int data[] = { a, b, c };
    size_t n = sizeof( data ) / sizeof( *data );

    size_t i = 0;

    while ( i < n && !( data[i] < 0 || data[i] > 255 ) ) i++;

    if ( i != n )
    {   
        printf("Number is over 255 or under 0, exit \n");
    }

    return i == n;
}

Though as the function is named error then it should return true when the numbers do not satisfy the condition that is it is better to rewrite the return statement like

return i != n;

Also 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

bool error(int a, int b, int c)
{ 
    int data[] = { a, b, c };
    size_t n = sizeof( data ) / sizeof( *data );

    size_t i = 0;

    while ( i < n && !( data[i] < 0 || data[i] > 255 ) ) i++;

    return i != n;
}

and in the caller you can write

if ( error( a, b, c ) )
{
    printf("Number is over 255 or under 0, exit \n");
}

If you want to use an array of values then the function can look the following way

bool error( const int data[], size_t n, int low, int high )
{ 
    size_t i = 0;

    while ( i < n && !( data[i] < low || data[i] > high ) ) i++;

    return i != n;
}

The values of low and high can be for example 1 and 255.

Sign up to request clarification or add additional context in comments.

3 Comments

Error in line size_t n = sizeof( data ) / sizeof( *data ), works with size_t n = sizeof( data ) / sizeof( *data );
Also this line cause me problem ` while ( i < n && !( data[i] <= 0 || data[i] > 255 ) ) i++;` while ` while ( i < n && !( data[i] < 0 || data[i] > 255 ) ) i++;` works fine
@elbarna There was a typo: a missed semicolon.
2

Simply iterate through the elements of the array with all the values:

#include <stdbool.h>

static inline bool is_in_range(int val, int start, int end) 
{
     return val >= start && val <= end;
} 

...
int array[] = {a, b, c};
  
for (size_t i = 0; i < sizeof array / sizeof *array; ++i) {
    if(!is_in_range(array[i], 0, 255)) {
        printf("Number is over 255 or under 0, exit \n");
        exit(1);
} 

Comments

1

I would keep the logic basically as-is, but try to combine it into a single condition:

void error(int a, int b, int c)
{
    if (a < 0 || b < 0 || c < 0 ||
        a > 255 || b > 255 || c > 255)
    {
        printf("Invalid input, one of the numbers is outside the range 0-255\n");
        exit(EXIT_FAILURE);
    }
}

Note that I have changed the return-type to void, because if the check fails then the program exits. If the function returns then the values are valid.

On a different note, the name error doesn't really describe what the function does. Perhaps something like verify_values_range would be better?

2 Comments

Thanks, is much short and better than mine, but you have to use a b c..a b c are only 3 vars and is ok, but my idea is another: is not possible for a lot of values an if-else statement using an array? So you have to write only once, for example, instead of use a b c d e f use a single array with values of a-f
@elbarna If you want an arbitrary number of arguments, then I suggest a vararg function instead, with the first argument being the number of values you pass. Then you check each value one by one in a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.