一个没有错误但总是返回0值的简单程序asanswer.why? [英] A simple programme with no error but always return 0 value asanswer.why?

查看:69
本文介绍了一个没有错误但总是返回0值的简单程序asanswer.why?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
int main()
{
	int a,b,c;
	printf("enter any no. to find its absolute ");
	scanf("%d",&a);
	b=c*a;
	if (a<0)
		c=-1;
	else	
		c=1;
	printf("absolute of your number is %d",b);
	}





它总是给出绝对数字是0


我尝试过的事情:



显然还有其他方法可以找到绝对的。但我只是想知道这个程序有什么问题。为什么它不起作用?



It always gives "absolute of your number is 0"

What I have tried:

Obviously there are other ways to find absolute. But I just want to know what is wrong in this programme. Why does it not work?

推荐答案

在为其赋值之前,您正在使用变量 c 。打印结果取决于变量 c 的初始值,在您的情况下显然为零。所以在使用之前设置变量:

You are using the variable c before assigning a value to it. The printed result depends on the initial value of the variable c which is obviously zero in your case. So set the variable before using it:
if (a<0)
    c=-1;
else
    c=1;
b=c*a;


因为你没有给 c 一个值在你将它乘以 a 之前 - 零次任何东西总是为零。



BTW:编译没有错误没有; t表示你有一个没有错误的程序 - 编译是让任何工作变得有效的第一阶段,就像用英文而不是德语写电子邮件一样 - 电子邮件是可读的,但这并不意味着它可以感觉或它带有你想要的信息。一旦你让程序编译没有错误,这只意味着你可以开始测试它以确保它符合规范!
Because you don't give a value to c before you multiply it by a - and zero times anything is always zero.

BTW: Compiling without error doesn;t mean you have a "programme with no error" - compilation is the first stage of getting anything working, it's like writing an email in English instead of German - the email will be readable, but that doesn't mean that it makes any sense or that it carries the information you wanted it to. Once you get the program to compile without errors, that just means you can start testing it to ensure it does meet the specification!


引用:

但我只是想知道这个程序有什么问题。为什么不起作用?

But I just want to know what is wrong in this programme. Why does it not work?



当你需要注意c的值


Pay attention to the value of c when you

b=c*a;



你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:运行你的代码在调试器上一步一步,检查变量。

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

调试器中没有魔法,它不知道你应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



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



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

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

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


Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于一个没有错误但总是返回0值的简单程序asanswer.why?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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