1
#include<stdio.h>
int main(){
    char user = "welcome";
    printf("Enter UserName \n");
    scanf("%s",&user);

    char pass = "hi";
    printf("Enter Password: \n");
    scanf("%s",&pass);

    if(user=="welcome" && pass == "hi"){
            printf("Login Successful");
    }
    else{
        printf("Retry");
    }
    return 0;
}

This program asks to enter username and password then checks if entered correctly and prints suitable statement.

However, for me it only shows else part even if the data entered is correct.

enter image description here

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
user123
  • 11
  • 1
  • This declaration char user = "welcome"; is incorrect. At least you need to write char *user = "welcome"; Or taking into account the following code you need to declare a character array. – Vlad from Moscow Jan 31 '23 at 18:58
  • When that is corrected the `scanf("%s",&user);` will fail for two reasons: a) you cannot modify a string literal, b) `&user` is not the address of the string, but of the pointer. – Weather Vane Jan 31 '23 at 19:06
  • That code should not (does not) compile without warnings — because you're trying to assign the pointers to the strings to a single `char`. Treat compiler warnings as bug reports in your code — the compiler does not deign to report a problem unless it is serious. If you use GCC (or Clang), consider using `-Wall -Wextra -Werror`, which will fail the compilation if the compiler finds anything to warn about. You can add more, fussier options if you like (`-Wmissing-prototypes -Wstrict-prototypes -Wold-style-declaration -Wold-style-definition`, for example). – Jonathan Leffler Jan 31 '23 at 19:15
  • Another part of the problem: [How do I compare strings properly in C?](https://stackoverflow.com/q/8004237/15168) – Jonathan Leffler Jan 31 '23 at 19:17

2 Answers2

3

The program is entirely wrong.

In these declarations

char user = "welcome";

and

char pass = "hi";

you declared objects of the type char that you are trying to initialize by expressions of the type char * (the string literals used as initializers are implicitly converted to pointers to their first elements).

Instead you need to declare character arrays where you will read the user name and his password.

For example

char user[10];
char pass[10];

The calls of scanf can look the following way

scanf("%9s", user);
scanf("%9s", pass);

In this if statement

if(user=="welcome" && pass == "hi"){

you are comparing pointers. Instead you need to compare strings like

if( strcmp( user, "welcome" ) == 0 && strcmp( pass, "hi" ) == 0 ){

To use the function strcmp you need to include header <string.h>

#include <string.h>
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Declare the username and password fields something like this:

char user[1024], pass[1024];

Currently they can only hold a single character. You can change the 1024 to the maximum number of characters that you'd assume the user would input. You should also keep in mind strings cannot be compared with user=="welcome"! Use the strcmp function inside string.h instead.

Oreoezi
  • 129
  • 9
  • 2
    These are declarations, and neither has been initialized. Initialization could take several forms, such as `char user[1024] = {0}, pass[1024] = {0};`, or... `char user[1024] = "welcome", pass[1024] = "hi";` – ryyker Jan 31 '23 at 19:21
  • 1
    @ryyker yeah, my bad for mixing up those terms. @Haris you made a misunderstanding. By "currently" I am referring to the current code that the OP is trying to execute (`char user = "welcome";`). – Oreoezi Jan 31 '23 at 19:27
  • @Oreoezi I take my words back. :) – Harith Jan 31 '23 at 19:30
  • 1
    You are encouraged to edit your post if necessary, to address comments as problems are noted. This will often times improve your answer. – ryyker Jan 31 '23 at 19:31