how to get timestamp in c

c

Solution

You are returning a pointer to a stack variable and therefore it is not valid to use after the function returns:

 char buffer[16];

Will be allocated on the stack in the function. When you return the stack is cleaned up and `buffer` is no longer valid. With minimal changes this is probably a better function signature:

void get_timestamp( char *buffer, size_t buffLen  )

The assumption being that you have properly allocated space for `buffer` before calling `get_timestamp`.

Problem

I want to get timestamp for my log in c. i have written a function to get timestamp. But when i return the variable i m getting different value. My code: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> char* get_timestamp(){ time_t rawtime; struct tm * timeinfo; char buffer[16]; time (&rawtime); timeinfo = localtime (&rawtime); strftime (buffer,16,"%G%m%d%H%M%S",timeinfo); puts(buffer); return buffer; } int main() { puts(get_timestamp()); return 0; } ``` output: ``` 20130315204815 Ir?0315204815 ``` Can anyone help out from this... Thank you.

Original source