\和0字符是否存储在字符串末尾的相同位置或不同位置?
main()
{
char x[]="Hello\0";
char y[]="Hello12";
char z[]="Hello\012";
char w[]="Hello1234";
printf("%d %d %d %d", sizeof(x), sizeof(y), sizeof(z), sizeof(w));
}
Run Code Online (Sandbox Code Playgroud)
输出:
7 8 7 10
Run Code Online (Sandbox Code Playgroud)
请解释代码的输出.
我对以下代码片段感到困惑,因为对于相同的字符串,输出分别为1和2.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char str2[1]="a",str3[]="a";
printf("%d %d ",sizeof(str2),sizeof(str3));
getch();
}
Run Code Online (Sandbox Code Playgroud) 我已经阅读了有关此主题的几篇文章,但是有人说应该完成的方式取决于您所使用的体系结构和系统(Windows / Linux),那么是否有适当的方法可以独立于该平台?
我使用了一个非常简单的代码:
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)
我的书也直接演示了上面的例子,但没有提到它是某种特殊的格式说明符,如果要包含库文件的话.即使是在线编译器也没有产生正确的结果.为什么代码不起作用?