1

I'm writing a program with C and this is one of my functions:

void group3()
{
    char select[5];
    printf("\nDetails etc.");
    printf("\nPlease confirm Yes/No if you would like to subscribe:");
    scanf("%s", select);
    if (select == "Yes")
        printf("Good.");
    else
    {
        printf("Alright.");
        main();
    }
}

But when I try to compile and debug the program, a warning called "return value ignored: 'scanf'" appears. I noticed that the warning appears when I'm running scanf for integers too but the program still works for integers. However, when I try to scanf for strings, it doesn't work. I've tried using getchar() but the same warning occurs for getchar(). I've also tried using scanf_s but the same problem appears when I try to scanf_s for strings. I've also defined _CRT_SECURE_NO_WARNINGS in my program. Would really appreciate it if someone could explain to me how I can eliminate this warning from occurring/get the scanf or getchar to work with strings.

edit: thanks to some helpful users i found out that the problem isn't because the string wasn't read, but because i cannot compare strings using the if statement this way. the answer is in the comment section and i can't select it as the correct answer to this but this question is closed. thank you :)

cloud
  • 105
  • 9
  • 1
    out of that do you really want to call `main()` in your function ??? – bruno Oct 17 '20 at 09:40
  • @user3121023 thanks for the advice. :) however i'm still looking for a solution as to why scanf isn't reading the string here because if scanf doesn't read the string i still can't use the if statement for it – cloud Oct 17 '20 at 10:13
  • @user3121023 ohh okay i tried it out and i get what you mean now! it was my mistake, sorry and tysm!! i rly appreciate it, have a nice day! :D – cloud Oct 17 '20 at 10:23

1 Answers1

2

The problem is that the return value of scanf() is ignored, so don't ignore that to eliminate the warning.

scanf() returns the number of data read on success and negative number on error, so it is useful to check if it successfully read required things.

Example:

    if (scanf("%4s", select) != 1) {
        fputs("read error\n", stderr);
        exit(1);
    }

Also it is good to read the length to read to avoid buffer overrun. This is also done in the above example.

One more point is that select == "Yes" is not a correct way to compare strings in C. This is comparing pointers and it won't be true because the array and the string literal are stored in differenct places in memory. strcmp() function from string.h is useful to compare strings.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • may be also a remark in your answer about the call of *main* ? – bruno Oct 17 '20 at 09:52
  • @bruno Calling `main()` recursively is allowed in C. I cannot say that this is wrong from this code. – MikeCAT Oct 17 '20 at 09:54
  • at least this is suspicious ;-) – bruno Oct 17 '20 at 09:55
  • sorry i'm kind of new to C so i'm not rly sure what you mean but i tried running the code above with my program and no error message nor negative number pops up... how to i fix the error of scanf not successfully reading the string? – cloud Oct 17 '20 at 09:56
  • @cmy in the current case scanf will not success if you reach end of file before its call, only you can decide what to do in that case ;-) – bruno Oct 17 '20 at 10:17
  • @MikeCAT hi, i just found out that the problem isn't because the string wasn't read, but because i shouldn't have used the if statement that way. i do appreciate your word of advice tho! tysm, have a nice day! :) – cloud Oct 17 '20 at 10:27