Using clock() to take a time stamp (Visual Studio 2010, C/C++)
Solution 1
Try moving the variables to the top before any statements...
void main() {
int n, c = 0;
clock_t start; /* Line 8 */
clock_t finish; /* Line 9 */
printf("Please enter an integer...\n");
scanf("%d", &n);
The rest of your errors are just consequences of these not being declared properly.
In a later C standard (C99) declarations can be mixed in anywhere. Often it's just easier to make it compliant... Another approach is to introduce a block { } like so:
void main() {
int n, c = 0;
printf("Please enter an integer...\n");
scanf("%d", &n);
{
clock_t start; /* Line 8 */
clock_t finish; /* Line 9 */
start = clock();
while (n != 1) {
if (n%2 == 0)
n = n/2;
else
n = (3*n)+1;
c++;
printf("n=%d\n", n);
}
finish = clock() - start;
double interval = finish / (double)CLOCKS_PER_SEC;
printf("%d iterations\n", c);
printf("%f clock cycles", finish);
printf("%f seconds elapsed", interval);
}
}
In this case, it's not an ideal solution as it's simple enough to rearrange your code. But sometimes this is useful when trying to make C99 code compile in C89 without rearranging things too much.
Solution 2
Microsoft Visual C++ doesn't support C99, but older C standards where variables have to be defined at the top of each block. So change your code to:
void main() {
int n, c = 0;
clock_t start;
clock_t finish;
printf("Please enter an integer...\n");
scanf("%d", &n);
Solution 3
If you're compiling code as C using Microsoft's compiler (the one bundled with Visual Studio), then you need to be aware that it does not actually support the C99 standard. You're stuck with C89.
And one of the most infuriating things that is missing in C89 is the ability to declare variables anywhere. Instead, you're forced to declare them at the top of a block.
The compiler errors that you get when you fail to follow this rule are often rather undescriptive. I make this mistake quite frequently and spend a few seconds puzzling at the output.
So change your code to look like this, declaring start
and finish
at the top of the function block:
#include <stdio.h>
#include <time.h>
int main(void) {
/* declare all variables at the top */
int n, c = 0;
clock_t start;
clock_t finish;
printf("Please enter an integer...\n");
scanf("%d", &n);
start = clock();
while (n != 1) {
if (n%2 == 0)
n = n/2;
else
n = (3*n)+1;
c++;
printf("n=%d\n", n);
}
finish = clock() - start;
double interval = finish / (double)CLOCKS_PER_SEC;
printf("%f clock cycles", finish);
printf("%f seconds elapsed", interval);
}
Also note that there is no such thing as void main()
in C. The function prototype of the main
function is always one of the following:
int main(void);
int main(int argc, char **argv);
int main(int argc, char *argv[]);
Vance
Updated on June 04, 2022Comments
-
Vance almost 2 years
Ok, so I am trying to implement the Collatz problem in C, and record/print the time it takes for the while loop to execute. I am supposed to report both the number of "ticks" and the time in seconds. However, I am getting some seemingly simple errors from my code but for whatever reason, I am not sure how to correct them.
This is my code
#include <stdio.h> #include <time.h> void main() { int n, c = 0; printf("Please enter an integer...\n"); scanf("%d", &n); clock_t start; /* Line 8 */ clock_t finish; /* Line 9 */ start = clock(); while (n != 1) { if (n%2 == 0) n = n/2; else n = (3*n)+1; c++; printf("n=%d\n", n); } finish = clock() - start; double interval = finish / (double)CLOCKS_PER_SEC; printf("%d iterations\n", c); printf("%f clock cycles", finish); printf("%f seconds elapsed", interval); }
These are the errors Visual Studio is reporting
Line 8 and 9 Errors
'clock_t' : illegal use of this type as an expression
syntax error : missing ';' before identifier 'start'
'start' : undeclared identifier
I am also getting an 'undeclared identifier' error for all lines in which 'start' or 'finish' show up
-
Vance over 12 yearsWow, never knew that about Visual Studio (I'm coming from Java). Thanks for the help.
-
Vance over 12 yearsThanks for the quick response
-
Vance over 12 yearsThank you for the quick response!