5 C functions for competitive programming

Written By : Prateek Bajpai

When talking about competitive programming, it can’t be argued that C++ is the holy grail of this field. But if you’re a fresher, chances are high that the first language you’ll learn to program is in C. I personally consider C to be the mother of all modern day programming languages, mainly because C++ is more or less completely based on C, and Python(yes, The Python programming language too!) is written in C, as well as some of its widely used packages. We are going to see 5 C functions for competitive programming that might help you.

Knowing some useful functions is always a great idea when programming in a competitive coding environment. Here I’ll mention some of the MOST USEFUL and lesser known functions in C that may help you up out there.

1.  C language’s in-built sorting function – qsort()

This function is present in the standard C library (stdlib.h). It is a handy implementation of the quicksort algorithm and will help you sort your arrays when the time is less.
It is defined in stdlib.h as:

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

base :  base address of the array to be sorted.
nitems : the length of the array.
size:  size of one array element.
compar : it is a function which is used by the qsort() to decide that how will the elements be sorted.

If we write it as follows:

int compar(int *a, int *b);

then, it can return two values – 1, if a should be placed before b in the sorted array, 0 otherwise.

Implementation of qsort()

int cmpfunc(int *a, int *b)
	if(*a > *b)
    	return 1;
	return 0;
int main(void)
	int a[] = {5,7,4,3,9,1,12,74,1,984,123,1,5,0,-5,-78,-694,54,556,87};
	int size_ar = sizeof(a)/sizeof(int);
	qsort(a, size_ar,  sizeof(int), cmpfunc);
	for(int i = 0 ; i < 20 ; i++)
        printf("%d ", a[i]);
            	return 0;

-694 -78 -5 0 1 1 1 3 4 5 5 7 9 12 54 74 87 123 556 984

2. Substring extraction using strncpy()

It is used to copy specified number of characters from source string into destination string. This is defined in the string.h header file as follows:

char *strncpy(char *dest, const char *src, size_t n);

dest : pointer to the destination string.
src  : string to be copied.
n : number of characters to be copied from source.

Implementation of strncpy()

int main()
            	char s[] = “www.askatul.com” ;
            	char d[15];
            	strncpy(d, s, 11);
            	return 0;


3. String to int type conversion – atoi()

It is used to convert string of numbers into integer number. It is defined in the stdlib.h header file as follows:

int atoi(const char *str);

str : string representation of the number.

Implementation of atoi()

int main()
            	char s[] = “92110”;
            	int n = atoi(s);
            	printf(“STR : %s %d”, s, sizeof(s));
		printf(“INT : %d %d”, n, sizeof(n));
            	return 0;

STR : 92110 6                   
INT :  92100 4

4. int to string type conversion – sprintf()

This is defined in the standard I/O library as follows:

int sprintf ( char * str, const char * format, ... );

Implementation of sprintf()

int main()
	char n[6];
	sprintf(n, "%d", 51213);  //if you pass %f instead of %d, you can to float to string type conversion
	printf("%s %d", n, sizeof(n));

51213 6

5. Splitting a string on some delimiter – strtok()

It splits the given string on some delimiter and creates tokens. It is defined in the string library (string.h) as follows:

char *strtok(char *str, const char *delim);

str : string to split
delim : the string on which the string is split

Implementation of strtok()

int main()
            	char s[] = “Have something in mind? AskAtul”;
		char *delim = “ “;
		char *chr = strtok(s, delim);
		while(chr != NULL)
			printf(“%s\n”, chr);
			chr = strtok(NULL, delim);
            	return 0;


Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *