7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語で現在時刻文字列をワンライナーで格納する

7
Last updated at Posted at 2016-07-31

例 (C99以降で動作)

strftimeを使う
# include <stdio.h>
# include <time.h>
 
int main(void)
{
    char buf[20];
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&(time_t){time(NULL)}));
    puts(buf);
    return 0;
}
直接特定のアイテムのみ参照する (変数が完全に不要)
# include <stdio.h>
# include <time.h>
 
int main(void)
{
    printf("只今%d時です\n", localtime(&(time_t){time(NULL)})->tm_hour);
    return 0;
}

注意

スレッドセーフな処理をしたければ localtime の代わりに localtime_r を使う

参考

7
6
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?