Thread: The consistence of an int input

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    The consistence of an int input

    Good morning. Can I code a do-while like this?

    Code:
     
    
    #include <stdio.h>
    
    int main(void) 
    { 
      int num; 
      do { 
       printf("Enter a number: ");
       scanf("%d", &num);
      } while(sizeof(num) > sizeof(int));            
      
      printf("You entered: %d\n", num);
      return 0;    
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    int num;
    ..
    sizeof(num)>sizeof(int)
    The condition will be always false, because num is an int!

    What are you trying to do?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    No, you cannot. sizeof() is a compile-time operator.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rags_to_riches View Post
    No, you cannot. sizeof() is a compile-time operator.
    Wrong. The code will compile and execute. Although, since the loop condition is a compile time expression that is always false, the loop body will only be executed once.

    I suspect the OP does not understand what the sizeof operator actually does. It certainly does not do error checking on user input obtained using scanf().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Fine. From a perfectly pedantic point of view, certainly it can be done. My point was that it almost certainly does not do what the OP wants, which you also agree with.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rags_to_riches View Post
    Fine. From a perfectly pedantic point of view, certainly it can be done. My point was that it almost certainly does not do what the OP wants, which you also agree with.
    Sure. I also consider that the OP (like many people here) needs to learn to ask questions with more precision. That may be pedantic, but I don't do it without cause - pedantry is a skill that I learned, not a trait I was born with. People who ask imprecise questions have significantly more trouble producing good code (or working systems) than people who ask precise questions. Computers and compilers interpret code pedantically, precisely, and with complete ignorance of programmer intent because that is their nature.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    What are you trying to do?
    I'm trying to verify if the size of number exceeds the size of an integer.

    I entered an integer and the program worked without any problems.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    The problem with trying to verify that a number fits in an int after putting it in an int is the same as asking whether a gallon-sized bucket contains more than a gallon of water. If you try to put more than a gallon of water in the bucket, the excess overflows and the bucket will never hold more than its capacity. The same is true of int (and other scalar types) in C. The overflow behavior might be different, though (signed integers have implementation-defined overflow).

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm trying to verify if the size of number exceeds the size of an integer.
    Then you need to use strtol() or strtoul()
    These return an appropriate status if you try and convert say 1234567890123456789012345678901234567890.

    scanf() in any form doesn't detect or signal numeric overflow.

    Which leads to the next point, regarding all your getchar() buffer cleanup.
    If you read a whole line using fgets(), then the whole problem of "some input left behind due to failed conversion" goes away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by thames View Post
    I'm trying to verify if the size of number exceeds the size of an integer.

    I entered an integer and the program worked without any problems.
    There is a library calles limits.h .There INT_MAX and INT_MIN are defined for you.Thus you can check if the input is inside this range

  11. #11
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    There is a library calles limits.h .There INT_MAX and INT_MIN are defined for you.Thus you can check if the input is inside this range
    lately, I've been thinking how I could use INT_MAX and INT_MIN for that. I already knew this library. I don't know how but your statement about the library made me think that I just have to do something like:

    do {
    .
    .
    .
    while(n < INT_MIN || n > INT_MAX);

    thank you very much.

    @Salem

    I already knew strtoll too but I used it before to convert a string to long long. When I coded scanf in this program, I thought a way that I didn't need to use fgets because IMHO, I don't surely know, this situation in particular is pretty straightforward. But I appreciated both opinions. Many thanks people!

    scanf() in any form doesn't detect or signal numeric overflow.
    I couldn't even imagine that, thank you!

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by std10093 View Post
    There is a library calles limits.h .There INT_MAX and INT_MIN are defined for you.Thus you can check if the input is inside this range
    Can you offer a sample of code that can produce an int with value outside that range?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while(n < INT_MIN || n > INT_MAX);
    Except this doesn't work at all, since INT_MIN...INT_MAX is the maximum range.
    If the user does type in an integer which overflows, the result is modulo-n reduced to fit somewhere in the allowed numeric range for an int.

    If you could possibly store INT_MIN-1 in an int, then INT_MIN would be mis-named.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hmm i think the two gentlemen are right because of the cut off of bits in order to get the number fitted in the register.
    Sorry thames, still muuuuch to learn by me ..

  15. #15
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    no problem man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2011, 07:02 PM
  2. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  5. Help loading a vector with input from input stream
    By Bnchs in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2006, 03:53 PM

Tags for this Thread