0

Whenever i put the correct int number instead of printing Correct its Print Invalid.

int main(void)
{
  int number = 042646;
  int pass;
  printf("Enter the PIN.\n");
  scanf("%d", &pass);/*enter code here*/
   if (pass == number)
    {
      printf("Correct\n");
    }
  else
    {
      printf("Invalid\n");
    }
    }
Haris Khan
  • 13
  • 1
  • 5
  • 3
    `042646` is treated as octal value. Remove the zero at the start. Or use `"%o"` as format specifier to read octal. – Barmak Shemirani Nov 04 '18 at 04:10
  • Possible duplicate of [What is special about numbers starting with zero?](https://stackoverflow.com/questions/26568200/what-is-special-about-numbers-starting-with-zero) – phuclv Nov 04 '18 at 09:10
  • [printf with "%d" of numbers starting with 0 (ex "0102") giving unexpected answer (ex '"66")](https://stackoverflow.com/q/19652583/995714), [How does C Handle Integer Literals with Leading Zeros, and What About atoi?](https://stackoverflow.com/q/1661369/995714), [Why does string to int conversion not print the number 0 if its at first index](https://stackoverflow.com/q/49851981/995714) – phuclv Nov 04 '18 at 09:10

1 Answers1

0

In "C" a number preceded by 0 is interpreted as an octal number. Here is a simple code which will help you to see the issue:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
  int number = 42646;
  int number_octal = 042646;
  int pass = 0;

  printf("Enter the PIN.\n");
  scanf("%d", &pass);/*enter code here*/

  /* Debug */
  printf("Pass: %d\n", pass);
  printf("Number: %d\n", number);
  printf("number_octal: %d\n", number_octal);

  if (pass == number)
  {
    printf("Correct\n");
  }
  else
  {
    printf("Invalid\n");
  }

  return 0;
}
Iliya Iliev
  • 151
  • 4