tmpnam C Library Function

The function char *tmpnam(char *str); returns a null terminated string containing a valid unique file name different from the name of any existing file. The tmpnam function is used to avoid overwriting any existing file while creating temporary files.

Function prototype of tmpnam

char *tmpnam(char *str);
  • str : This is the pointer to a character string where the proposed temporary name will be stored as a null terminated string.

Return value of tmpnam

On success, a pointer to the null terminated string containing the proposed name for a temporary file. If str is not null, It returns str other a pointer to an internal buffer is returned. It returns a null pointer, In case of failure.

C program using tmpnam function

The following program shows the use of tmpnam function to generate unique names for temporary files

#include <stdio.h>

int main(){
   char fileName[L_tmpnam];
   char *ptr;
   /*Create and returns a temporary file name */
   printf("Temporary file name : %s\n", fileName);
   ptr = tmpnam(NULL);
   printf("Temporary file name : %s\n", ptr);


Temporary file name : \s6q0.
Temporary file name : \s6q0.1