I've read this post here and I followed the instructions, applying them to a simple program that sums all the numbers below 1000 divisible by 3 and 5.
#include <stdio.h>
#include <time.h>
clock_t begin, end;
double time_spent;
begin = clock();
int sumDivisibleBy (div, limit) {
int h = (limit - 1)/div;
return div*h*(h+1)/2;
}
int main(void) {
int l = 1000;
int s = sumDivisibleBy(3,l) + sumDivisibleBy(5,l) - sumDivisibleBy(15,l);
printf("%d \n", s);
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC
printf("%f \n", time_spent)
Now, when I type in the terminal "make 1" (the file is called 1.c), this is what I get:
cc 1.c -o 1
1.c:9:1: warning: data definition has no type or storage class [enabled by default]
begin = clock();
^
1.c:9:1: error: conflicting types for ‘begin’
1.c:6:9: note: previous declaration of ‘begin’ was here
clock_t begin, end;
^
1.c:9:1: error: initializer element is not constant
begin = clock();
^
1.c:20:1: warning: data definition has no type or storage class [enabled by default]
end = clock();
^
1.c:20:1: error: conflicting types for ‘end’
1.c:6:16: note: previous declaration of ‘end’ was here
clock_t begin, end;
^
1.c:20:1: error: initializer element is not constant
end = clock();
^
1.c:21:1: warning: data definition has no type or storage class [enabled by default]
time_spent = (double)(end - begin) / CLOCKS_PER_SEC
^
1.c:21:1: error: conflicting types for ‘time_spent’
1.c:7:8: note: previous declaration of ‘time_spent’ was here
double time_spent;
^
1.c:21:1: error: initializer element is not constant
time_spent = (double)(end - begin) / CLOCKS_PER_SEC
^
1.c:21:1: error: expected ‘,’ or ‘;’ at end of input
make: *** [1] Error 1
Why is it? Can somebody help please?