Like Tree4Likes

The consistence of an int input

This is a discussion on The consistence of an int input within the C Programming forums, part of the General Programming Boards category; Good morning. Can I code a do-while like this? Code: #include <stdio.h> int main(void) { int num; do { printf("Enter ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    No, you cannot. sizeof() is a compile-time operator.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    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,531
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, 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
    383
    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).
    thames likes this.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > 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.
    thames likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    thames likes this.

  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,531
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > 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.
    thames likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2011, 08:02 PM
  2. Replies: 1
    Last Post: 09-24-2009, 02: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21