Home > OS >  I compiled this program and the compiler keeps giving me this error
I compiled this program and the compiler keeps giving me this error

Time:12-17

#include <stdio.h>
#include <ctype.h>

int main(void) {
  int j;
  scanf ("%d",&j);
  if (j>=1&&j<=10){
    int i=0;
    int USER_NAME=100;
    char name[j][USER_NAME];
    for (i=0;i<j;i  ){
      scanf ("%s",name[i]);
    }
    for (i=0;i<j;i  ){
      if ((i%2)==0){
        i  ;
      }
      if ((i%2)!=0){
        printf ("%s\n",name[i]);
      }
    }

  }
  else (printf ("No additional constrainsts"));
  return 0;
}

it keeps giving me this error

./oddecho.c: In function 'main':
./oddecho.c:6:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
    6 |   scanf ("%d",&j);
      |   ^~~~~~~~~~~~~~~
./oddecho.c:12:7: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   12 |       scanf ("%s",name[i]);
      |       ^~~~~~~~~~~~~~~~~~~~

I tried using (void) before scanf in line 6 but it still keeps on bugging. Can someone help why this error keeps on popping up? Any help would be appreciated

CodePudding user response:

In general you need to check the return value of scanf to be sure that the input was successful.

And the warnings say about that.

To avoid the warnings you can cast an expression with scanf to the type void

( void )scanf ("%d",&j);

Pay attention to that due to increasing the variable i two times in this for loop

for (i=0;i<j;i  ){
  if ((i%2)==0){
    i  ;
  }
  if ((i%2)!=0){
    printf ("%s\n",name[i]);
  }
}

It can output nothing.:)

Just write

for (i=0;i<j;i  ){
  if ((i%2)!=0){
    printf ("%s\n",name[i]);
    i  ;
  }
}

CodePudding user response:

i found some problem in your code.

  1. You are using non-const variable to allocate a static array. IDK how can your compiler compile it without giving an error that j and USER_NAME is not const. I suggest to use this code instead:
...
    // Manual allocate memories
    char** names = (char**)malloc(j * sizeof(char*));
    for (i = 0; i < j; i  ) {
      names[i] = (char*)malloc(USER_NAME * sizeof(char));
      scanf("%s", names[i]);
    }
...
    // Free memories
    for (i = 0; i < j; i  ) {
      free(names[i]);
    }
    free(names);
...
  1. If you are facing the error error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. caused by MSVC compiler, you can put this line before including headers #define _CRT_SECURE_NO_WARNINGS or see this reference https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?view=msvc-170 for more details.
  2. About the warning. The scanf function's signature is int scanf ( const char * format, ... ), that means it return an integer value. Using it without storing the value returned from it can cause that warning.
  • Related