【问题标题】:C Program to print Current TimeC程序打印当前时间
【发布时间】:2014-11-13 02:01:32
【问题描述】:

我正在学习 C 程序。尝试运行代码时出现错误:[Error] ld returned 1 exit status

   #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);线。代码有什么问题

【问题讨论】:

  • 您忘记包含conio.h
  • 这可能不是您尝试运行代码时的错误,而是您尝试链接它时的错误。此外,完整的错误消息可能还有更多内容。
  • 这一行:void main() 不是有效格式。它应该是: int main() 在调用 getch() 之后需要有一行:return(0);此外,printf() 格式字符串应以 '\n' 结尾,以便刷新输出缓冲区,因此输出将显示在终端上

标签: c windows time


【解决方案1】:

我认为您正在寻找这样的东西:

#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;
}

【讨论】:

【解决方案2】:

你需要改变 clrscr();到系统(清除)。下面是您的代码的工作版本:

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

void main()
 {

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

  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 2015-02-01
    • 1970-01-01
    相关资源
    最近更新 更多