😸

[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
 です。
脚注
  1. ctime関数はtime_t型の値(現在時刻など)を人が読める形式の文字列に変換する関数。配列に格納して表示や加工が可能。 ↩︎

  2. 改行やタブなど、画面に表示されない制御文字のこと。 ↩︎

Discussion