0

I want to check if a number given by a user is an integer or not in another way i want to verify if the input data is between −(2)^31= −2,147,483,648 and ((2)^31) - 1 =2,147,483,647 this is my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int x;

    int y = pow(3,31) * (-1);
    int z = pow(3,32) - 1;
    printf("\n\ty = %d et z = %d \n\n", y, z);
    scanf("%d", &x);
    if ((x < y) || (x > z)) {
        printf("x is not an integer");
    }
    else {
        printf("x is an integer");
    }
    return 0;
}

But while running the program the result always showing me x is integer even if x is greater than 2,147,483,647 or lesser than −2,147,483,648.

21
  • 3
    The value of a variable x of type int can't possibly fall outside the range representable in int, by definition. Commented Jul 18, 2020 at 19:48
  • I don't understand, you are asking about using the C language but you have a lot of C++ language tags. They are distinct languages. For example, C++ has cin, cout, inheritance and overloading of functions. The C language doesn't. Commented Jul 18, 2020 at 19:53
  • BTW, the pow function in C++ returns a floating point value. You may get inaccuracies do to conversion from floating point to integer. Best to use an integer exponentiation function. Commented Jul 18, 2020 at 19:54
  • 4
    strtol()is a good starting point. Commented Jul 18, 2020 at 19:54
  • BTW, numbers can be valid and outside of your range checking. You'll need a 64-bit or larger integer to hold your range. The value pow(3, 31) is larger than (pow(2, 32) - 1). Commented Jul 18, 2020 at 19:57

2 Answers 2

1

Testing whether input is a valid int decimal numeral or is a decimal numeral in [-231, 231) is actually a bit complicated. The C standard does not provide a direct way to do this. What we can do is:

  • Read characters and check to see whether they are in the expected form: spaces, an optional minus sign (hyphen), and digits. (Any non-digits after the digits will be allowed and ignored.)
  • Try using strtol to convert the numeral to a long. We use strtol because there is no C-standard library routine for converting to an int (or your fixed bounds using 231) that provides error indications.
  • Compare the long produced by strtol to the int bounds.

Example code for int bounds follows. If you want bounds of -2147483648 and 2147483647 instead, substitute those for INT_MIN and INT_MAX. To be completely safe, the code should actually use long long and strtoll, since the C standard does not require long to be able to represent −2147483648.

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    //  Prepare a buffer.
    size_t BufferSize = 100, BufferUsed = 0;
    char *Buffer = malloc(BufferSize * sizeof *Buffer);

    //  Skip white space.
    int c;
    do
        c = getchar();
    while (isspace(c));

    if (c == EOF)
    {
        printf("Input is not an int:  EOF before \"-\" or digit seen.\n");
        exit(EXIT_SUCCESS);
    }

    //  Accept a hyphen as a minus sign.
    if (c == '-')
    {
        Buffer[BufferUsed++] = c;
        c = getchar();
    }

    //  Accept digits.
    while (isdigit(c))
    {
        Buffer[BufferUsed++] = c;
        if (BufferSize <= BufferUsed)
        {
            BufferSize *= 2;
printf("Realloc:  size = %zu, used = %zu.\n", BufferSize, BufferUsed);
            char *NewBuffer = realloc(Buffer, BufferSize * sizeof *NewBuffer);
            if (!NewBuffer)
            {
                fprintf(stderr, "Error, unable to allocate %zu bytes.\n",
                    BufferSize);
                exit(EXIT_FAILURE);
            }
            Buffer = NewBuffer;
        }
        c = getchar();
    }

    //  Ensure we saw at least one digit (input is not blank or just a hyphen).
    if (BufferUsed == 0 || BufferUsed == 1 && Buffer[0] == '-')
    {
        printf("Input is not an int:  No digits present.\n");
        exit(EXIT_SUCCESS);
    }

    //  Put back the unaccepted character, if any.
    if (c != EOF)
        ungetc(c, stdin);

    //  Terminate the string.
    Buffer[BufferUsed] = 0;

    //  Attempt to convert the numeral to long.
    char *End;
    errno = 0;
    long x = strtol(Buffer, &End, 10);

    //  Test whether strtol succeeded.
    if (*End)
    {
        /*  I do not expect this to occur since we already tested the input
            characters.
        */
        printf("Input is not an int:  strtol rejected %c.\n", *End);
        exit(EXIT_SUCCESS);
    }
    if (errno == ERANGE)
    {
        printf("Input is not an int:  strtol reported out of range.\n");
        exit(EXIT_SUCCESS);
    }

    if (x < INT_MIN || INT_MAX < x)
    {
        printf("Input is not an int:  Value is outside bounds.\n");
        exit(EXIT_SUCCESS);
    }

    printf("Input is an int, %ld.\n", x);

    free(Buffer);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe i think i should store the number on a char array and check if it contains the float character '.'

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main(){
char number[10];
int flag=0,i = 0;
printf("\n\nEnter a number: ");
scanf("%s", number);
while(number[i++] != '\0'){
if(number[i] == '.'){
     flag = 1;
     break;}}
if(flag)
printf("\n\n\n\tyou Entered a Floating Number not an integer number\n\n");
else
printf("\n\n\n\t you Entered an integer Number\n\n");
return 0;}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.