简体   繁体   English

当我尝试 output 我的代码在 C 时出现警告。(在赋值周围放置括号以消除此警告等)

[英]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。通常,您将得到\nEOF以指示您已到达行尾或文件尾。 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 .否则你永远无法区分值255EOF

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.

相关问题 在 C 中赋值后结构不保持值。当我尝试打印时不显示值 - Structure not holding value after assignment in C. No Value is displayed when I try to print C中的双指针,我的代码的分配警告 - Double pointer in c, assignment warning for my code 编译器警告 - 建议用作真值的赋值括号 - Compiler warning - suggest parentheses around assignment used as truth value 警告:建议在赋值周围使用括号作为真值 [-Wparentheses] - warning: suggest parentheses around assignment used as truth value [-Wparentheses] Objective C /C。当我尝试在IOS设备上获取Mac Adress时,我得到了错误的价值 - Objective C / C. I get wrong Value when i try to get Mac Adress on IOS device C 中的 fork()。 我需要解释这段代码 - fork() in C. I need explanation on this code 不断收到警告“建议在 '||' 内的 '&amp;&amp;' 周围加上括号适用于 C 程序 - Keep getting warning "Suggest parentheses around '&&' within '||' for C program 我的代码有什么问题:当我尝试用C计算欧拉数e时,输出错误的值? - What's wrong with my code: wrong value output when I try to calculate Euler's number, e, in C? 当我尝试压缩代码时出现错误(C) - Getting errors when I try and condense my code down ( C ) 每当我尝试在 C 中编码时,我都会不断收到此消息 [10:1: error: expected identifier or ')']。 有人可以提供解决方案吗? - I keep getting this message [10:1: error: expected identifier or ')'] whenever I try to code in C. Can someone provide a solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM