Program to tell which if any of three numbers is in the middle I wrote this program, and it does what it asks, but it's so big and ugly. What can I change?

Some examples:


a  b  c ---&gt;
5  1  12   a is between b and c
5  12 1    a is between c and b
5  5  2    there is no gap



This was in an example of a test that is supposed to be small, so that's why I'm asking if there is another way.

#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;stdlib.h&gt;

int main (void){
int a,b,c;

printf("Inserir 3 números: "); //its in portuguese, insert 3 numbers
scanf("%i %i %i",&amp;a,&amp;b,&amp;c);

if(a==b||b==c||a==b){
    printf("não há intervalo\n"); //there is no gap
    exit(0);
}

int maior, meio, menor,flag,flag2,flag3;

maior=a; //maior means biggest
flag=1;
if(maior&lt;b){
    maior=b;
    flag=2;
}if(maior&lt;c){
    flag=3;
    maior=c;
}

if(maior!=a){
    meio=a;  //meio means middle
    flag2=1;
    if(meio&lt;b&amp;&amp;b&lt;maior){
        meio=b;
        flag2=2;
    }if(meio&lt;c&amp;&amp;c&lt;maior){
        flag2=3;
    }
}else{
    meio=b;
    flag2=2;
    if(meio&lt;c&amp;&amp;c&lt;maior){
        flag2=3;
    }
}

menor=a; //menor means smallest
flag3=1;
if(menor&gt;b){
    menor=b;
    flag3=2;
}if(menor&gt;c){
    flag3=3;        
}

if(flag2==1)
    printf("o a entre [");
if(flag2==2)
    printf("o b entre [");
if(flag2==3)
    printf("o c entre [");

if(flag3==1)
    printf("a e ");
if(flag3==2)
    printf("b e ");
if(flag3==3)
    printf("c e ");

if(flag==1)
    printf("a]\n");
if(flag==2)
    printf("b]\n");
if(flag==3)
    printf("c]\n");


return 0;
}
Well, first of all, consider adopting one of the myriad common indentation-styles and use it consistently.
Doing so will make your code far easier to read and write.
You should test whether you really got three numbers.
Currently, you don't, which leads to UB.
If in doubt, sort.

That eliminates an astonishing number of problems.
You aren't using anything from &lt;math.h&gt;, so don't include it.
exit(0); and return 0; have nearly the same effect when done in main.
The only differences are when main is called recursively, and we won't do such an abomination outside the IOCCC. (Or if we change to a different language, like C++, but who would do that?)




#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

typedef struct namednumber {int num; char name; } namednumber;

static int comp_namednumber(const void* pa, const void* pb) {
    int a = ((namednumber*)pa)-&gt;num,
        b = ((namednumber*)pb)-&gt;num;
    return (a &gt; b) - (a &lt; b);
}

int main() { // No need for void in the parameter-list, this is a definition
    int a, b, c;
    printf("Please enter three numbers:\n");
    if(3 != scanf("%i %i %i", &amp;a, &amp;b, &amp;c)) {
        printf("You didn't enter three numbers. Aborting.\n");
        return EXIT_FAILURE;
    }

    if(a == b || b == c || c == a) {
        printf("There is no gap.");
        return 0;
    }

    namednumber data[] = {{a,'a'},{b,'b'},{c,'c'}};
    qsort(data, 3, sizeof *data, comp_namednumber);
    printf("%c is between %c and %c\n", data[1].name, data[0].name, data[2].name);

    return 0; // This line is implicit in C99+
}


Actually, getting out qsort is severe overkill, so here with a standard sorting-network:

#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

static void sort2(int* a, char* na, int* b, char* nb) {
    if(*a &lt; *b) return;
    int t = *a;
    *a = *b;
    *b = t;
    char nt = *na;
    *na = *nb;
    *nb = nt;
}

int main() {
    int a, b, c;
    printf("Please enter three numbers:\n");
    if(3 != scanf("%i %i %i", &amp;a, &amp;b, &amp;c)) {
        printf("You didn't enter three numbers. Aborting.\n");
        return EXIT_FAILURE;
    }

    if(a == b || b == c || c == a) {
        printf("There is no gap.");
        return 0;
    }

    char na = 'a', nb = 'b', nc = 'c';
    sort2(&amp;a, &amp;na, &amp;b, &amp;nb);
    sort2(&amp;a, &amp;na, &amp;c, &amp;nc);
    sort2(&amp;b, &amp;nb, &amp;c, &amp;nc);
    printf("%c is between %c and %c\n", nb, na, nc);
}