简体   繁体   English

time.h适用于C ++,而不适用于C语言

[英]time.h works in C++ not in C

The code: 代码:

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "time.h"
int main()
{
    time_t start, end;
    time (&start);
    int i;
    double dif;
    /*int serie[100000];*/
    /* int *serie = malloc( sizeof(int) );

    for (i = 0; i <= 100000; i++) {
        *serie(i)=rand();
        printf("%d \n", serie[i]);
    }
    */
    time (&end);
    dif = difftime (end, start);

    printf ("Time of execution is: %f\n", dif );

    getchar();

    return 0;

}

The Introduction: (no need to read it) 简介:(无需阅读)

I knew a bit of C++ from about 3 years ago. 我从大约3年前就知道了一些C ++。 I have decided to learn C to create a "fast" subset sum algorithm. 我决定学习C来创建一个“快速”子集和算法。 This was a simple program to learn C. The problem is to create random numbers and time it. 这是一个学习C的简单程序。问题是创建随机数和时间。 This code actually works when I compiled as default or C++ with Visual Studio, but I decided to do it in C and I wanted to create a dynamic array. 当我使用Visual Studio编译为默认或C ++时,此代码实际上有效,但我决定在C中执行此操作,并且我想创建一个动态数组。

It seems that there is no new in C. We have to use malloc , but to compile malloc I think it has to be compiled in C. In C++, it gives me this error: 似乎C中没有new 。我们必须使用malloc ,但是要编译malloc我认为它必须在C中编译。在C ++中,它给了我这个错误:

cannot convert from 'void *' to 'int *'

Anyway, it happens that I decided to write in C, so it seems logical to compile in C. I have chosen the time function to measure the program, I like it more than clock because I do not know how many processors are working. 无论如何,碰巧我决定用C语言编写,所以在C语言编译似乎是合乎逻辑的。我选择了time函数来测量程序,我喜欢它比clock更多,因为我不知道有多少处理器正在工作。

The question: 问题:

The code as it is above with the comments compiles perfectly in C++, but in C it doesn't compile. 上面的代码与注释在C ++中完美编译,但在C中它不编译。 Specifically, this line: 具体来说,这一行:

time (&start);

gives me this error: 给我这个错误:

syntax error : missing ';' before 'type'

I just want to compute the start time and the end time and then subtract them with difftime as I have successfully done in C++. 我只想计算开始时间和结束时间,然后用difftime减去它们,就像我在C ++中成功完成的那样。

I bet that your C compiler is defaulting to the rule that variable declarations must be at the beginning of a block. 我敢打赌,你的C编译器默认为变量声明必须在块的开头。

You have your call to time(&start) before int i . 你可以在int i之前调用time(&start) This is OK in C++ but not in C. To be specific, all variables must be declared before any program code in all versions of C until C99. 这在C ++中是可以的,但在C中则不行。具体来说,所有变量必须在C的所有版本中的任何程序代码之前声明,直到C99。 In the 1999 C Standard, the rule changed to be like C++ and you can mix code and variable declarations. 在1999 C标准中,规则变为C ++,您可以混合代码和变量声明。

If you have a C99 compiler or a compiler option that does allow C99 rules, still don't do it, because it isn't reliably portable. 如果你有一个允许C99规则的C99编译器或编译器选项,仍然不这样做,因为它不可靠地可移植。 And even in C99 there are not many good reasons to mix variables and code anyway. 即使在C99中,无论如何都没有很多理由混合变量和代码。 The only one I can think of is a C99 variable length array that requires code to calculate the desired length. 我能想到的唯一一个是C99可变长度数组,它需要代码来计算所需的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM