😸
[C] ctime 関数
機能
現在時刻を文字列として取得する[1]
使用例
Thu Jun 13 15:04:52 2024\n
と表示する
\n
は改行を表す エスケープシーケンス[2]
コード
time_t t = time(NULL);
printf("%s", ctime(&t));
全文
#include <stdio.h>
#include <time.h>
void main() {
time_t t = time(NULL);
printf("%s", ctime(&t));
}
実行結果
Thu Jun 13 15:04:52 2024
現在時刻は Thu Jun 13 15:04:52 2024 です。
を変数から表示する
コード
time_t t = time(NULL);
char *str = ctime(&t);
printf("現在時刻は %s です。", str);
全文
#include <stdio.h>
#include <time.h>
void main() {
time_t t = time(NULL);
char *str = ctime(&t);
printf("現在時刻は %s です。", str);
}
実行結果
現在時刻は Thu Jun 13 15:04:52 2024
です。
Discussion