Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library

The C++ <cstdio> tmpname() function creates a string containing a file name different from the name of any existing file, and stores it in the character string pointed to by str. The function is capable of generating up to TMP_MAX of unique filenames, but some or all of them may already be in use. If it is called more than TMP_MAX times, the behavior is implementation dependent.


char * tmpnam ( char * str );


str Specify a pointer to the character array where the proposed temporary name will be stored as a C string. The size of this array should be at least L_tmpnam bytes. If a null pointer is passed, a pointer to an internal static buffer is returned.

Return Value

On success, returns str if str is not a null pointer, Otherwise a pointer to an internal static buffer is returned. On failure, a null pointer is returned.


The below example shows the usage of tmpnam() function.

#include <cstdio>
int main (){

  //creating file name by passing null pointer
  char * filename1 = tmpnam(NULL);
  printf ("Temporary file name: %s\n", filename1);

  //creating file name by passing character string
  char filename2[L_tmpnam];
  printf ("Temporary file name: %s\n", filename2);

  return 0;

One of the possible outcome of the above code could be:

Temporary file name: /tmp/fileRGh1Gn
Temporary file name: /tmp/fileAtjekX

❮ C++ <cstdio> Library