5

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

  • 2
    You forgot to include `conio.h` – Spikatrix Nov 13 '14 at 02:03
  • 2
    That is probably not an error from when you try to run the code, but when you try to link it. Also, there's probably more to the full error message. – aschepler Nov 13 '14 at 02:04
  • this line: void main() is not a valid format. it should be: int main() And there needs to be a line after that call to getch(): return(0); also, the printf() format string should end with '\n' so the output buffer gets flushed, so the output will show on the terminal – user3629249 Nov 13 '14 at 03:28
  • Related: http://stackoverflow.com/questions/3673226/how-to-print-time-in-format-2009-08-10-181754-811 – Ciro Santilli OurBigBook.com May 07 '16 at 11:54

2 Answers2

10

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;
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

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

  }
Miss J.
  • 351
  • 1
  • 5
  • 15