【问题标题】:Error in a c program to calculate the execution time of programc程序中计算程序执行时间的错误
【发布时间】:2015-06-25 08:33:55
【问题描述】:

我正在尝试计算下面给出的程序的执行时间,但它给了我一些错误

#include<stdio.h>
#include<time.h>

int main()

{
  clock_t start, end;   
  start = clock();


int a[5] ={1,2,3,4,51};
int b[5] = {2,3,4,5,6};
int c[5] = {3,4,5,6,7};


for (int i =0;i<5;i++)
{
    if(a[i]== NULL )
    printf("%d element null::",a[i]);
    else
    printf("no null values in first array\n");


        if(b[i]== NULL )
    printf("%d element null::",b[i]); 
        else 
        printf("no null values in second array\n");


        if(c[i]== NULL )
    printf("%d element null::",c[i]);
        else
        printf("no null values in third array\n");

}


  end = clock();
  printf("Start time: %i, End time: %i \n" start, end);

return 0;

}

以下是错误和警告..

        [eshwar@localhost ~]$ gcc -std=c99 ab.c
ab.c: In function ‘main’:
ab.c:18:9: warning: comparison between pointer and integer [enabled by default]
  if(a[i]== NULL )
         ^
ab.c:24:16: warning: comparison between pointer and integer [enabled by default]
         if(b[i]== NULL )
                ^
ab.c:30:16: warning: comparison between pointer and integer [enabled by default]
         if(c[i]== NULL )
                ^
ab.c:39:44: error: expected ‘)’ before ‘start’
   printf("Start time: %i, End time: %i \n" start, end);

为什么会给出这个警告..因为我认为我们可以像这样初始化数组??

【问题讨论】:

  • printf("Start time: %i, End time: %i \n" start, end); 应该是printf("Start time: %i, End time: %i \n", start, end);
  • 两者有什么区别??两者看起来一样
  • 逗号在start之前,在格式字符串之后。
  • ints 不能是 NULL。你想在那里做什么?
  • 是的。与NULL 的比较没有任何意义。

标签: c gcc time


【解决方案1】:

我猜你只是想知道数组中的一个数字是否为 0。 在这种情况下,您只需将 if(a[i]== NULL ) 更改为 if(a[i] == 0)。(与其他两个相同)

正如 Cool guy 已经说过的,字符串和开头之间必须有一个逗号。

程序会运行,但编译器仍然会抱怨 'clock_t' 和 'int' 之间的差异

所以最后你可以把这一行改成:

printf("Start time: %i, End time: %i \n", (int)start, (int)end);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2011-11-20
    相关资源
    最近更新 更多