如何调试我的代码(在C中),并了解我得到的错误和警告? [英] How do I debug my code( in C), and understand the errors and warnings that I get?

查看:61
本文介绍了如何调试我的代码(在C中),并了解我得到的错误和警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天要完成一项任务,但我对编程完全不熟悉,并且在运行代码方面遇到了一些问题。如果我理解错误和警告告诉我什么,那就没关系,但我不太明白。



这是我的C代码:



(我有#include< stdio.h> in开头也是int main(),这是我遇到问题的两个任务,其余的都很好)



(assignement1:

I have an assignment that I am trying to finish today, but I am completely new to programming and are having some problems with running the code. It would be okay if I understood what the errors and warnings were telling me, but I don´t quite understand.

here is my code in C:

( i have #include<stdio.h> in the beginning and also int main(), this is the two tasks that I am having problems with, the rest are fine)

(assignement1:

Quote:

编写一个C函数,用于确定一个数字是否具有给定的素数因子。例如,如果使用该函数调用该函数值10为数字,5为primeFactor,函数应返回1,表示5是10的素数因子。同样,如果示例中的数字为9,则函数应返回0.

C函数应具有以下签名:



int myPrimeFactor(int number,int primeFactor);)







Write a C function that determines whether a number have a given prime factor. For example, if the function is called with the value 10 as number and 5 as primeFactor, the function should return 1 indicating that 5 is a prime factor of 10. Similarly, the function should return 0 if the number in the example was 9.
The C Function should have the following signature:

int myPrimeFactor(int number, int primeFactor);)



}

int myPrimeFactor(int number, int myprimeFactor){


    printf("Enter one integer and one primeFactor\n:  ");
    scanf("%d%d", &number,&myprimeFactor);

  if (number%myprimeFactor==0)
  	printf("This is a prime factor of number ");
  else if (myprimeFactor%number==0)
  	printf("This is a prime factor of number");
  else
  	printf("This is not a prime factor of number");

    return 0;

  }







(作业2:




(assignment 2:

Quote:

Quote:

编写一个C函数,打印给定范围内的数字,同时指示数字是奇数还是偶数,5是否是数字中的素数因子。必须使用myPrimeFactor函数来确定5是否是该函数应该打印如下:

1是奇数而5不是素因子



2是偶数,5是不是一个主要因素)





Write a C function that prints numbers in a given range, along with an indication of whether the number is odd or even and whether 5 is a prime factor in the numbers. You must use the myPrimeFactor function to determine whether 5 is a prime factor of the number. The function should print something like:
1 is odd and 5 is not a prime factor

2 is even and 5 is not a prime factor)


void myNumbers(int startnum, int endnum){

for (startnum=1;endnum<=50;startnum++)
{
  if(startnum%2 == 0 %% startnum%5==0);
      printf("%d is even and 5 is a primeFactor", startnum);

if(startnum%2==0 %% startnum%5!==0);
    printf("%d is even and 5 is not a primeFactor", startnum);

  else if(startnum%2!==0 %% startnum%5==0);
      printf("%d is not even and 5 is a primeFactor", startnum);

  else
    printf("%d is not even and 5 is not a PrimeFactor", startnum);

  }

}







也这是我得到的错误




also this is the bugs that I am getting

8 errors generated.

startriangle_edit.c:67:23: error: expected expression
  if(startnum%2 == 0 %% startnum%5==0);
                      ^
startriangle_edit.c:70:19: error: expected expression
if(startnum%2==0 %% startnum%5!==0);
                  ^
startriangle_edit.c:70:33: error: expected expression
if(startnum%2==0 %% startnum%5!==0);
                                ^
startriangle_edit.c:73:3: error: expected expression
  else if(startnum%2!==0 %% startnum%5==0);
  ^
startriangle_edit.c:76:3: error: expected expression
  else
  ^
startriangle_edit.c:98:15: error: expected expression
    myNumbers(int startnum, int endnum);
              ^
startriangle_edit.c:98:29: error: expected expression
    myNumbers(int startnum, int endnum);
                            ^





我尝试了什么:



我试过更改大括号({})和;看看是否有帮助。另外我不确定某些代码是否正确,特别是在编写startnum / endnum时,以及使用%% as和。



What I have tried:

I have tried changing the braces ({}) and the ; to see if that helped. Also I was not sure if some of the code was right, especially when to write startnum/endnum, and if it was correct to use %% as and.

推荐答案

在你得到一个干净的编译之前,调试器不会帮助你 - 在你的级别,你应该摆脱所有的编译错误和警告。在编译之前你无法调试代码,因为如果只有一个编译器错误就没有生成EXE。



所以首先仔细查看你的错误信息:

The debugger won't help you until you get a clean compile - and at your level, you should get rid of all compilation errors and warnings. You can't debug code until it compiles, as there is no EXE produced if there is a single compiler error.

So start by looking at your error messages closely:
startriangle_edit.c:67:23: warning: implicit declaration of function 'and' is
      invalid in C99 [-Wimplicit-function-declaration]
  if(startnum%2 == 0) and (startnum%5==0);

它抱怨你在该行代码中使用和这个词,这是正确的,因为逻辑AND在C中是&&,和。所以改变这一行:

It's complaining about your use of the word "and"in that line of code, and rightly so, because "logical AND" in C is "&&", nort "and". So change that line to this:

if(startnum%2 == 0) && (startnum%5==0);

这会产生另一个错误,因为你需要围绕整个条件的括号:

That'll generate another error, because you need brackets round the whole condition:

if (a)
   {
   ...
   }

所以也要添加它们:

So add them as well:

if( (startnum % 2 == 0) && (startnum % 5 == 0));

不是你越来越近了。

但是...条件结束时的分号意味着;语句的结尾,所以你不想要那个:

Not you are getting closer.
But ... the semicolon at the end of the condition means that;'s the end of the statement, so you don't want that either:

if( (startnum % 2 == 0) && (startnum % 5 == 0))



那应该摆脱那些行问题,所以编译再次,看看它抱怨的第一个错误(通常修复一个错误摆脱其他错误,因为编译器不会混淆你想要再说什么)。



当你修复它们之后,你可以通过在调试器中运行它来开始调试你的代码,但直到那时才开始调试。


That should get rid of that lines problems, so compile it again, and look at the first error it is complaining about this time (often fixing one error gets rid of others, because the compiler isn't confused as to what you are trying to say any more).

When you have fixed them all, you can start debugging your code by running it in the debugger, but not until then.


the erro r你使用假令牌。你需要使用&&在if之后你应该/可以执行一些代码。有时编译器不理解代码并在稍后显示错误。阅读一些教程以获取更多详细信息。

The error that you use the false token. You need to use "&&" and after the if you should/can execute some code. Sometimes compilers dont understand the code and show the error at a later point. Read some tutorials for further detail.
 if(startnum%2 == 0 && startnum%5==0) {
//do something
}

为了清晰起见,我喜欢使用额外的括号

I like using additional braces for clarity

 if((startnum%2 == 0) && (startnum%5==0) ) {
//do something
} else {
//do something if NOT true
}


这篇关于如何调试我的代码(在C中),并了解我得到的错误和警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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