小编Spi*_*rix的帖子

动态字符串输入 - 使用scanf("%as")

我正在尝试使用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)

c scanf

9
推荐指数
1
解决办法
1万
查看次数

C是否需要堆栈和堆才能运行?

人们谈论堆栈和堆是什么以及它们之间的差异.但我很想知道如果一个CPU不支持堆栈和堆结构,那么C可以在没有堆栈和堆的情况下正常运行吗?

c heap stack

9
推荐指数
2
解决办法
310
查看次数

GCC在编译时不会发出警告

以下代码编译并运行但我在编译时会发出警告:

#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)

GCC,来自使用命令行参数的在线编译器

-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)

c gcc gnu

8
推荐指数
1
解决办法
274
查看次数

在GCC中编译时使用`-Wextra`标志的缺点

我知道应该总是用两者编译-Wall并且-Wextra因为它们启用警告并帮助我们理解我们的错误,如果有的话.

我已经读过-Wextra不建议使用编译器标志,因为它太冗长了很多误报.

读这篇文章我很惊讶.所以我开始谷歌搜索它,但我没有得到任何答案,因为所有搜索结果显示" -Wextra旗帜做什么?".

所以,我的问题是

  • 在哪种情况下,-Wextra旗帜会发出不必要的警告?
  • 是否可以阻止该-Wextra标志启用导致GCC发出这些类型警告的其他标志?

c gcc code-analysis compiler-flags compiler-warnings

8
推荐指数
1
解决办法
2156
查看次数

如何在带有iso_8859-1的C程序中输出度数符号?

我查了iso_8859-1的手册,找到了度数符号:

\n\n
Oct   Dec   Hex   Char   Description\n260   176   B0     \xc2\xb0     DEGREE SIGN\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码:

\n\n
int 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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但它打印出来的是?.

\n\n

如何在 C 程序中输出学位符号?我需要包含一些文件吗?

\n

c iso-8859-1

8
推荐指数
1
解决办法
2万
查看次数

为什么在声明多维数组时允许省略第一维,而不是其他维?

为什么没有必要提到多维数组的第一维,必须提到其他维度:

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)

我无法理解这背后的概念或逻辑.

c c++ arrays multidimensional-array

8
推荐指数
2
解决办法
773
查看次数

在C中,如何使用scanf扫描1,000,000忽略逗号

在C中,在使用scanf1,000,000这样的数字时忽略逗号的最佳方法是什么?

c scanf

8
推荐指数
2
解决办法
255
查看次数

getchar()等效于scanf("%c")和putchar()等效于printf("%c")?

是a = getchar()相当于scanf("%c",&a);?

是putchar(a)等同于printf("%c",a);其中a是一个char变量?

c scanf putchar getchar

7
推荐指数
1
解决办法
3309
查看次数

释放分配给char*的int*(由`malloc`分配)是否会调用Undefined Behavior?

标题可能令人困惑.假设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?

c memory free pointers undefined-behavior

7
推荐指数
1
解决办法
298
查看次数

什么是`printf`中的%*c%*c?

当我用printf %*c%*c的printf,它需要4个值,并打印出的4和5的总和我找不到这个正当的理由.

在研究时,我发现它%*c表示宽度.但是什么是宽度以及如何得出以下示例的总和?

printf("%*c%*c", 4, ' ', 5, ' ');
Run Code Online (Sandbox Code Playgroud)

Ideone链接

代码:

#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)

c printf

7
推荐指数
2
解决办法
5043
查看次数