Hello i am new to Linux and c programming so this might be stupid question but i couldn't find an answer.
I am writing a home work and they want me to print the execution time at the end of program using time()
function, so when i used the function in my program i got the message segmentation fault (core dumped)
and when i remove it the program works agine. Then i created a test file in the code below :
#include <stdio.h>
int main()
{
time();
return 0;
}
And i got the same error message.
Also tried :
#include <stdio.h>
int main()
{
time(NULL);
return 0;
}
And
#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
return 0;
}
And got the same error.
so what i am doing wrong ?
Thanks
In your first two examples, you forget to include time.h. That is the cause of the segmentation fault in those examples. If you're using gcc, try compiling with -Wall (which turns on all warnings). You should get a warning indicating an implicit declaration of function "time" - in other words, that you've forgotten to include time.h.
Your final example, however, works fine for me. If you keep getting a segmentation fault, however, try debugging with gdb.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
time_t now;
time(&now);
printf("%s", ctime(&now));
return EXIT_SUCCESS;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.