This post is part of a series of posts where I want to document a bunch of C functions I use for my projects that I have written, collected and modified over the past few years. One of the functions that I use regularly is a string replacement function like the str_replace in PHP. This function replaces all occurences of a particular substring with a replacement pattern. The code for this was borrowed from here. In the original code, a newly allocated string is returned after all the substrings are replaced. I have modified it so that the replacements are made in place.Continue reading

A lot of programs require you to pass sensitive information like passwords etc as arguments to the program. However if you pass passwords as arguments, it will be visible through the /proc filesystem or the ps ax output while the program is in execution. To avoid the possibility of anybody prying on sensitive information, programmers should modify the memory location where the input parameters are stored (argv array), so that it is not visible to any other users, who might have the access levels to see what processes you’re running. Jotted below is some sample code which modifies its input parameters to hide it from the proc file system.Continue reading

I recently worked with a client who wanted a Bluetooth RFCOMM client-server application. The server would sit on an android phone, while the client will run on an embedded linux device. The rfcomm client portion was just sample code that he wanted to base his implementation on. I wrote a quick app for the same. The code you are about to see is ugly and can be rewritten to look more pleasing to the eye. Also includes a horrible goto hack. But it works and can be used to base rfcomm implementations on.Continue reading

A friend posted this question on my college mailing list.

How do we find if a Number is a Palindrome?

Note: No conversion to string. And also, no traditional method of dividing the number to construct the reversed number and then compare.

Here is how you go about it.

  1. Lets say the number is 1987891 (odd number of digits)
  2. Reduce it to 198891. If the number were 19877891 (even number of digits) don’t change it (basically change it to a number with even number of digits, by excluding the middle digit)
  3. Now Split it into 198 and 891
  4. If ((891-198)%9 == 0 and (891*1000+198)%11 == 0 and (198*1000+891)%11 == 0) then its a palindrome

Sample C Code

#include <stdio.h>

int numdigits(int n) {
    int count = 0;
    while(n != 0)   {
        n /= 10;
    return count;

int main(void) {
    int n;
    int l;
    int a,x;
    int n1, n2, n3;
    l=numdigits(n);     /* Find the number of digits */
    if (((n1-n2)%9 == 0) && ((n2*a+n1)%11 == 0) && ((n1*a+n2)%11 == 0)) {
        return 0;
    return 1;

To compile it

gcc -o palindrome palindrome.c

You can verify that above code works using the below script.


for (( i = 0; i < 100000; i++ )); do
  echo $i | ./palindrome

The script assumes that the palindrome binary is in the present working directory.

Jotted this down for school students scouring the web for their homework answers: 😉

There are multiple ways of creating unique temporary files in C, namely tempnam, tmpfile, mkstemp. mkstemp and tmpfile are more secure than tempnam. mkstemp additionally lets you specificy a template for the file name and the directory in which these temporary files should be created. Here’s a code snippet which uses mkstemp to create temporary files in the /tmp directory.

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

#define TEMPLATE_NAME "CLIXXXXXX"       /*Template Name*/
#define TEMPLATE_DIR "/tmp"             /*Directory*/

int main (void) {
    char *template;
    int templateSize, dirSize;
    int fd;

    templateSize  = strlen( (const char *) TEMPLATE_NAME );
    dirSize =  strlen( (const char *) TEMPLATE_DIR );
    template = (char *) malloc ((size_t) ((sizeof(char)) * (templateSize + dirSize + 2)));
    strncpy(template, (const char *) TEMPLATE_DIR, dirSize);
    strncpy(template + dirSize, (const char *) "/", (int) 1);
    strncpy(template + dirSize + 1, (const char *) TEMPLATE_NAME, templateSize);
    template[dirSize + 1 + templateSize] = '\0';
    if ((fd = mkstemp (template)) < 0 ) {
        return -1;
    } else {
        printf("filename - %s\n", template);
    return 0;
The template name should always end with the six characters XXXXXX