2

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?

Community
  • 1
  • 1
Pigna
  • 2,792
  • 5
  • 29
  • 51

2 Answers2

3

You can't use function calls to initialize file scope variables (those outside main).

You must move begin and end into main() (well, at least their initialization).

A C program doesn't execute top to bottom; it starts with main(). The values of initialized file scope variables must be known at compile-time, which is the reason you can't use function calls.

To get meaningful results, I also suggest you run the code to be tested many times in a loop, since the resolution of the clock is often too coarse to time a few instructions, i.e. do something like

 begin = ...
 for (j = 0; j < 10000; ++j) {
     code_to_test();
 }
 end = ...
Jens
  • 69,818
  • 15
  • 125
  • 179
  • @Pigna That's good news. Now the next step on Stack Overflow is to *accept* the answer you deem most helpful by clicking the *check mark* to the left. This will earn you two reputation points. – Jens Dec 06 '15 at 17:41
  • Yep, I wanted to do it right away, but he asked me to wait 4 minutes @Jens – Pigna Dec 06 '15 at 17:42
  • @Pigna In this case they're equivalent. I prefer `++j` over `j++` because mentally I read the code as "increment j" and not as "j, increment that". – Jens Dec 06 '15 at 17:44
1

While you can initialize global variables outside of a code block, you can't do what you're doing (if you want code to work). In general, code shouldn't be sitting outside of functions. You want end = clock() to be executed at the end! To do this, it needs to be at the end of the main() function.

Move the code into main() so it reads:

int main(void) {
    begin = clock();

    ... //your code here

    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time spent %f \n", time_spent);
}
Matthew Gunn
  • 4,451
  • 1
  • 12
  • 30