c – Tribonacci for high range limit

The program is like the next number is the sum of the 3 previous numbers. I like 0,1,1,2,4,7,13,24 … N is the term for the number of numbers that should be present in the Tribonacci series. The main problem is $ N <= 10 ^ {18} $. Here is my code. It is not giving the correct answer for a high rank of N. How can I solve the problem?

#include 
#include 
int main ()
{
unsigned long long int n;
scanf ("% lld", & n);
int signed long long int sum = 0, p = 0, q = 1, r = 2, k;
for (k = 0; (k + 2) <n; k ++)
{
sum = p + q + r;
p = q; q = r; r = sum;
}
printf (" n% lld  n", sum);
returns 0;
}