Addition Muster

Reading time ~1 minute

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <inttypes.h>

#define equal(x,y) ((x==y) ? 0 : 1)

uint8_t wrong_input(int argc, const char *argv[]);

int main(int argc, char const *argv[])
{
    if (argc < 3)
    {
        printf("To few arguments.\n");
        return EXIT_FAILURE;
    }

    if (wrong_input(argc, argv))
    {
        printf("Only numbers allowed.\n");
        return EXIT_FAILURE;
    }

    int result = 0;

    for (size_t i = 0; i < argc-1; i++)
    {
        result += atoi(argv[i]);
    }

    printf("%d\n", equal(result, atoi(argv[argc-1])));

    return EXIT_SUCCESS;
}

uint8_t wrong_input(int argc, const char *argv[])
{
    size_t      j = 0;
    uint64_t    swtch = 0;

    for (size_t i = 1; i < argc; i++)
    {
        j = 0;
        while (argv[i][j])
        {
            if ((argv[i][j] < 0x30 || argv[i][j] > 0x39) && argv[i][j] != 0)
            {
                swtch++;
                j++;
                break;
            }
            j++;
        }
    }
    return equal(swtch, 0);
}