为什么命令提示符在我开始之前显示数字?(Why does command prompt show numbers before I begin?)

编程入门 行业动态 更新时间:2024-10-26 23:26:18
为什么命令提示符在我开始之前显示数字?(Why does command prompt show numbers before I begin?)

命令提示符在程序开始之前显示数字。 为什么? 给出了2687688但数字不会写入文件?

#include <stdio.h> #include <conio.h> int main(void){ FILE*nfPtr; int n; if ((nfPtr=fopen("c:\\Users\\raphaeljones\\Desktop\\newfile.dat","w"))==NULL) { printf ("Sorry! The file cannot be opened\n"); } else {//else 1 begin printf("Enter numbers to be stored in file\n"); printf("%d",&n); while (!feof(stdin)){ fprintf(nfPtr,"%d",n); scanf("%d",&n); } }//else 1 ends fclose(nfPtr); getch(); return 0; }

The command prompt shows numbers before program begins. Why? 2687688 is given but the numbers won't write to file?

#include <stdio.h> #include <conio.h> int main(void){ FILE*nfPtr; int n; if ((nfPtr=fopen("c:\\Users\\raphaeljones\\Desktop\\newfile.dat","w"))==NULL) { printf ("Sorry! The file cannot be opened\n"); } else {//else 1 begin printf("Enter numbers to be stored in file\n"); printf("%d",&n); while (!feof(stdin)){ fprintf(nfPtr,"%d",n); scanf("%d",&n); } }//else 1 ends fclose(nfPtr); getch(); return 0; }

最满意答案

替代

printf("%d",&n);

scanf("%d",&n);

printf将格式指向的C字符串写入标准输出(stdout)

scanf从stdin读取数据

在您的代码中,您打印n,即未初始化,在"Enter numbers to be stored in file"字符串后打印出一个随机数。

Substitute

printf("%d",&n);

with

scanf("%d",&n);

printf Writes the C string pointed by format to the standard output (stdout)

scanf Reads data from stdin

In your code you are printing n, that is not initialized, that a random number is printed out after "Enter numbers to be stored in file" string.

更多推荐

本文发布于:2023-08-02 23:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382513.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在我   命令提示符   数字   command   show

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!