C语言 C 程序打印当前时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26900122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:33:16  来源:igfitidea点击:

C Program to print Current Time

cwindowstime

提问by Rizier123

I am learning C program. When try to run the code I am getting error as : [Error] ld returned 1 exit status

我正在学习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();

      }

Can someone explain me What I am doing wrong here?

有人可以解释一下我在这里做错了什么吗?

I tried this code :

我试过这个代码:

 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; 

   }

but still shows me error as : error: called object ‘1' is not a function in current_time = time(NULL); line. What is wrong with the code

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

回答by Rizier123

I think your looking for something like this:

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

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

回答by Miss J.

you need to change clrscr(); to system(clear).Below is the working version of your code:

你需要改变 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));

  }