当我尝试编译使用gets()GCC函数的C代码时,
我明白了
警告:
(.text + 0x34):警告:`gets'函数很危险,不应该使用.
我记得这与堆栈保护和安全性有关,但我不确定为什么?
有人可以帮我删除这个警告并解释为什么会有这样的使用警告gets()?
如果gets()是如此危险,为什么我们不能删除它?
我经常看到人们不鼓励其他人使用scanf并说有更好的选择。但是,我最终看到的只是“不要使用scanf”或“这里是正确的格式字符串”,并且从来没有提到“更好的替代方案”的任何示例。
例如,让我们看一下这段代码:
scanf("%c", &c);
Run Code Online (Sandbox Code Playgroud)
这将读取最后一次转换后留在输入流中的空白。通常建议的解决方案是使用:
scanf(" %c", &c);
Run Code Online (Sandbox Code Playgroud)
还是不使用scanf。
由于scanf不好,用于转换scanf通常无需使用即可处理的输入格式(例如整数,浮点数和字符串)的ANSI C选项有哪些scanf?
在C:我正在尝试从用户获取char scanf,当我运行它时程序不等待用户输入任何东西......
这是代码:
char ch;
printf("Enter one char");
scanf("%c", &ch);
printf("%c\n",ch);
Run Code Online (Sandbox Code Playgroud)
为什么不工作?
因此,谷歌快速搜索fflush(stdin)清除输入缓冲区会发现许多网站警告不要使用它.然而,这正是我的CS教授教授课程的原因.
使用有多糟糕fflush(stdin)?即使我的教授正在使用它并且似乎完美无缺地工作,我是否真的应该放弃使用它?
如果我尝试以下内容:
int anint;
char achar;
printf("\nEnter any integer:");
scanf("%d", &anint);
printf("\nEnter any character:");
scanf("%c", &achar);
printf("\nHello\n");
printf("\nThe integer entered is %d\n", anint);
printf("\nThe char entered is %c\n", achar);
Run Code Online (Sandbox Code Playgroud)
它允许输入一个整数,然后scanf完全跳过第二个,这真的很奇怪,因为当我交换两个(charscanf优先)时,它工作正常.究竟是什么错了?
我有这个代码块(函数省略,因为逻辑是家庭作业的一部分):
#include <stdio.h>
int main()
{
char c = 'q';
int size;
printf("\nShape (l/s/t):");
scanf("%c",&c);
printf("Length:");
scanf("%d",&size);
while(c!='q')
{
switch(c)
{
case 'l': line(size); break;
case 's': square(size); break;
case 't': triangle(size); break;
}
printf("\nShape (l/s/t):");
scanf("%c",&c);
printf("\nLength:");
scanf("%d",&size);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
前两个Scanf的工作很好,没有问题,一旦我们进入while循环,我有一个问题,当你应该被提示输入一个新的形状char时,它会跳转到printfLength的长度并等待输入从那里获取一个char,然后是循环的下一次迭代的小数.
Preloop迭代:
Scanf:形状.Works Great
Scanf:长度.没问题
循环1.
Scanf:形状.跳过此
Scanf:长度.问题,这个scanf映射到形状char.
循环2
Scanf:形状.跳过此
Scanf:长度.问题,这个scanf现在映射到int大小.
它为什么这样做?
这段代码scanf("%d")和之间scanf("%d ")的区别是什么,其中差异是格式字符串中的尾随空白?
#include <stdio.h>
int main(void)
{
int i, j;
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d\n", j);
printf("enter a value for i ");
scanf("%d", &i);
printf("i is %d\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
scanf()当我在格式说明符之后添加空格时,函数如何实际工作scanf("%d ", &j);?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,提示符要求我两次输入(我只scanf在代码中使用一次).这是为什么?
(我输入'jo';它要求更多输入,所以我再次输入'jo'.然后它只打印出'jo'一次.)
我是编码的新手,我正在尝试do while使用嵌套if语句进行长循环,但是我遇到了让循环实际循环的问题.
我没有直接获得有关代码很长的项目的帮助,而是制作了一个简单的版本.它也不循环.它将结束并询问用户是否要再次尝试但是当输入"y"时它忽略if语句.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sodaChoice;
char answer = 'n';
do
{
cout << "Please choose your favorite soda from the following: Rootbeer, Diet Coke, Coke, Sprite" << "\n";
getline(cin, sodaChoice);
if (sodaChoice == "Rootbeer")
{
cout << "Me too!" << "\n";
}
else if (sodaChoice == "Diet")
{
cout << "Not me :(" << "\n";
}
else if (sodaChoice == "Coke")
{
cout << "It's alright" …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <string.h>
#include <ctype.h>
void delspace(char *str);
int main() {
int i, loops;
char s1[101], s2[101];
scanf("%d", &loops);
while (loops--) {
fgets(s1, 101, stdin);
fgets(s2, 101, stdin);
s1[strlen(s1)] = '\0';
s2[strlen(s2)] = '\0';
if (s1[0] == '\n' && s2[0] == '\n') {
printf("YES\n");
continue;
}
delspace(s1);
delspace(s2);
for (i = 0; s1[i] != '\0'; i++)
s1[i] = tolower(s1[i]);
for (i = 0; s2[i] != '\0'; i++)
s2[i] = tolower(s2[i]);
if (strcmp(s1, s2) == 0) {
printf("YES\n");
}
else …Run Code Online (Sandbox Code Playgroud)