# C char function on Visual Studio C 2019 wrong result, or not.

#### nagkiller

##### Full Member level 4
Could anyone help me with a error in char function on Visual Studio C 2019.

C++:
#include <windows.h>
#include <iostream>

char *timeStamp();

int main(void) {
printf_s("%s", timeStamp());
return 0;
}

char *timeStamp() {
SYSTEMTIME t;
GetLocalTime(&t);
char buffer[35];
int j;
j = sprintf_s(buffer, 25, "%02d/", t.wDay);
j += sprintf_s(buffer + j, 30 - j, "%02d/", t.wMonth);
j += sprintf_s(buffer + j, 30 - j, "%04d ", t.wYear);
j += sprintf_s(buffer + j, 30 - j, "%02d:", t.wHour);
j += sprintf_s(buffer + j, 30 - j, "%02d:", t.wMinute);
j += sprintf_s(buffer + j, 30 - j, "%02d = ", t.wSecond);
printf_s("%s\n", buffer);
return buffer;
}
Inside function, result is ok, but in main function it is printing differents characters.

Code:
Inside function ===> 10/11/2020 22:07:11 =
Outside function ==> ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠♀■´
If I change main function to use strncpy_s, a half of result is printed.

C++:
int main(void) {
char timeNow[35];
strncpy_s(timeNow,35, timeStamp(), 34);
printf_s("%s\n", timeNow);
return 0;
}
The result is:
Code:
10/11/20☺

Last edited:

#### std_match

The "buffer" is local to the function (on the stack), so the contents can change after you exit the function.
Make buffer "static" or provide a buffer in the function call.

#### KlausST

##### Super Moderator
Staff member
Hi,

C char function on Visual Studio C 2019 wrong result, or not.
Altough I´m not a C expert.... (please experts correct me if I´m wrong)
I´d like to clarify.
"char" is not a function. "char" is the declarion of the variable for the return value of the function "timestamp".

The function "timestamp" defines an array, thus it transferres "timestamp" as pointer.
It´s likely that in main you expect a char (= usually a byte), but you receive a pointer (= an address of maybe 32 bit in length).

or something like that....

Klaus

#### std_match

"char" is not a function. "char" is the declarion of the variable for the return value of the function "timestamp".

The function "timestamp" defines an array, thus it transferres "timestamp" as pointer.
It´s likely that in main you expect a char (= usually a byte), but you receive a pointer (= an address of maybe 32 bit in length).
char *timeStamp(); // "char *" means that the function returns a pointer to "char", in this case a pointer to the first element of an array
"char *" is the normal "type" for a strings in C/C++. It is only a pointer to the first character in a null-terminated string.

The problem in this case is that the string pointer points to an area that isn't valid after exit from the function,
because the area "buffer" is located in the stack frame for the function.
A simple fix is to move "buffer" into a dedicated memory area by adding the keyword "static":

static char buffer[35];

The drawback with this solution is that the function isn't "reentrant", so only one thread should use it.

#### nagkiller

##### Full Member level 4
Sorry for bad english or don't express myself correctly.