Search code examples
ctimestdio

Called object 'time' is not a function


I am using a very simple one line code to get the current time in C on ubuntu, but it is giving me the following error: Called object 'time' is not a function

The code is: int currentTime = (unsigned int)time(NULL);

This error makes no sense at all, what is the possible reason for this?


Solution

  • Not being able to see your actual code makes it difficult but, first up, you need to ensure you have included time.h so that the function is declared.

    Second, you need to ensure that there is no other thing in your code somewhere called time.

    For example, this code works fine:

    #include <stdio.h>
    #include <time.h>
    
    int main(){
        int currentTime = (unsigned int)time(NULL);
        printf("%u\n", currentTime);
        return 0;
    }
    

    Whereas this code:

    #include <stdio.h>
    int time;
    
    int main(){
        int currentTime = (unsigned int)time(NULL);
        printf("%u\n", currentTime);
        return 0;
    }
    

    produces:

    testprog.c: In function ‘main’:
    testprog.c:5:38: error: called object ‘time’ is not a function
    

    on my Debian box, which is like Ubuntu, only better :-)