scanf函数中的什么/ n在我的程序中执行 [英] What /n in scanf function doing in my program

查看:92
本文介绍了scanf函数中的什么/ n在我的程序中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从scanf函数中删除\ n然后我的代码运行良好,它不会要求第二个输入,它没有任何作用,并且当我输入0然后它不跳转到下一行既不停止程序但是之后再次给0输入它会停止程序,所以PLZ解释了什么在这里扮演角色以及它在程序中创造了什么区别



我尝试过:



if i remove \n from scanf function then my code runs well and it doesnot ask for second input which has no role in it and and also when i enter 0 then it doesnot jumpto next line neither it stops the program but after giving 0 input again it stops the program so plz explain what \n is playing role here and what difference it created in program

What I have tried:

#include<stdio.h>
#include<stdlib.h>
int main()
{
float testno=0,scoreentered,sum=0,avg;
printf("enter 0 to stopthe program\n");
do
{
    printf("testno:%f avg:%f\n",testno,avg);
    printf("enter score");
    scanf("%f\n",&scoreentered);
    sum+=scoreentered;
    testno++;
    avg=sum/testno;
}
while(scoreentered!=0);
}



输出


output

enter 0 to stopthe program
testno:0.000000 avg:0.000000
enter score1
3
testno:1.000000 avg:1.000000
enter score0
testno:2.000000 avg:2.000000
enter score0
~/workspace/ $ 

推荐答案


阅读文档和学习调试器是一个良好的开始了解你的程序。



当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
reading documentation and learning debugger is a good start to understand your programs.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于scanf函数中的什么/ n在我的程序中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆