Home > front end >  result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare
result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare

Time:12-20

please help me i am struggling with this it keeps giving me errors on errors btw LABEL 2 leads to the main part

#include <stdio.h>

void main()
{
    int a,b,c;
    char d[10],e[10];

    Label:

    printf("Username of the agent: ");

    scanf("%s",d);

    printf("Password: ");

    scanf("%s",e);


    if(d=="Vayush" && e=="Vasireddy")
        { goto Label2;}
    else
        printf("wrong username and password\n");
    goto Label;
}

CodePudding user response:

if(d=="Vayush" && e=="Vasireddy")

is comparing pointers to string literals, thats wrong because you want to compare contents and not addresses. The compiler is telling you to use the standard function strncmp instead of ==, in this case strcmp can do the job:

#include <stdio.h>
#include <string.h> // strcmp

int main(void) // Use a valid signature
{
    // Use meaningful names for your vars
    char usr[10];
    char pwd[10];

    // Do not use goto, loop until you get valid values
    while (1)
    {
        printf("Username of the agent: ");
        // scanf return the number of input items successfully matched or EOF
        int n = scanf("%9s", usr); // %9s in order to avoid buffer overflows
        if (n == EOF) { break; }
        if (n == 1)
        {
            printf("Password: ");
            n = scanf("%9s", pwd);
            if (n == EOF) { break; }
            if ((n == 1) &&
                (strcmp(usr, "Vayush") == 0) &&
                (strcmp(pwd, "Vasireddy") == 0))
            {
                break;
            }
        }
        printf("wrong username and password\n");
    }
}
  •  Tags:  
  • c
  • Related