繁体   English   中英

C程序打印当前时间

[英]C Program to print Current Time

我正在学习C程序。 当尝试运行代码时,我收到错误:[错误] ld返回1退出状态

   #include <stdio.h>
   #include <time.h>

    void main()
     {

       time_t t;
       time(&t);

       clrscr();

       printf("Today's date and time : %s",ctime(&t));
       getch();

      }

谁能解释一下我在这里做错了什么?

我试过这段代码:

 int main()
   {

  printf("Today's date and time : %s \n", gettime());
  return 0;

   }

  char ** gettime() { 

   char * result;

    time_t current_time;
    current_time = time(NULL);
   result = ctime(&current_time);

     return &result; 

   }

但仍然显示错误为:错误:被叫对象'1'不是current_time = time(NULL)中的函数; 线。 代码有什么问题

我想你在寻找这样的东西:

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main() {

    time_t current_time;
    char* c_time_string;

    current_time = time(NULL);

    /* Convert to local time format. */
    c_time_string = ctime(&current_time);

    printf("Current time is %s", c_time_string);

    return 0;
}

你需要改变clrscr(); 系统(清除).Below是您的代码的工作版本:

#include<stdio.h>
#include<time.h>

void main()
 {

   time_t t;
   time(&t);
   system("clear");
   printf("Today's date and time : %s",ctime(&t));

  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM