title: While loop in C giving Segmentation Fault when not using print statement
body: <p>I came across a strange behaviour of C with while loop today.</p>
  <p>A very simple code :</p>
  <pre><code>int userLogin()
  {
      struct User user;
         
      while (1)
      {
          printf(&quot;\n\nEnter username (or quit to return back) - &quot;);
          char userName[50];
          scanf(&quot;%s&quot;, userName);
  
          if (strcmp(userName, &quot;quit&quot;) == 0)
          {
              printf(&quot;\nReturning to Login menu...\n&quot;);
              return 2;
          }
  
          printf(&quot;\n\nEnter Password - &quot;);
          char password[50];
          scanf(&quot;%s&quot;, password);
  
          user = login_user(userName, password);
          if (strcmp(user.userID, userName) != 0 ||
              strcmp(user.password, password) != 0)
          {
              printf(&quot;\nInvalid username or password !\n&quot;);
          }
          else
          {
              printf(&quot;\nLogin successful...Entering Library menu !\n&quot;);
              currentUser = user;
  
              if (currentUser.role == 1)
              {
                  libraryOptions();
              }
              else
              {
                  adminPanel();
              }
  
              break;
          }
      }
  }
  </code></pre>
  <p>Now this gives out Segmentation fault (Core Dumped).</p>
  <p><a href="https://i.stack.imgur.com/RUYr8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RUYr8.png" alt="enter image description here" /></a></p>
  <p>However, same code, I just insert a print statement before while(1) :</p>
  <p><a href="https://i.stack.imgur.com/7TmnI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7TmnI.png" alt="enter image description here" /></a></p>
  <p>And suddenly it starts working...</p>
  <p><a href="https://i.stack.imgur.com/HuXoH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HuXoH.png" alt="enter image description here" /></a></p>
  <p>Can somebody help me understand the reason for this ? What am I missing here ?</p>
  <p>PS: I know what segmentation fault is, what I need to understand is what changes if I include a print statement.</p>
  <p>PPS:
  struct User is in a header file</p>
  <pre><code>struct User {
          char *userName; //User name
          char *userEmail; //Usre email
          char *userID; // user id
          char *password; //password
          int role; //1 for regular user and 2 for admin
  };
  </code></pre>
  <p>login_user is also in another C file which includes the user header file:</p>
  <pre><code> struct User login_user(const char *userName, const char *password)
  {
     
  
   int i;
      struct User user;
      user.userName = &quot;&quot;;
      user.password=&quot;&quot;;
  
      if (allUsers.length == 0)
      {
          return user;
      }
  
      for (i = 0; i &lt; allUsers.length; i++)
      {
          if (strcmp(userArray[i].userID, userName) == 0 &amp;&amp;
              strcmp(userArray[i].password, password) == 0)
          {
              user.userName = userArray[i].userName;
              user.password = userArray[i].password;
              user.userEmail = userArray[i].userEmail;
              user.userID = userArray[i].userID;
              user.role = userArray[i].role;
              return user;
              
          }
  
        
      }
      return user;
      
  }
  </code></pre>
  <p>I still reiterate that I don't want the answer to main code or reason to segmentation fault (I am working on it), all I request is someone to explain the reason why the print statement makes the code work.</p>