user3407267
user3407267

Reputation:

C Program to print Current Time

I am learning C program. When try to run the code I am getting error as : [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();

      }

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

Upvotes: 5

Views: 27243

Answers (2)

Miss J.
Miss J.

Reputation: 391

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

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

void main()
 {

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

  }

Upvotes: 0

Rizier123
Rizier123

Reputation: 59681

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

Upvotes: 11

Related Questions