Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strftime

Status
Not open for further replies.

cmancre

Programmer
Jan 30, 2005
35
PT
code:

char buffer[2]={NULL,NULL};
...// time code and stuff
strftime(buffer, 2, "%M", loctime);

when i print to stdout the buffer i got trash like @, but like this:

char buffer[3]={NULL,NULL,NULL};
...// time code and stuff
strftime(buffer, 3, "%M", loctime);

the buffer is OK, i got minutes, buffer[2] still NULL
My question is, why i need that extra char (buffer[2]) for this works well?
 
> My question is, why i need that extra char (buffer[2]) for this works well?
Because all proper C strings have a \0 at the end.

So your minutes would be say "59\0" - a total of 3 chars.

> char buffer[3]={NULL,NULL,NULL};
NULL is used for pointers, which these are not.
If you want the nul character, then it's typically written as '\0'

But char buffer[3] = { 0 }; should also work just as well, since C sets all unspecified initialisers to 0 anyway.

It's a rather moot point, since you're going to initialise it anyway when you call strftime.


--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top