w3resource

C tmpnam() function

C library function - tmpnam()

The tmpnam() function is used to generate a string that is a valid pathname that does not name an existing file.

Syntax:

char *tmpnam(char *s);

tmpnam() Parameters:

Name Description Required /Optional
s Stores the file name in s. Required

Return value from tmpnam()

  • Upon successful completion, tmpnam() shall return a pointer to a string.
  • If no suitable string can be generated, the tmpnam() function shall return a null pointer.

Example: tmpnam() function.

Following example calls tmpnam() function to create a valid file name.

#include <stdio.h>
 
int main(void)
{
   char *file_name;
   if ((file_name = tmpnam(NULL)) !=NULL)
      printf("%s can be used as a file name.\n", file_name);
   else 
      printf("Cannot create a unique file name\n");
 
}

Output:

\sge4. can be used as a file name.

C Programming Code Editor:

Previous C Programming: C tmpfile()
Next C Programming: C fprintf()



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What's an object file in C?

An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)

A linker takes all these object files and combines them to form one executable (assuming that it can, i.e.: that there aren't any duplicate or undefined symbols). A lot of compilers will do this for you (read: they run the linker on their own) if you don't tell them to "just compile" using command-line options. (-c is a common "just compile; don't link" option.)

Ref : https://bit.ly/3CbzF8M