我正在尝试使用GCC手册中指定的动态读取输入scanf和存储,但它给出了编译时错误.char *
char *string;
if (scanf ("%as",&string) != 1){
//some code
}
else{
printf("%s\n", *string);
free(string);
//some code
}
Run Code Online (Sandbox Code Playgroud) 人们谈论堆栈和堆是什么以及它们之间的差异.但我很想知道如果一个CPU不支持堆栈和堆结构,那么C可以在没有堆栈和堆的情况下正常运行吗?
以下代码编译并运行但我在编译时会发出警告:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x = 10;
printf("%p\n",&x);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
-Wall -std=gnu99 -O2 -o a.out source_file.c -pedantic -Wextra
Run Code Online (Sandbox Code Playgroud)
编译时发出以下警告
source_file.c: In function ‘main’:
source_file.c:7:3: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int *’ [-Wformat=]
printf("%p\n",&x);
Run Code Online (Sandbox Code Playgroud)
因为我之前没有添加一个(void*)强制转换,因为我在
编译时需要一个类型&x为%p.But的参数void*
gcc SO.c -o so -Wall -Wextra -pedantic -std=c11
Run Code Online (Sandbox Code Playgroud)
要么
gcc SO.c -o so -Wall -Wextra -pedantic -std=c99
Run Code Online (Sandbox Code Playgroud)
要么
gcc SO.c -o …Run Code Online (Sandbox Code Playgroud) 我知道应该总是用两者编译-Wall并且-Wextra因为它们启用警告并帮助我们理解我们的错误,如果有的话.
我已经读过-Wextra不建议使用编译器标志,因为它太冗长了很多误报.
读这篇文章我很惊讶.所以我开始谷歌搜索它,但我没有得到任何答案,因为所有搜索结果显示" -Wextra旗帜做什么?".
所以,我的问题是
-Wextra旗帜会发出不必要的警告?-Wextra标志启用导致GCC发出这些类型警告的其他标志?我查了iso_8859-1的手册,找到了度数符号:
\n\nOct Dec Hex Char Description\n260 176 B0 \xc2\xb0 DEGREE SIGN\nRun Code Online (Sandbox Code Playgroud)\n\n代码:
\n\nint main( int argc, char *argv[])\n{\n char chr = 176; //stores the extended ASCII of a symbol\n printf("Character with an ascii code of 251: %c \\n", chr);\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n但它打印出来的是?.
如何在 C 程序中输出学位符号?我需要包含一些文件吗?
\n为什么没有必要提到多维数组的第一维,必须提到其他维度:
int A[][][2]={{{1,2},{3,4}},{{4,5},{5,6}}}; // error
int A[][2][2]={{{1,2},{3,4}},{{4,5},{5,6}}}; // OK
Run Code Online (Sandbox Code Playgroud)
我无法理解这背后的概念或逻辑.
是a = getchar()相当于scanf("%c",&a);?
是putchar(a)等同于printf("%c",a);其中a是一个char变量?
标题可能令人困惑.假设str是一个分配的指针malloc.ptr类型的int*,分配给它并释放,如下面的代码片段所示:
char* str = malloc(64);
int* ptr = str;
free(ptr);
Run Code Online (Sandbox Code Playgroud)
我试图编译上面的代码.它只是发出警告:
source_file.c: In function ‘main’:
source_file.c:10:16: warning: initialization from incompatible pointer type
int* ptr = str;
^
Run Code Online (Sandbox Code Playgroud)
上面的代码是否调用未定义的行为?
上面的代码片段是否释放了由mallocfor 分配的内存str?
当我用printf %*c%*c的printf,它需要4个值,并打印出的4和5的总和我找不到这个正当的理由.
在研究时,我发现它%*c表示宽度.但是什么是宽度以及如何得出以下示例的总和?
printf("%*c%*c", 4, ' ', 5, ' ');
Run Code Online (Sandbox Code Playgroud)
代码:
#include <stdio.h>
int add(int x, int y)
{
return printf("%*c%*c",x,' ', y,' ');
}
int main()
{
printf("%d",add(4,5));
return 0;
}
Run Code Online (Sandbox Code Playgroud)