[英]Warnings when I try to output my code in C. (place parentheses around assignment to silence this warning, etc)
This is my code.这是我的代码。 I am getting some weird warning's when I run it and I'm confused on why.
我在运行它时收到一些奇怪的警告,我对原因感到困惑。 Warnings on the bottom.
底部警告。 My code will still run and the output is correct, however I still want to understand and fix the warnings thanks.
我的代码仍会运行并且 output 是正确的,但是我仍然想了解并修复警告,谢谢。 I tried to fix all the warnings, however when I do my code won't output correctly anymore.
我试图修复所有警告,但是当我这样做时,我的代码将不再正确 output 。 My program fails.
我的程序失败了。 So I am confused.
所以我很困惑。 Please help!
请帮忙!
#include <stdio.h>
#include <stdlib.h>
char s1(char *random);
char s2(char *s2_input, int index);
char strfilter(char *random, char *s2_input, char replacement);
int main()
{
int s1_index = 41;
char s1_random[s1_index];
s1(s1_random);
printf("\ns1 = ");
puts(s1_random);
printf("s2 = ");
int s2_index = 21;
char s2_input[s2_index];
s2(s2_input, s2_index);
if(s2_input[1] == '\0')
{
printf("size too small");
exit(0);
}
printf("ch = ");
char replacement = getchar();
printf("\n");
int filter_index = 41;
strfilter(s1_random, s2_input, replacement);
printf("\ns1 filtered = ");
puts(s1_random);
}
char s1(char *random)
{
int limit = 0;
char characters;
while(characters = ('A' + (rand() % 26))) /* random generatro */
{
if(limit == 41)
{
*(random + 41 - 1) = '\0';
break;
}
*(random + limit) = characters;
limit++;
}
}
char s2(char *s2_input, int index)
{
char array[21] = "123456789012345678901"; /* populated array to make sure no random memory is made */
char input;
int count = 0;
int check = 0;
while(input = getchar() )
{
if(input == '\n')
{
*(s2_input + count) = '\0';
break;
}
else if(input < 65 || input > 90)
{
printf("invalid input");
exit(0);
}
*(s2_input + count) = input;
count++;
}
index = count;
}
char strfilter(char *random, char *s2_input, char replacement) /* replacement function */
{
while(*s2_input)
{
char *temp = random;
while(*temp)
{
if(*temp == *s2_input)
*temp = replacement;
temp++;
}
s2_input++;
}
}
** Error message: ** ** 错误信息: **
matthew.c:41:22: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
while(characters = ('A' + (rand() % 26))) /* random generatro */
~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
matthew.c:41:22: note: place parentheses around the assignment to silence this warning
while(characters = ('A' + (rand() % 26))) /* random generatro */
^
( )
matthew.c:41:22: note: use '==' to turn this assignment into an equality comparison
while(characters = ('A' + (rand() % 26))) /* random generatro */
^
==
matthew.c:51:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
matthew.c:61:17: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
while(input = getchar() )
~~~~~~^~~~~~~~~~~
matthew.c:61:17: note: place parentheses around the assignment to silence this warning
while(input = getchar() )
^
( )
matthew.c:61:17: note: use '==' to turn this assignment into an equality comparison
while(input = getchar() )
^
==
matthew.c:80:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
}
^
matthew.c:96:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
5 warnings generated.
I tried what the compiler says to do, such as ==, or adding the parenthesis but then my code won't run correctly if I do that.我尝试了编译器要求的操作,例如 ==,或添加括号,但如果我这样做,我的代码将无法正确运行。 So I am confused.
所以我很困惑。
I tried what the compiler says to do, such as ==, ...
我尝试了编译器说要做的事情,例如 ==,...
You should not just try any suggestion without understanding it.你不应该在不理解的情况下尝试任何建议。 Your code is basically correct.
你的代码基本上是正确的。 You want an assignment.
你想要一个任务。
But assignments in conditions is a very common case of errors.但是条件赋值是一种非常常见的错误情况。 In most cases, there are comparisons used in conditions.
在大多数情况下,条件中使用比较。 The compiler does just warns you that this might be wrong.
编译器只是警告您这可能是错误的。
Making it a comparison does not make any sense for you.进行比较对您来说没有任何意义。 What you should do instead is using extra brackets:
你应该做的是使用额外的括号:
while(characters = ('A' + (rand() % 26)))
should be changed into应该改成
while ((characters = ('A' + (rand() % 26))) != 0)
In the next message you have an additional problem:在下一条消息中,您还有一个问题:
while(input = getchar() )
Here the reason for the warning is the same: Might be a typo in a comparison.这里警告的原因是一样的:可能是比较中的错字。 But this time, you are missing the actual condition.
但这一次,你错过了实际情况。 This loop will iterate until
input
will become 0. Normally, you will get \n
or EOF
to indicate you reached the end of line or end of file.此循环将迭代直到
input
变为 0。通常,您将得到\n
或EOF
以指示您已到达行尾或文件尾。 Also, getchar
returns an int
which means you must use an int
variable to store the result.此外,
getchar
返回一个int
,这意味着您必须使用int
变量来存储结果。 Otherwise you could never distinguish value 255
from EOF
.否则你永远无法区分值
255
和EOF
。
This line should be这条线应该是
while ((input = getchar()) != EOF )
Regarding your other type of warning:关于您的其他类型的警告:
matthew.c:51:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
I think, that is self explaining.我认为,这是自我解释。 If you define a function with a return type, you are supposed to return something.
如果您定义一个带有返回类型的 function,您应该返回一些东西。
If you don't need any return value of that function, define it as void
function.如果您不需要该 function 的任何返回值,请将其定义为
void
function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.