用C time.h中工作++不是在C [英] time.h works in C++ not in C

查看:129
本文介绍了用C time.h中工作++不是在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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)

我知道一点C ++的约3年前。我已决定
学习C创建一个快的子集和算法。这是一个
简单的程序来学习C的问题是创建随机数和
计时。当我编译为默认或这code的实际工作
C ++和Visual Studio,但我决定做在C
我想创建一个动态数组。

I knew a bit of C++ from about 3 years ago. I have decided to learn C to create a "fast" subset sum algorithm. This was a simple program to learn C. The problem is to create random numbers and time it. 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.

似乎没有在C.我们必须使用的malloc
但编译的malloc 我觉得它在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:

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

反正它发生,我决定用C写的,所以它似乎是合乎逻辑
编译C.我选择了时间功能测量程序,
我喜欢它比时钟更多,因为我不知道有多少处理器
都在工作。

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.

问题:

在code,因为它是上面的意见完全在编译C ++,但在C编译不能通过。
具体而言,该行:

The code as it is above with the comments compiles perfectly in C++, but in C it doesn't compile. Specifically, this line:

time (&start);

给我这个错误:

syntax error : missing ';' before 'type'

我刚要计算的开始​​时间和结束时间,然后
difftime 减去他们,因为我已经在C ++中成功完成。

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++.

推荐答案

我敢打赌,你的C编译器默认为变量的声明必须在块开头的规则。

I bet that your C compiler is defaulting to the rule that variable declarations must be at the beginning of a block.

您有您的来电时间(安培;开始)前 INT I 。这是OK在C ++中,但不是在C.具体而言,所有的变量必须在直到C99的C所有版本的任何程序code之前声明。在1999年的C标准,规则改变能像C ++和可以随意code和变量声明。

You have your call to time(&start) before int i. 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. In the 1999 C Standard, the rule changed to be like C++ and you can mix code and variable declarations.

如果你有一个C99编译或确实允许规则C99编译器选项,仍然不这样做,因为这是不可靠的便携。即使在C99没有很多很好的理由反正混合变量和code。唯一一个我能想到的是C99变长数组,需要code,计算所需的长度。

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. And even in C99 there are not many good reasons to mix variables and code anyway. The only one I can think of is a C99 variable length array that requires code to calculate the desired length.

这篇关于用C time.h中工作++不是在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆