Tag: scanf

Scanf(“%c%f%d%c”)返回奇怪的值

我的类赋值要求我提示用户在一个输入行中输入四个变量char float int char。 这是整个代码: #include #include #include #include int main(void){ char h = ‘a’; char b, c, d, e; int m, n, o; float y, z, x; short shrt = SHRT_MAX; double inf = HUGE_VAL; printf(“Program: Data Exercises\n”); printf(“%c\n”, h); printf(“%d\n”, h); printf(“%d\n”, shrt); printf(“%f\n”, inf); printf(“Enter char int char float: “); scanf(“%c %d […]

虽然scanf!= EOF或scanf == 1?

Ceteris paribus (格式良好的数据,良好的缓冲实践以及不适用的东西),有什么理由我喜欢循环而scanf的返回值是1,而不是!EOF ? 我可能已经在某个地方读过这个,或者其他什么,但我也可能错了。 其他人怎么想?

当第一个跟踪失败时,为什么我不能再对同一个变量使用scanf

请参阅以下代码: #include int main(int argc, char const *argv[]) { int n; int i = scanf(“%d”, &n); printf(“%d\n”, i); int j = scanf(“%d”, &n); printf(“%d\n”, j); return 0; } 假设我第一次为n输入一个字符串, scanf将返回0 ; 但是,为什么它不允许我第二次输入n的值而j是自动0 ? 即使我第一次键入错误的值,我该怎么做才能让我再次输入n值?

在文本文件中扫描到链接列表

我只是在学习链接列表,我必须做一个包含许多部分的任务,但我已经开始了,我需要做的第一件事是将输入文件读入链表。 部分文件是: George Washington, 2345678 John Adams, 3456789 Thomas Jefferson, 4567890 James Madison, 0987654 James Monroe, 9876543 John Quincy Adams, 8765432 并包含总共26行。 我现在要做的只是简单地在文件中读取。 我尝试使用此代码(现在在main中) #include #include struct node{ char name[20]; int id; struct node *next; }*head; int main(void){ struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); head = temp; FILE *ifp; ifp = fopen(“AssignmentOneInput.txt”, […]

scanf扫描后从输入中删除了什么?

我经历了很多问题,博客和书籍,但我想我无法正确地解决问题。 scanf扫描后做了什么? 它读取除了空格,制表符和换行符之外的所有内容,但它对换行有什么作用? 扫描输入后,我无法以任何方式删除换行符。 例如,将此视为输入: – 五 ABCDEFGH 现在,我已将5扫描为整数, scanf(“%d”, &i); 但是如何删除’\ n’以便一次读取一个字符前面的所有内容? 我想用: – while((c=getchar())!=’\n’){//Do something} ‘\ n’应该是字符串之后的那个,但是getchar()在5之后得到一个并且循环在甚至运行一次之前终止。 我想我只是错过了一个小技巧。 scanf(“%d”, &i); while((c=getchar())!=’\n’){ //Do something with the character } 输入文件:- 五 ABCDEF 期待while循环运行,而它不运行。 因为它在5.之后捕获’\ n’。我尝试在scanf()和while()之间添加getchar(),但是程序卡住并且什么都不做。

printf与scanf请求输入。 BST循环错误

我一直试图看看为什么printf在打印文件输入后没有打破循环。 .c文件是BST,我现在正在测试是否已经构建了一个树,但似乎无法退出printf循环。 我需要printf循环来获得代码的正确输出。 有关此错误发生原因的任何建议。 显示完整的代码和输出。 #include “bst.h” #include #include ///Author: Joe W //arbitrary list of temp nodes TreeNode *new_node, *root, *tmp, *parent; int elemArray[100], i1, i2, i0; /* Insert a new node into the tree by referencing the root and using recursion */ TreeNode* getN(int dataElem) { TreeNode* temp; temp = malloc(sizeof(TreeNode*)); temp-> left = […]

for循环运行的时间比指定的多吗?

所以我的代码所做的是它接收一个数字列表并输出所有数字的连续滚动平均值。 我的scanf函数运行了一个额外的时间,所以我只是把它放入零。 我怎样才能解决这个问题? 这是我有的: #include #include #include int main(){ int i,n; double numbers[100]; double previous[100]; double x[100]; double mean[100]; double old_average[100]; double new_average[100]; printf(“Total amount of numbers: “); scanf(“%d\n”, &n); for (i=1; i<n+1; i++){ scanf("%lf\n", &numbers[i]); } old_average[1] = numbers[1] / 1; for (i=1; i<n+1; i++){ new_average[i] = (((old_average[i] * (i)) + numbers[i+1]) / (i+1)); […]

C编程 – Scanf无法在ubuntu中运行

我正在Ubuntu 10中编写一个C程序来创建进程,显示进程ID并终止进程。 我正在使用kill()命令来终止用户通过scanf输入的进程ID。 但是,scanf根本不起作用。 我尝试在%d之前添加“空格”但没有发生任何事情。 感谢是否有人可以提供帮助! 以下是我的代码: include include include include include main () { int x; int pid[10]; // to store fork return value int p[10]; // to store process ID // Create 5 new processes and store its process ID for (x=1;x<=5;x++) { if ((pid[x]=fork())==0) { p[x]=getpid(); printf("\n I am process: %d, my […]

‘%’和格式说明符之间的数字在scanf中的含义是什么?

我知道printf()格式说明符中使用的选项,但我对%3d含义完全无能为力,如下面的代码所示。 scanf(“%3d %3d”,&num1,&num2); 一般来说, %和format specifier之间的数字在scanf语句中表示什么。 不是scanf()意味着只是接收输入并将其存储在参数中指定的地址中吗?

为什么fgets卡在回车上\ r?

我是新来的论坛和c一般,所以请耐心等待我。 我正在尝试编写一个带有文本文件并解析所有单词和字符的ac程序,然后将它们保存到输出文本文件中。 我正在使用C99,Windows 7-64bit,MinGW,记事本,记事本++和用于txt文件的ASNI格式。 我已经读过fgets()比fscanf用于读取输入更好,因为它有缓冲区溢出保护,所以我决定尝试使用它,但它在测试文件中有一些标点符号的问题(我认为这是回车\ r)。 我尝试使用fscanf,除了它跳过所有空格(我可以在以后添加,不关心它),它似乎接受所有文本就好并将其打印在输出文件中。 这是我的测试代码: #include #include #include #include #include void main(int argc, char* argv[]) { int limit=100, flimit=0, flimitmax=1900000000; //I stopped flimitmax short of the 2GB mark char name[limit], copyname[limit]; FILE *data, *output; //Gets the value of a specified data file for reading printf(“\nPlease specify a file to format for data […]