Tag: 格式说明符

printf不产生输入值

我有以下代码块。 最后一行用于产生用户输入的X , Y和P值的值。 但它只会返回 (0,0,0) 而不是用户给出的值。 我错过了什么? printf(“What is the robot’s initial X position? (cm)\n”); scanf(“%f”,&X); printf(“What is the robot’s initial Y position? (cm)\n”); scanf(“%f”,&Y); printf(“What is the robot’s initial angular position? (degrees)\n”); scanf(“%f”,&P); printf(“The initial position is (%d, %d, %d)\n”, X,Y,P);

C 中scanf函数的格式说明符中%c规范之前的空格

如果我在以下程序中的scanf函数的格式字符串中不包含%d和%c规范之间的空格,并在运行时将输入设置为“4 h”,则输出为“Integer = 4且Character = 。 在这种情况下,变量“c”如何获取输入,如果我在%d和%c规范之间包含空格,它会有什么不同? #include main() { char c; int i; printf(“Enter an Integer and a character : \n”); scanf(“%d %c”,&i,&c); printf(“Integer = %d and Character = %c\n”,i,c); getch(); }

什么`scanf(“%* %* c”)`是什么意思?

我想在C中创建一个循环,当程序要求一个整数并且用户键入一个非数字字符时,程序再次请求一个整数。 我刚刚找到了以下代码。 但我不明白这意味着什么是scanf(“%*[^\n]%*c”) 。 ^\n是什么意思? ^\n和c之前的*是什么意思? /* This program calculate the mean score of an user 4 individual scores, and outputs the mean and a final grade Input: score1, score2,score2, score3 Output: Mean, FinalGrade */ #include //#include int main(void){ int userScore = 0; //Stores the scores that the user inputs float meanValue = 0.0f; //Stores […]

sprintf for unsigned _int64

我有以下代码。 sprintf中第二个%d的输出始终显示为零。 我想我指的是错误的说明符。 任何人都可以帮助我获得具有正确值的写字符串。 这必须以posix标准实现。 感谢您的投入 void main() { unsigned _int64 dbFileSize = 99; unsigned _int64 fileSize = 100; char buf[128]; memset(buf, 0x00, 128); sprintf(buf, “\nOD DB File Size = %d bytes \t XML file size = %d bytes”, fileSize, dbFileSize); printf(“The string is %s “, buf); } 输出: The string is OD DB File […]

为什么scanf(“%d”,)不消耗’\ n’? 而scanf(“%c”)呢?

在这里 ,我在接受的答案中看到了这个陈述: 大多数转换说明符跳过前导空格,包括换行符,但%c不跳过。 对我来说,不清楚这种不同行为的基本原理,我会期待一个统一的行为(例如总是跳过或从不)。 我用这样的一段C代码遇到了这种问题: #include “stdio.h” int main(void){ char ch; int actualNum; printf(“Insert a number: “); scanf(“%d”, &actualNum); // getchar(); printf(“Insert a character: “); scanf(“%c”, &ch); return 0; } 交换两个scanf解决了问题,以及(注释) getchar ,否则第一次插入的’\n’将由第二个scanf消耗%c 。 我在linux和windows上测试了gcc,行为是一样的: gcc(GCC)4.7.2 20120921(Red Hat 4.7.2-2) 版权所有(C)2012 Free Software Foundation,Inc。 这是免费软件; 查看复制条件的来源。 没有保修; 甚至不适用于适销性或特定用途的适用性。 所以我的问题是:为什么%d和%c行为与scanf ‘\n’不同?

当我使用错误的格式说明符时会发生什么?

只是想知道当我在C中使用错误的格式说明符时会发生什么? 例如: x = ‘A’; printf(“%c\n”, x); printf(“%d\n”, x); x = 65; printf(“%c\n”, x); printf(“%d\n”, x); x = 128; printf(“%d\n”, x);