C qsort() function

C qsort() function - Sort a table of data

Syntax qsort() function

void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))

The qsort() function is used to sort an array of nitems elements, each of width bytes in size. The base pointer is a pointer to the array to be sorted. The original array is overwritten by the qsort() function.

Parameters qsort() function

Name Description Required /Optional
base Start of target array. Required
nitems Array size in elements. Required
size Element size in bytes. Required
compar The pointer to the user-supplied routine for comparing two array elements and returning a value specifying their relationship. Required

Return value from qsort()

  • This function does not return any value.

Qsort calls the compare routine one or more times during the sort process, passing pointers to two array elements each time. When compare indicates that two elements are the same, their order in the sorted array is unspecified.

Name Description
< 0 element1 less than element2
0 element1 equivalent to element2
> 0 element1 greater than element2

Example: qsort() function

The following example shows the usage of qsort() function.

#include <search.h>
#include <string.h>
#include <stdio.h>

int compare( char **arg1, char **arg2 )
   /* Compare all of both strings: */
   return _strcmpi( *arg1, *arg2 );

int main( void )
   char *colors[] = {"red", "green", "pink", "orange", "red", "white", "black", "blue"};
   char *key = "red";
   char *key1 = "yellow";
   char *key2 = "black";
   int i;
   printf("Original array elements:\n");
   for(int i = 0; i < sizeof(colors)/sizeof(colors[0]); i++) {
        printf("%s ", colors[i]);

   /* Sort using Quicksort algorithm: */
   qsort( (void *)colors, sizeof(colors)/sizeof(colors[0]), sizeof( char * ), (int (*)(const
   void*, const void*))compare );
   printf("Sorted array elements:\n");
   for( i = 0; i < sizeof(colors)/sizeof(colors[0]); ++i )    /* Output sorted list */
      printf( "%s ", colors[i] );


N.B.: This code is executed in windows 10


Original array elements:
red green pink orange red white black blue

Sorted array elements:
black blue green orange pink red red white

C Programming Code Editor:

Previous C Programming: C bsearch()
Next C Programming: C abs()

Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
    ptr = 0;// oops, I meant *ptr = 0

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q