public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* printf behaviour
       [not found] <1547579412.2083091.1683883185453.ref@mail.yahoo.com>
@ 2023-05-12  9:19 ` johannes janssens
  2023-05-12  9:45   ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: johannes janssens @ 2023-05-12  9:19 UTC (permalink / raw)
  To: gcc-bugs

//-- gcc (Debian 10.2.1-6) 10.2.1 20210110
//-- Debian 11.7
/*cut and paste and compile*/

#include <stdio.h>
#include <time.h>
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;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: printf behaviour
  2023-05-12  9:19 ` printf behaviour johannes janssens
@ 2023-05-12  9:45   ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-05-12  9:45 UTC (permalink / raw)
  To: johannes.janssens; +Cc: 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-help@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.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-05-12  9:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1547579412.2083091.1683883185453.ref@mail.yahoo.com>
2023-05-12  9:19 ` printf behaviour johannes janssens
2023-05-12  9:45   ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).