printf behaviour

2023-05-12 Thread johannes janssens via Gcc-bugs
//-- gcc (Debian 10.2.1-6) 10.2.1 20210110
//-- Debian 11.7
/*cut and paste and compile*/

#include 
#include 
typedef enum {
   false=0,
   true=1
}
predikaat;

static char *timestamp(predikaat bStamp){
//-
   static char stamp[22];

   time_t curtime;
   struct tm *gmtijd, *loctijd;

   curtime=time (NULL);
   gmtijd = gmtime (&curtime);
   loctijd = localtime(&curtime);
   if (bStamp==true){
  strftime (stamp, 22, "GM%Y%m%d%H%M%S", gmtijd );
   }
   else {
  strftime (stamp, 22, "%d/%m/%Y-%H:%M:%S",loctijd);
   }
   return stamp;
}
int main(void){
   printf("Look what happens when I call timestamp multiple times with 
different args within the same printf...\n"
  "Is this normal behaviour?\n"
  "I presume it has to do with the static variable?\n\n");

   printf("\ntime as stamp : %s",timestamp(true));

   printf("\ntime as human readable : %s",timestamp(false));

   printf("\nstamp=\n%s\n%s\n%s\n%s",
timestamp(true),
timestamp(false),
timestamp(false),
timestamp(true));

   printf("\nstamp=\n%s\n%s\n%s\n%s\n",
timestamp(false),
timestamp(true),
timestamp(false),
timestamp(true));

   return 0;
}


Re: printf behaviour

2023-05-12 Thread Jonathan Wakely via Gcc-bugs
This mailing list is for automated mails from our Bugzilla bug tracker
system.

If you want to report a bug, please see https://gcc.gnu.org/bugs/

If you want to ask a question, use the gcc-h...@gcc.gnu.org list, not
the gcc-bugs list. Please direct any follow-up discussion there
instead.

There is nothing wrong with printf. Yes, it's the sttic variable.
Before printf is called, every argument has to be evaluated. Since
every call to timestamp returns the same pointer, you just print that
string four times, and whatever it happens to contain at that time is
printed four times.