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()



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Reading a string with scanf :

An array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[256], but it points to the same place.

Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in string or &string[0], but when you've passed in &string you're depending on something that the language standard doesn't guarantee, namely that the pointers &string and &string[0] -- pointers to objects of different types and sizes that start at the same place -- are represented the same way.

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