当我尝试编译使用gets()GCC函数的C代码时,
我明白了
警告:
(.text + 0x34):警告:`gets'函数很危险,不应该使用.
我记得这与堆栈保护和安全性有关,但我不确定为什么?
有人可以帮我删除这个警告并解释为什么会有这样的使用警告gets()?
如果gets()是如此危险,为什么我们不能删除它?
我知道大多数终端默认处于行缓冲模式.即输出被缓冲,并且在遇到换行字符之前不会被引导到stdout.
所以我希望它什么都不打印(至少在填充缓冲区之前):
int main() {
while(1) {
printf("Haha");
sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它确实在很短的时间内打印出来.
如果我想每秒打印"哈哈",我可以在printf之后printf("Haha\n")或者fflush(stdout)之后做.(我知道这不是那么便携,但它仍然是一个解决方案)
现在我回想起非常经典的scanf程序(我添加了while(1)循环以防止程序退出时缓冲区刷新):
int main() {
char char_in;
while(1) {
printf("Haha. Input sth here: ");
scanf("%c", &char_in);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在程序打印Haha. Input sth here:(并等待我的输入).如果我删除scanf语句,它不在这里.为什么会这样?
谢谢.
我有一个非常简单的代码将大写转换为小写:
#include <stdio.h>
int main()
{
char c;
int i=0;
for (i=0;i<10;i++){
c=getchar();
c=c-'A'+'a';
printf("%c\n",c );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但运行这个简单的代码总是*在输出时有一个额外的字符.它打印下面的char *.看一看:
D
d
*
D
d
*
E
e
*
Run Code Online (Sandbox Code Playgroud)
这是从哪里来的?
我正在尝试编写一个简单的程序,它将读取两个输入行,一个整数后跟一个字符串。然而,它似乎对我不起作用。
int main()
{
int i;
char str[1024];
scanf("%d", &i);
scanf("%[^\n]", str);
printf("%d\n", i);
printf("%s\n", str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输入整数并按“Enter”后,程序立即打印该整数。它不会等我输入字符串。怎么了?对此进行编程的正确方法是什么?
我有这段代码读取算术表达式,如1 + 2 * 3整数和字符:
int main() {
int d, flag = 0;
char c;
while ((flag = scanf("%d", &d)) != EOF)
{
if (flag == 1) // if integer was read sucessfully
{
// an integer has been read into variable 'd'
printf("%d,", d);
}
else // else if it was a character
{
// then read char into variable 'c'
c = getchar();
printf("%c,", c);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在这个代码在Windows和MacOS上用MingGW编译时可以正常工作,但不知何故在Linux上,它的字符+并-没有正确读取.
示例运行:
输入:1 …
我正在进行一个tic-tac-toe最终项目.我要求用户打印他想要填写的箱号,然后我使用整数err来获得scanf的返回值.在这种情况下,Scanf应该返回它已经读取的整数的数量,并且我要求读取一个整数,所以只要err!= 1,它就应该进入while循环.但是,这不起作用.
printf("\nBox number: ");
int boxNumber, err;
err = scanf("%d", &boxNumber);
while (err != 1) {
printf("\nWrong input.");
printf("\nBox number: ");
err = scanf("%d", &boxNumber);
}
Run Code Online (Sandbox Code Playgroud)
输入非整数时,输出为:
输入错误.
箱号:
在一个不停的循环中.该生产线,其中scanf函数,然后再要求用户输入一个值,就好像从来没有运行.
我不知道问题是什么.请帮忙.
在输出中,不打印最后一个字符.
Input: 3 3
abcabcabc
Expected Output: a b c a b c a b c
Actual Output: a b c a b c a b
Run Code Online (Sandbox Code Playgroud)
c ???在哪里?
#include <stdio.h>
int main() {
int i,j,k,n;
char a[3][3],b[3][3];
printf("enter size\n");
scanf("%d %d",&n,&k);
printf("enter character \n");
for(i=0;i<n;i++)
for(j=0;j<k;j++)
scanf("%c",&a[i][j]);
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<k;j++)
printf("%c ",a[i][j]);
return 0;
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(){
float x,y;
printf("Enter 2 numbers: \n");
scanf_s("%f %f", &x, &y);
if (getchar() == '\n')
{
printf("Division: %f\n", x / y);
printf("Product: %f\n", x * y);
printf("Sum: %f\n", x + y);
printf("Difference: %f\n", x - y);
}
else
{
printf("no Float-Value!");
}
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我们需要得到一个循环,所以如果我们输入错误的格式,程序应该再次要求输入两个数字
这听起来像是一个XY 问题,但我很困惑。
我想在用户输入6时执行一些代码。首先看一下代码:
#include<stdio.h>
int main(void) {
short int x;
printf("Val :");
scanf("%d", &x);
if (x == 6) {
//some code
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
SHRT_MAX在我的系统中是32767,如果用户设法输入65442,这最终将转换为6,并且代码将以此值执行,而它应该在6处执行。嗯,它在6 点执行,但从用户的角度来看,这是缺乏安全性的。是的,我可以使用intor long int,但是如果用户正在破解short int,那么这不是正确的选择。我该如何处理这个问题?
我scanf在C中遇到问题.请不要将这个问题标记为重复,因为我在这里搜索了很多这个问题并发现了许多相似的问题,但他们并没有完全回答我.
当我运行这个:
char c;
int a, b;
scanf("%d", &a);
scanf("%c", &c);
scanf("%d", &b);
Run Code Online (Sandbox Code Playgroud)
然后前两个scanf工作正常,但第三个完全跳过.我在这个论坛上搜索了关于这个问题的不同帖子,发现了很多信息,但想知道其他的东西.
我已经发现最简单的解决方案是:
scanf("%d %c %d", &a, &c, &b);
Run Code Online (Sandbox Code Playgroud)
另一个解决方案可能是使用:
getchar();
Run Code Online (Sandbox Code Playgroud)
而且我还发现我的问题背后的原因是它\n向缓冲区写了一个额外的新行字符,这就是跳过第3个字符的原因.但进一步研究,我发现,当我使用其他scanf的char类型的第二后scanf,然后它工作.这意味着,在我的情况下,如果我在integer类型后接受任何类型的输入,则会出现问题char.具有相对的情况下,他们无法采取再次输入我看到很多其他人的问题char后integer.现在我想澄清C中支持的确切方案scanf,因为我将遇到类似的问题,为什么char可以扫描char但integer不能扫描.谢谢大家.
我试图将多个单词输入和多行输入到一个数组中.但某些地方代码跳过获取输入并跳过结束程序.我已经尝试在'%s'(或'%s')之前和之后添加空格但是它不起作用(也许是因为它在循环中?).非常感谢任何人的帮助!如果我输入两三个以上的单词,它也会开始变得怪异:(我的目标是找到特定字母在所有这些单词和行中出现的次数.
#include <stdio.h> //include standard library
int main(){
int lineCount, occuranceCount;
printf("How many lines are you going to enter?");
scanf("%d", &lineCount);
char input[lineCount][100], searchChar;
for(int i=0; i<lineCount; i++){
printf("Please enter line #%d (100 characters of less):",i+1);
scanf("%s", &input[i]);
}
printf("What letter do you want to check the frequence in those lines? ");
scanf("%c", &searchChar);
for(int j=0; j<lineCount; j++){
for(int k=0; k<100; k++){
if(input[j][k] != '\0'){
if(input[j][k]==searchChar)
occuranceCount++;
}
}
}
printf("The letter occurs for %d time", occuranceCount);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图从给定的char数组中解析数学方程式,例如char equation[255] = "4+4";,我想将第一个数字复制到long number1,运算符到char oper第二个数字long number2.
我已经尝试了,sscanf(equation, "%d%c%d", &number1, &oper, &number2);但它只是得到第一个数字,无法提取运算符和第二个数字.
试过这个方法:
while (isdigit(equation[k]))
{
k++;
}
oper = equation[k];
Run Code Online (Sandbox Code Playgroud)
但它仍然没有得到运营商.有没有更简单的方法来解析c ++中的方程?
有人可以向我解释为什么ints从用户那里获取的内容scanf()存储在8h分开的地址中,即使int我的64位机器的大小是4字节?它与内存中的对齐?
#include <stdio.h>
void main() {
int *a;
int i, n;
printf(" Input the number of elements to store in the array : ");
scanf("%d",&n);
printf(" Input %d number of elements in the array : \n",n);
printf("size of on int is %d\n", sizeof(i));
for(i=0;i<n;i++) {
printf(" element - %d : ",i+1);
printf("address of a is %p\n", &a+i);
scanf("%d",a+i);
}
return 0;
}
Input the number of elements to store in the array …Run Code Online (Sandbox Code Playgroud)