comparing version numbers in c

I really wonder why people strive for such complicated solutions when there is sscanf in C. Here is a very simple solution to that problem that will work for 99% of all use cases:

int compVersions ( const char * version1, const char * version2 ) {
    unsigned major1 = 0, minor1 = 0, bugfix1 = 0;
    unsigned major2 = 0, minor2 = 0, bugfix2 = 0;
    sscanf(version1, "%u.%u.%u", &major1, &minor1, &bugfix1);
    sscanf(version2, "%u.%u.%u", &major2, &minor2, &bugfix2);
    if (major1 < major2) return -1;
    if (major1 > major2) return 1;
    if (minor1 < minor2) return -1;
    if (minor1 > minor2) return 1;
    if (bugfix1 < bugfix2) return -1;
    if (bugfix1 > bugfix2) return 1;
    return 0;
}

Here, give it a try: https://ideone.com/bxCjsb


I know I wont be able to tokenize 2 strings at once.

Fortunately, you do not need to: make a function that takes a string, and parses it for three integer numbers using strtok_r (use a reentrant version, it's a lot safer).

strunct version_t {
    int major;
    int minor;
    int build;
};

version_t parse_ver(const char* version_str) {
    version_t res;
    // Use strtok_r to split the string, and atoi to convert tokens to ints
    return res;
}

Now you can call parse_ver twice, get two version_t values, and compare them side-by-side.

P.S. If you adopt a convention to always pad the numbers with leading zeros to a specific length, i.e. make sure that you write "141.1.03" and not "141.1.3", you could substitute integer comparison with lexicographic one.


The following routine compares version-number strings that are made up of genuine numbers. The advantage is that the delimiter does not matter; it will work with, for example, 141.01.03, 141:1:3, or even 141A1P3. It also handles mismatched tails so that 141.1.3 will come before 141.1.3.1.

#include <assert.h>
#include <stdlib.h>

int versionCmp( char *pc1, char *pc2)
{
    int result = 0;
    /* loop through each level of the version string */
    while (result == 0) {
        /* extract leading version numbers */
        char* tail1;
        char* tail2;
        unsigned long ver1 = strtoul( pc1, &tail1, 10 );
        unsigned long ver2 = strtoul( pc2, &tail2, 10 );
        /* if numbers differ, then set the result */
        if (ver1 < ver2)
            result = -1;
        else if (ver1 > ver2)
            result = +1;
        else {
            /* if numbers are the same, go to next level */
            pc1 = tail1;
            pc2 = tail2;
            /* if we reach the end of both, then they are identical */
            if (*pc1 == '\0' && *pc2 == '\0')
                break;
            /* if we reach the end of one only, it is the smaller */
            else if (*pc1 == '\0')
                result = -1;
            else if (*pc2 == '\0')
                result = +1;
            /*  not at end ... so far they match so keep going */
            else {
                pc1++;
                pc2++;
            }
        }
    }
    return result;
}

int main( void )
{
    assert(versionCmp("1.2.3" , "1.2.3" ) == 0);
    assert(versionCmp("1.2.3" , "1.2.4" )  < 0);
    assert(versionCmp("1.2.4" , "1.2.3" )  > 0);
    assert(versionCmp("10.2.4", "9.2.3" )  > 0);
    assert(versionCmp("9.2.4",  "10.2.3")  < 0);
    /* Trailing 0 ignored. */
    assert(versionCmp("01", "1") == 0);
    /* Any single space delimiter is OK. */
    assert(versionCmp("1a2", "1b2") == 0);
    return EXIT_SUCCESS;
}

Replace the strtouls with strcspns and a strncmp, and you can use it to compare non-numeric version "numbers" -- but the delimiter must be a dot. For example, 141.3A.1 sorts before 141.3B.

...
while (result == 0) {
    /* ignore leading zeroes */
    pc1 += strspn( pc1, "0" );
    pc2 += strspn( pc2, "0" );
    /* extract leading version strings */
    int len1 = strcspn( pc1, "." );
    int len2 = strcspn( pc2, "." );
    /* if one is shorter than the other, it is the smaller version */
    result = len1 - len2;
    /* if the same length then compare as strings */
    if (result == 0)
        result = strncmp( pc1, pc2, len1 );
    if (result == 0) {
        pc1 += len1;
        pc2 += len2;
        if (*pc1 == '\0' && *pc == '\0')
            ...