Tag: 扫描

如果我在末尾添加一个新行’\ n’,scanf如何工作

包括 int main(){ int a,b,c; scanf(“%d%d%d\n”,&a,&b,&c); printf(“%d %d %d”,a,b,c); return 0; } 看看我正在接受3个输入并打印3个输出……但是当我在scanf函数结束时添加了一个新行时,我必须给4个输入来获得3个输出(我得到前3个作为输入)… .scanf如何在这里工作? 在这种情况下: 包括 int main(){ double n[2][5]; int i,j; for (i=0;i<=1;i++){ for(j=0;j<=4;j++){ scanf("%lf\n",&n[i][j]); printf("Class=%d Roll=%d Marks=%lf\n",i+6,j+1,n[i][j]); } } return 0; } 看看我必须提供11个输入才能获得10个输出…每次我输入一个输入时,我将前一个输入作为输出…… scanf如何在这里工作?

为什么scanf对大浮点数输错了?

#include int main() { float k; scanf(“%f”, &k); printf(“%f”, k); } 在这个简单的程序中,当我输入一个包含最多8位数的数字时,它会正确显示。 但如果我超过8位,即对于输入123456789 ,输出为123456792 。 为什么会这样? 好有趣的事实是,如果我在123456789和123456796之间输入任何数字,那么它总是显示123456792 。 它是否与浮点数的8位小数精度有关?

C中的fscanf没有读完整行?

这非常简单,但我只是遇到了问题。 文本文件有一个标题, 例如, # Avizo BINARY-LITTLE-ENDIAN 2.1 define Lattice 496 384 470 Parameters { AlignTransform { slice0000 0 -0 -30 -1, slice0001 0 -0 -30 -1, slice0002 0 -0 -30 -1, 我正在尝试使用fscanf读取每一行。 int i; for ( i = 0; i < 10; i++ ) { fscanf(fp, "%s\n", buf); printf("%d) %s\n",i,buf); } 导致这个 0) # 1) […]

C中的scanf和字符串的分段错误

我是c的初学者,我遇到了scanf和字符串的问题。 这是我写的问题的一个例子。 #include #include int main(void) { char* string; scanf(“%s”, &string); if (strcmp(string, “Foo”) == 0) //segmentation fault here printf(“Bar”); } 基本上,这个代码编译,但是当我运行它时,我在strcmp()中得到一个分段错误 如果我用“&string”替换该行中的“string”,它可以工作,但是我从编译器中得到了这个错误 /usr/include/stdio.h:362:12: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’ 这让我觉得这个解决方案并不是很理想。 如果我声明这样的字符串: char string[100]; 它没有任何警告,但这也不理想,因为我不确定字符串的大小。 有没有更好的解决方案我在这里缺少,或者这些是我唯一的选择? 谢谢。

Scanf没有读取双倍

我试图使用scanf连续读取用户的double值。 码: printf(“Enter A value: \n”); double input; int result = scanf(“%f”, &input); printf(“INPUT: %f\n”, input); 输出是 INPUT: 0.000

使用C中的scanf()从stdin读取输入

假设输入是: 6 4 0 1 2 2 1 0 1 0 0 0 0 1 1 0 0 0 0 1 0 1 1 1 1 0 图6和4分别是宽度和高度。 我用的是: scanf(“%d %d”, &width, &height); 然后我使用for循环将其余输入放在2D数组(board [height] [width])中。 问题是关于validation宽度和高度。 当我输入 6 4 0 1 2 2 1 0 1 0 0 0 0 1 1 0 0 […]

扫描期间C中的分段错误

我试图扫描一个整数用于我的程序。 但是我的程序在编译期间给了我分段错误,这是给我错误的部分: int main(void) { int totalHeight=0, floorWidth=0, amountOfStories, amountWindowForTop, amountWindowForMiddle, amountWindowForBottom, windowHeight, middleWindowWidth, topWindowWidth, bottomWindowWidth, minimumHeight, minimumWidth; char topFloorWindowContent, middleFloorWindowContent, bottomFloorWindowContent, windowBorder, floorBorder; int tempMax; printf(“please enter how many stories your building would like to have: “); scanf(“%d”,&amountOfStories); minimumHeight=amountOfStories*6+1; while((totalHeight<minimumHeight)||((totalHeight%amountOfStories)!=1)) { printf("please enter the totalHeight (minimum %d): ",minimumHeight); scanf("%d",&totalHeight); } printf("please enter how many […]

scanf忽略,无限循环

int flag = 0; int price = 0; while (flag==0) { printf(“\nEnter Product price: “); scanf(“%d”,&price); if (price==0) printf(“input not valid\n”); else flag=1; } 当我输入有效数字时,循环按预期结束。 但是如果我输入一些不是数字的东西,比如hello ,那么代码会进入无限循环。 它只是保持打印Enter Product price:并且input not valid 。 但它不等我输入一个新号码。 这是为什么?

扫描后的SegFault?

#include #define TimeConverter 60 #define TempFormula time * time * 4 / time + 2 – 20 double HoursMinToTime(int hour, int min); double Temperature(double time); int main() { int hour, min; double time, temperature; printf(“Hours and minutes: “); scanf(“%d %d”, hour, min); //Segfault HERE time = HoursMinToTime(hour, min); temperature = Temperature(time); printf(“After a %lf hour […]