0
0
CHow-ToBeginner · 3 min read

How to Use qsort in C: Syntax, Example, and Tips

In C, use qsort from stdlib.h to sort arrays by providing the array pointer, number of elements, size of each element, and a comparison function. The comparison function defines the sorting order by returning negative, zero, or positive values.
📐

Syntax

The qsort function sorts an array with this syntax:

  • void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

base: pointer to the first element of the array.
nmemb: number of elements in the array.
size: size in bytes of each element.
compar: pointer to a function that compares two elements.

c
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
💻

Example

This example sorts an array of integers in ascending order using qsort. The comparison function returns the difference between two integers.

c
#include <stdio.h>
#include <stdlib.h>

int compare_ints(const void *a, const void *b) {
    int arg1 = *(const int *)a;
    int arg2 = *(const int *)b;
    return (arg1 > arg2) - (arg1 < arg2); // returns 1, 0, or -1
}

int main() {
    int numbers[] = {42, 23, 17, 13, 57, 9};
    size_t count = sizeof(numbers) / sizeof(numbers[0]);

    qsort(numbers, count, sizeof(int), compare_ints);

    for (size_t i = 0; i < count; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");
    return 0;
}
Output
9 13 17 23 42 57
⚠️

Common Pitfalls

Common mistakes when using qsort include:

  • Not writing the comparison function correctly, especially returning values other than negative, zero, or positive.
  • Passing incorrect sizes or counts, causing memory errors.
  • Using the wrong pointer types inside the comparison function.

Always cast the void * pointers to the correct type inside the comparison function.

c
#include <stdio.h>
#include <stdlib.h>

// Wrong comparison function: returns difference directly (may overflow)
int wrong_compare(const void *a, const void *b) {
    return *(const int *)a - *(const int *)b; // can cause overflow
}

// Correct comparison function
int correct_compare(const void *a, const void *b) {
    int arg1 = *(const int *)a;
    int arg2 = *(const int *)b;
    return (arg1 > arg2) - (arg1 < arg2);
}

int main() {
    int arr[] = {1000000000, 2000000000, -1000000000};
    size_t n = sizeof(arr) / sizeof(arr[0]);

    // Using wrong_compare may cause incorrect sorting due to overflow
    qsort(arr, n, sizeof(int), wrong_compare);
    for (size_t i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // Using correct_compare sorts properly
    qsort(arr, n, sizeof(int), correct_compare);
    for (size_t i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}
Output
1000000000 2000000000 -1000000000 -1000000000 1000000000 2000000000
📊

Quick Reference

Remember these tips when using qsort:

  • Always include <stdlib.h>.
  • Comparison function must return negative if first < second, zero if equal, positive if first > second.
  • Cast void * pointers to the correct type inside the comparison function.
  • Pass the correct element size and count.

Key Takeaways

Use qsort by passing the array pointer, element count, element size, and a comparison function.
The comparison function must return negative, zero, or positive to indicate order.
Always cast void pointers to the correct type inside the comparison function.
Avoid returning direct subtraction in comparison to prevent integer overflow.
Include to use qsort.