bza*_*zal 0 c scanf format-specifiers c11
我使用了一个非常简单的代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
但是,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
Run Code Online (Sandbox Code Playgroud)
我的书也直接演示了上面的例子,但没有提到它是某种特殊的格式说明符,如果要包含库文件的话.即使是在线编译器也没有产生正确的结果.为什么代码不起作用?
scanf("%zd", variable);
Run Code Online (Sandbox Code Playgroud)
是未定义的行为,因为您没有传递指针.
printf("you entered %zd value of the variable");
Run Code Online (Sandbox Code Playgroud)
也是未定义的行为,因为您使用了格式说明符但未传递匹配的参数.
%zd是C11托管实现的强制功能.这意味着任何实现stdio.h的编译器都必须使用它.但是,%zd没有多大意义,因为size_t总是没有签名.请%zu改用.
请注意,存在标准库的不兼容实现,最明显的是Microsoft的Windows实现.Visual Studio以及gcc/mingw编译器使用标准库的Microsoft实现.该库不支持%zu正确,因为它不符合C11标准.
| 归档时间: |
|
| 查看次数: |
978 次 |
| 最近记录: |