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; }
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; }
The condition will be always false, because num is an int!Code:int num; .. sizeof(num)>sizeof(int)
What are you trying to do?
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().
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.
I'm trying to verify if the size of number exceeds the size of an integer.What are you trying to do?
I entered an integer and the program worked without any problems.
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).
> 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.
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: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
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!
I couldn't even imagine that, thank you!scanf() in any form doesn't detect or signal numeric overflow.
> 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.
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 ..