0

I'm a noob at C/C++ So excuse the simplicity of the question, but here goes

unsigned char i;
for (i=0; i<1000; ++i)
  if ((i%4) == 0)
    printf("hello\n");

how many times will the code print "hello". I say 63, but alas its not one of the options. Can someone provide an answer, but more importantly an explanation as to why

iammilind
  • 68,093
  • 33
  • 169
  • 336
user595985
  • 1,543
  • 4
  • 29
  • 55

6 Answers6

7

Note: I am assuming 8 bit char types.

You will overflow when you perform ++i for i equal to 255. At that point the language standard decrees that i becomes 0, a phenomenon commonly known as wraparound.

So, you have an infinite loop, since i<1000 for all values of i.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • -1 to compensate bad upvote on a bad answer ;-) Overflowing an `unsigned char` is not undefined behavior, but the answer is otherwise correct. I think you would get UB if `UCHAR_MAX` is equal to `INT_MAX`, but that would be some weird implementation. – Steve Jessop Sep 20 '11 at 11:20
  • HA! You need to downvote about 5 times to compensate my upvote :P – Luchian Grigore Sep 20 '11 at 11:22
  • @Steve Sorry. What is the standards definition of the behaviour? I might as well learn something. – David Heffernan Sep 20 '11 at 11:22
  • @David: in both C++ and C, converting an out-of-range value to an unsigned type results in the value modulo UWHATEVER_MAX+1. So in this case, modulo 256 if `unsigned char` is 8 bits. The slight possibility for UB, then, is if `i+1` were promoted to `int` and overflowed that before it even gets a chance to be modulused back into range of `unsigned char`. – Steve Jessop Sep 20 '11 at 11:25
  • However, the standard says that: "If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined, unless such an expression is a constant expression (5.19), in which case the program is ill-formed." So it might be undefined behavior... – Luchian Grigore Sep 20 '11 at 11:30
  • @Steve Thanks. What caught me out is the difference between signed and unsigned types. Do you happen to know why overflow is UB for signed types? What's special about them? – David Heffernan Sep 20 '11 at 11:31
  • 1
    @David: I assume without proof that it's a hardware consideration from the dawn of time -- either some machine out there which traps on out-of-bounds signed arithmetic but not unsigned, or else to do with sign-magnitude representation (where an overflowing signed value of `INT_MAX+1` can't be represented modulo `2*(INT_MAX+1)`). – Steve Jessop Sep 20 '11 at 11:43
  • @Luchian: it does say that, but the result of adding, say, `UINT_MAX` and `1` is defined. In C99, that's 6.2.5/9, which is nice and simple. There's an added confusion here because `unsigned char` is (on any sane implementation) going to be promoted to `int`. So it's possible for apparently-unsigned arithmetic to overflow for that reason. – Steve Jessop Sep 20 '11 at 11:54
  • Thanks I qquickly wrote it up as suggested below and it is an infinite loop. I originally thought that when i was pushed beyond its range 255, that the code would halt/crash. I didn't realize it would wrap around back to zero. Many thanks for your speedy respoinses – user595985 Sep 20 '11 at 12:08
4

I would urge you to conduct an experiment by running the code. If that doesn't clear things up, try printing out the values of i for which the condition is true. If you then notice any anomalies in how the value of i changes, think about possible causes of that.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

Answer is Infinite time, range of unsigned character is between 0-255(1 byte) when it goes beyond 255 it will overflow and come back to 0 that mean it will never reach to 1000 ..hence infinite loop

Aman Agarwal
  • 727
  • 6
  • 15
1

Here the you decleared "i" as unsigned char whose range in less than 1000 and its of size 1 byte (0-255) when it reaches the 255 again it decrements then at any condition the value will not exceeds 1000 bcoz "i" ranges from 0-255 only.

so the for loop doesn't fail n executes indefinatly

I Hope u got my point......!!!!!!!!!!

Gouse Shaik
  • 340
  • 1
  • 2
  • 15
0

I say 0 - it is not "hello", but "hellow" :-)

But now in real: i has the values 0, 1, 2, ... 999. These are 1000 values.

When will the string be printed? If i is 0, 4, 8, 12, ... - so once every 4 loop cycles.

--> In 1000 loop cycles, it gets printed 250 times.

This would be true without unsigned char as data type.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

Infinite time "hello" print because reason is simple, unsigned char have a limit up to 255 after that if u again increment on it ,they will become zero & again they reach to 255 then zero , so variable i never reach 1000 we call it infinite loop.

ABH
  • 1
  • 1