What is the tmpnam function in C ?

mohamad wael
2 min readMay 8, 2021

The tmpnam function is part of the stdio.h header , which is part of the C standard library . The stdio.h header , is related to performing I/O operations , in a portable manner , across different operating systems .

The signature of the tmpnam function is as follows :

#include <stdio.h>char * tmpnam (char *storage);

The role of the tmpnam function , is to generate a unique file path , which does not exist . Although , at a later point of time , this path might exist or maybe created by other programs . As such it is not recommended to use this function , to get a file path , which is later on used to create a file , but what is recommended , is to use the tmpfile function , to just create the temporary file .

Under windows the path returned by tmpnam is affected by the GetTempPathW function , and under linux it is affected by P_tmpdir , which is a directory path prefixed to the generated string . So in a sense , tmpnan generate temporary paths .

If storage is passed to this function , then the path is written to the char storage memory area , and a pointer to that area is returned . The char storage memory area , must have a length of L_tmpnam .

L_tmpnam is defined in the stdio.h header , and it specifies the size of an array , large enough , to hold a value , returned by the tmpnam function .

If NULL storage is passed to this function , then it will use an internal static char memory area , and return a pointer to that area . Subsequent NULL calls , will affect this internal static char memory area.

If the function cannot generate a temporary file name , it will return NULL .

The least number of different strings , that this function can generate is TMP_MAX .

TMP_MAX is defined in the stdio.h header file , it must have at least a value of 25 , and it is the least number of unique strings that can be generated by tmpnam .

It is implementation defined , what happens , if tmpnam is called more than TMP_MAX times .

--

--