【发布时间】:2017-07-10 19:52:46
【问题描述】:
我使用了一个非常简单的代码:
int main(void)
{
size_t variable;
/*prompt*/
printf("enter the value of the variable: ");
scanf("%zd", variable);
printf("you entered %zd value of the variable", variable);
}
但是,GCC 编译器会产生以下结果:
Enter the vale of the size_t variable: 15
You entered zd value of size_t type
Process returned 35 (0X23) execution time: 3.094s
Press any key to continue
我的书也直接演示了上面的例子,没有提到如果要包含库文件,它是某种特殊的格式说明符。 即使the online compiler 也没有产生正确的结果。 为什么代码不起作用?
【问题讨论】:
-
因为您调用了
printf却没有传递说明符的参数。 -
如果您打开更多警告,您会看到您将
variable的值 而不是它的地址 传递给scanf(). -
"%zd"需要匹配的signed size_t参数,但在您的情况下,size_t variable;是long unsigned int。你在那里尝试什么? -
任何初级编程书籍的第一章都应该解释如何使用这些函数。您还必须弄清楚如何启用编译器警告。
标签: c scanf c11 format-specifiers