Compare int with leading zero int

86 views Asked by At
#include <stdio.h>

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    
    if (a == b)
        printf("equal");
}

so I input the number with 001 and 1, but they are equal. I want that they be considered unequal. Is there any possibility to do that and how?

3

There are 3 answers

0
Jan Schultke On

scanf("%d") discards all leading zeros, so you won't be able to compare the ints properly. In general, an int is just a number and doesn't store anything like leading zeros. To keep leading zeros, you need to read and compare strings with strcmp:

char a[21], b[21];
scanf("%20s %20s", a, b);

int c = strcmp(a, b);
if (c < 0)  /* a is lexicographically lower */;
if (c == 0) /* a is equal to b */;
if (c > 0)  /* a is lexicographically greater */;
0
chux - Reinstate Monica On

If wanting to avoid strings, code could compare the length of input by using "%n" to record the scan offset.

Tip: First check the return value of scanf().

#include <stdio.h>

int main(void) {
  int a, b;
  int n1, n2, n3, n4;
  int count = scanf(" %n%d%n %n%d%n", 
    &n1, &a, &n2,
    &n3, &b, &n4);
  if (count != 2) {  // %n do not contribute to the return count.
    printf("Invalid input.\n");
  } else {
    if (a != b) {
      printf("Unequal values.\n");
    } else {
      printf("Equal values.\n");
      if (n2 - n1 == n4 - n3) {
        printf(".. and input the same length.\n");
      } else {
        printf(".. and input lengths differ.\n");
      }
    }
  }
}

This will still see "01" and "+1" both with the same value and length, but is a step towards OP's goal as it does distinguish "01" and "1".


For a string input, ditch scanf() and use fgets().

With any scanning into a string, make certain there is no string overflow. Use ample buffers.

#define BUF_N 100

int main(void) {
  int a, b;
  int n1, n2, n3, n4;
  char buff[BUF_N];
  char num1[BUF_N];
  char num2[BUF_N];
  if (fgets(buff, sizeof buff, stdin)) {
    int count = sscanf(buff, " %s %s", num1, num2);
    if (count == 2) {
      if (strcmp(num1, num2) == 0) {
        printf("inputs are the same.\n");
      }
    }   
  }
}
    


0
chqrlie On

0 and 001 represent the same number, and for scanf, so does 1 and +1.

If you want to distinguish the different representations, you must keep the characters entered by the user and compare them when the numbers compare equal.

You can achieve this by simply reading strings instead of integers.

Alternately, you can read the input as a string and parse it with sscanf(), which is always a good idea, and keep both the values and the original representations.

Here is an example:

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

int main(void) {
    char input[80];
    if (fgets(input, sizeof input, stdin)) {
        int a, b, a0, a1, b0, b1;
        if (sscanf(input, " %n%d%n %n%d%n", &a0, &a, &a1, &b0, &b, &b1) == 2) {
            if (a == b) {
                if (a1 - a0 == b1 - b0
                &&  !memcmp(input + a0, input + b0, a1 - a0)) {
                    printf("equal\n");
                } else {
                    printf("equal (but different representations)\n");
                }
            } else {
                printf("unequal\n");
            }
        } else {
            printf("line does not contain 2 integers\n");
        }
    } else {
        printf("no input\n");
    }
    return 0;
}

Explanation:

  • a space in the format string causes sscanf to consume any whitespace pending in the input.
  • %n stores the number of characters consumed so far into an int variable whose address is passed as the next argument.
  • %d attempts to convert the input as an int from its base 10 representation and store the resulting value into an int variable whose address is passed as the next argument.
  • scanf() returns the number of successful conversions (%n directives are not conversions).
  • if 2 conversions were performed successfully, a0 is the index of the first non white character of the representation of the value stored into a, a1 the offset of the character past the end of that, and b0 and b1 the same for b.
  • also be aware that 010 and 10 represent different numbers in C source code (010 is the octal representation of 8), but will both be converted to 10 by scanf("%d", &a).
  • 010 and 10 would convert to 8 and 10 respectively by scanf("%i", &a): i stands for integer whereas d stands for decimal.
  • converting strings with "%s%s" is not equivalent because an input of +1+1 would be successfully parsed as a=1 and b=1 with identical representations whereas it would be considered a single string under "%s%s" and cause sscanf() to return 1.
  • if you wish to accept only positive integers and reject signs, you could parse the line with sscanf(input, " %[0-9] %[0-9]", ...) and then convert and/or compare the strings with atoi.