1

I made the following program

#include <stdio.h>

int main()
{
    // Testing Number

    unsigned int num;

    printf("Enter The Number : ");
    scanf("%u" , &num);                  // If I Enter 4294967298
    printf("Your Number is : %u" , num); // Output Comes Out To Be 2
}

Now in the above program if enter 4294967298 Output Comes Out To Be 2, and here on stackoverflow, answers are saying that if you enter number more than the required range then the scanf will store the UINT_MAX and will print that, inspired by that answer, and discussion going on it being wrong and such. I tried on my own and it is not behaving like this instead after range it is printing after doing modulo arithmetic.

If one could point to what the standard says about it, it would be very helpful.

I have read the other answer, and both answers on that question are conflicting and no one is giving the correct one, selected answer differ on the topic.

Is it implementation dependent?

And many People seem to differ in this question, if you could support your argument with reference to standard, it would be highly appreciated.

If someone find anything wrong with this question, please do tell me in the comments.

Suraj Jain
  • 4,463
  • 28
  • 39
CuriosGuy
  • 181
  • 1
  • 9
  • https://groups.google.com/forum/#!topic/comp.std.c/7mSxlJir4Eo for what happens; https://stackoverflow.com/questions/1694266/validate-max-integer-in-scanf for what to do instead. – Ry- Feb 10 '17 at 15:09
  • @Ryan I am not asking about validating , i just want to know what happens – CuriosGuy Feb 10 '17 at 15:10
  • @CuriosGuy: Okay, so read the first link. – Ry- Feb 10 '17 at 15:10
  • @Ryan Note both [link](https://groups.google.com/forum/#!topic/comp.std.c/7mSxlJir4Eo) and [link](https://stackoverflow.com/questions/1694266/validate-max-integer-in-scanf) are about `"%d"`. This is about `"%u"` and unsigned conversions have different limitations than `int`. – chux - Reinstate Monica Feb 10 '17 at 15:18
  • @chux Can You Answer It ? Also Look Here http://stackoverflow.com/a/32648957/5473170 – CuriosGuy Feb 10 '17 at 15:22
  • IMO 1) 7.21.6.2 pushes toward UB yet overflow with `unsigned` is well defined. 2) Robust code uses `strto*()` for text to integer conversion - _that_ is well defined rather than `scanf()`, so the the question is moot there. – chux - Reinstate Monica Feb 10 '17 at 15:27
  • @chux Sir , can you see this http://stackoverflow.com/questions/42164550/what-is-happening-here-in-pow-function/42165119 – CuriosGuy Feb 10 '17 at 17:19
  • @Ryan Is there wrong with this question it is getting so much hate? – CuriosGuy Feb 11 '17 at 03:41
  • @Ryan How can answer be here http://stackoverflow.com/questions/16056758/c-c-unsigned-integer-overflow , i am not talking about assignment. That would be misleading , now it can be seen by everyone , assignment is totally different thing. – CuriosGuy Feb 11 '17 at 04:06
  • @CuriosGuy: I think it's undefined behaviour, as per 7.21.6.2 (which has been quoted several times in this discussion). With undefined behaviour, anything goes: the library could store just the low-order 32 bits (as in your experience), or it could store UINT_MAX, or it could leave the value of the variable unmodified. This is just one of the many problems with `scanf`; unlike many people here, I have no problem using scanf for quick coding exercises, but production code should use `strtoul` and friends, which have much clearer semantics. – rici Feb 11 '17 at 05:01
  • @rici Sir , i am mainly confused with answer on this http://stackoverflow.com/questions/32648358/scanning-a-number-having-more-data-than-predefind-value-in-c – CuriosGuy Feb 11 '17 at 05:03
  • @CuriosGuy: Yes, I understand how that is confusing. That answer is wrong, in my opinion. That happens. – rici Feb 11 '17 at 05:05
  • @CuriosGuy: It’s an incorrect automatic banner. Maybe I can clear it by reopening it and closing it again. – Ry- Feb 11 '17 at 09:12
  • @Ryan Thanks :) , it is a great relief – CuriosGuy Feb 11 '17 at 09:13
  • @Ryan Can you let it open for a day , it will attract good answer , then you can close it , i do not know why there is a downvote – CuriosGuy Feb 11 '17 at 09:14
  • @CuriosGuy: You don’t think the existing answers on this question and the non-accepted answer on the duplicate are good? They all agree and cite the same part of the specification. – Ry- Feb 11 '17 at 09:14
  • @Ryan As i have told in the question how the other answer did not fully address the issue – CuriosGuy Feb 11 '17 at 09:15
  • @CuriosGuy: Well, you said “both answers on that question are conflicting”, which is true – the first answer (voted at −2) is wrong. Taking into account only the second answer on the other question and the two answers on this question, what’s inadequate? – Ry- Feb 11 '17 at 09:15
  • @Ryan I think in such case , the question which is well formed is left open and other is closed , and clearly you can see that i asked more correclty and even asked for the quotation from standard further selected answer on the other is wrong , which is further misleading. – CuriosGuy Feb 11 '17 at 09:17
  • @Ryan And most improtant other question asked "why circulation is not following while scanning input via scanf(), why its following in case of char ?" which is wrong in itself because it is implementation thing , and i have not asked that thing . – CuriosGuy Feb 11 '17 at 09:19
  • So so what the person was asking is quite different what i was asking , and he was talking about his implementation . – CuriosGuy Feb 11 '17 at 09:21

2 Answers2

5

Undefined behavior will be invoked.

Quote from N1570 7.21.6.2 The fscanf function, paragraph 10:

If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Your thoughts on 2nd half on my [comment](http://stackoverflow.com/questions/42162536/what-happens-if-i-input-number-more-than-the-range-of-data-type-in-c#comment71490209_42162589) as they apply here too. – chux - Reinstate Monica Feb 10 '17 at 15:15
  • http://stackoverflow.com/a/32648957/5473170 Can you justify what this answer says? – CuriosGuy Feb 10 '17 at 15:17
2

This is undefined behavior. The C11 draft says:

[...] the result of the conversion is placed in the object pointed to by the first argument following the format argument that has not already received a conversion result. If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Sir , the person marked my question as asked here http://stackoverflow.com/questions/16056758/c-c-unsigned-integer-overflow , and it is not true as i am talking about input – CuriosGuy Feb 10 '17 at 15:10
  • Hmmm is the result of the conversion of text `"4294967298"` to `2U` not well defined? (NMDV) Consider `"0.1"` and `"%f"`, the result cannot be _exactly_ represented. so there must be some squishy-ness in the meaning of "conversion cannot be represented". – chux - Reinstate Monica Feb 10 '17 at 15:10
  • @CuriosGuy: This answer *is* about input. – Ry- Feb 10 '17 at 15:10
  • @Ryan Sir , i was saying that the person who commented on my question is suggesting it to be duplicate. – CuriosGuy Feb 10 '17 at 15:13
  • @CuriosGuy I don't think this is a dupe (or I would have closed it). – unwind Feb 10 '17 at 15:14
  • a person said : It should be noted that on platforms with 64-bit unsigned long and 32-bit unsigned int, a value that is valid in the range of unsigned long will not be converted to e.g. UINT_MAX, instead it will be converted using modulo arithmetic as detailed here. Lets take the value like 4294967299. It is to big to fit in a 32-bit unsigned int, but fits very well in a 64-bit unsigned long. Therefore the call to strtoul will not return ULONG_MAX, but the value 4294967299. Using the standard conversion rules (linked to above), this will result in an unsigned int value of 3. – CuriosGuy Feb 10 '17 at 15:15
  • @CuriosGuy But that's completely irrelevant to the question, which clearly uses plain `%u` and thus `unsigned int`, not `unsigned long`. – unwind Feb 10 '17 at 15:22
  • Please See Here http://stackoverflow.com/a/32648957/5473170 – CuriosGuy Feb 10 '17 at 15:22
  • That answer just says something else , and i am confused. – CuriosGuy Feb 10 '17 at 15:26
  • @CuriosGuy That's the first accepted answer I see with a negative score, I believe. That should tell you to perhaps not blindly trust what it says. I just flagged it. – unwind Feb 10 '17 at 15:35
  • @unwind It got downvoted today , earlier it had one upvote , and it is by highly reputed person in this case a person doubt himself. – CuriosGuy Feb 10 '17 at 15:36
  • @chux I think that the important distinction is the *exactly* represented. *0.1* can be represented as a float, sorta. Too large integers cannot be *represented* at all, even in unsigned. The [modulo reduction](http://port70.net/~nsz/c/c11/n1570.html#6.2.5p9) deals with computations involving unsigned operands and their results, but scanf does string conversion. I might be reading it wrong, but actual (mis)behaviour of scanf seems to imply UB. A version of glibc for example does seem to use strtoul internally, so you can get both wrapping and gapping, if ulong > uint... – Ilja Everilä Feb 10 '17 at 21:16
  • @IljaEverilä The other question did not have good answer , therefore i asked again , i said in question look at this answer and it is not very clear, why is the question getting so much hate , it is genuine to ask what happen when you pass more than limit to scanf? – CuriosGuy Feb 11 '17 at 03:43
  • @IljaEverilä How can it be duplicate of this http://stackoverflow.com/questions/16056758/c-c-unsigned-integer-overflow , it clearly is not , i am talking abut scanf , and the other question is talking about assignment , please remove it is duplicate at best. – CuriosGuy Feb 11 '17 at 03:46