Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中打印作为函数参数传递的整数_C_Integer_Printf - Fatal编程技术网

在C中打印作为函数参数传递的整数

在C中打印作为函数参数传递的整数,c,integer,printf,C,Integer,Printf,我对C编程非常陌生,在编译一个非常简单的函数时遇到困难。该函数名为printSummary,只需将3个整数作为参数,然后将一些文本与这些整数一起打印。例如,如果命中率=1,未命中率=2,逐出率=3,则printSummary(命中率,未命中率,逐出率)应打印以下内容: 命中:1次未命中:2次驱逐:3次 这是我正在使用的代码。提前谢谢你的建议 #include<stdio.h> void printSummary(int hits, int misses, int evictions

我对C编程非常陌生,在编译一个非常简单的函数时遇到困难。该函数名为printSummary,只需将3个整数作为参数,然后将一些文本与这些整数一起打印。例如,如果命中率=1,未命中率=2,逐出率=3,则printSummary(命中率,未命中率,逐出率)应打印以下内容:

命中:1次未命中:2次驱逐:3次

这是我正在使用的代码。提前谢谢你的建议

#include<stdio.h>

void printSummary(int hits, int misses, int evictions)
{
    printf('hits: %d\n');
    printf('misses: %d\n');
    printf('evictions: %d\n');
}

int main()
{
    int hit_count = 1;
    int miss_count = 2;
    int eviction_count = 3;
    printSummary(hit_count, miss_count, eviction_count);
    return 0;
}
#包括
无效打印摘要(整数命中、整数未命中、整数收回)
{
printf('hits:%d\n');
printf('未命中:%d\n');
printf('逐出:%d\n');
}
int main()
{
int hit_count=1;
int miss_count=2;
int逐出计数=3;
打印摘要(命中计数、未命中计数、驱逐计数);
返回0;
}

编译这段代码会给我一些警告,但没有错误。当我运行代码时,我得到一个分段错误。正如我所说的,我对C相当陌生,所以很可能有一个我刚刚错过的简单解决方案。提前谢谢你的建议

未正确调用printf函数。必须包含打印所需的整数:

#include<stdio.h>

void printSummary(int hits, int misses, int evictions)
{
    printf('hits: %d\n');
    printf('misses: %d\n');
    printf('evictions: %d\n');
}

int main()
{
    int hit_count = 1;
    int miss_count = 2;
    int eviction_count = 3;
    printSummary(hit_count, miss_count, eviction_count);
    return 0;
}
printf("hits: %d\n", hits);
printf("misses: %d\n", misses);
printf("evictions: %d\n", evictions);

阅读有关printf函数的更多信息。

进行以下更改

   printf("hits: %d\n",hits);
   printf("misses: %d\n",misses);
   printf("evictions: %d\n",evictions);
printf有一个

int printf(const char *format, ...)
原型。因此,在第一个参数中,您可以传递格式说明符,并在下一个参数中提供要打印的实际变量/值。错误如下:

void printSummary(int hits, int misses, int evictions)
{
    /* Name: printf
    Prototype: int printf (const char *template, ...)
    Description:
    The printf function prints the optional arguments under the
     control of the template string template to the stream stdout. 
    It returns the number of characters printed,or a negative value if
     there was an output error.*/

    printf("hits: %d\n", hits); // don't use ' it is used only for char variable for example: char a = 'c';
    printf("misses: %d\n", misses);
    printf("evictions: %d\n", evictions);
}

第1步
'hits:%d\n'
-->“hits:%d\n”“vs”请始终列出您收到的警告和错误。“给我几个警告”是一件很好的事情,但除非您告诉我们您收到了什么警告,否则最终将毫无用处。将“改为”有效。它们的工作方式不同是有原因的吗?
'a'
在C中是字符文字的表示法,而
“abc”
是字符串文字。您介意解释一下OP的错误吗,这样他就可以理解和学习了?我原以为使用%d只是用来按顺序给定参数。我没有意识到我必须指定它们。我以前试过,但仍然收到一大堆警告。按照你的建议做,并将“to”改为“Do”似乎已经奏效。Do和“有不同的含义吗?刚才向我解释过,在cahh中,“Do”用于字符,而“to”用于字符串,我现在明白了……我需要将“to”改为“to”。我已经编辑了我的答案。