-3

I have this source code:

void main() {
  int a[10]={9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; int i;
  for (i=0; i<10; i=1<<i) a[9-i]=a[i];
  for (i=1; i<10; i++) a[0]+=a[i];
  printf("%d", a[0]);
}

Since I'm new to C programming, I don't really understand what it does. What does i=1<<i mean in the for loop?

8
  • 3
    << is the bitwise left shift operator. (And void main() is wrong; it should be int main(void).) Commented May 20, 2016 at 22:45
  • i will take on the values 0, 1, 2, 4, 16. 16 > 10, so that will end the loop. Commented May 20, 2016 at 22:46
  • 2
    Yeah I thought that void main() is wrong too but what I'm looking at is the last years exam questions. I'm trying to prepare for the first step of the exam which is held over the whole country but the problem is I have been learning Python, and I'm pretty new to C. Commented May 20, 2016 at 22:48
  • They put that code on an exam?!
    – Schwern
    Commented May 20, 2016 at 22:55
  • 1
    The proper answer to "what does this C program do" is to answer: "The C standard imposes no requirements, as the prototype of main is not one of those defined in the standard." Commented May 21, 2016 at 5:24

3 Answers 3

4

1<<i is the bitwise left shift operator. If you think of 1 as 00001 it says to shift the 1 left by i times. The loop feeds the result back into itself.

  • 1<<0 is 00001 or 1.
  • 1<<1 is 00010 or 2.
  • 1<<2 is 00100 or 4.
  • 1<<4 is 10000 or 16.

It starts at 0 and stops at 4, because 16 is greater than 10.

You can see this in action by doing the same loop and printing i.

#include <stdio.h>

int main() {
    for (int i=0; i<10; i=1<<i) {
        printf("%d\n", i);
    }

    return 0;
}

Why you would do this, I'm not sure.

2
  • Except that the loop skips 8 because 1 << 4 is 16. Commented May 20, 2016 at 22:49
  • @MichaelBurr Righto, I forgot it has a feedback.
    – Schwern
    Commented May 20, 2016 at 22:50
1

The operator << is known as left-shift. It shifts bits to the left in its first argument by the number of places given in the second argument. Its actions are only defined for unsigned types. In your code, i is shifted to the left by 1 place, and the result of the shifting is placed back in i. This is the same as multiplying by powers of 2.

Also, you should write int main(). The void version is wrong and non-standard.

5
  • 1
    int main(void) is better than int main(). Commented May 20, 2016 at 22:48
  • @KeithThompson ...why?
    – Schwern
    Commented May 20, 2016 at 22:54
  • @KeithThompson how is it better?
    – Andreas DM
    Commented May 20, 2016 at 22:58
  • 3
    int main() { /* ... */ } is an old-style definition that doesn't specify the number of type(s) of the expected arguments. int main(void) { /* ... */ } uses a prototype, which explicitly says that main takes no arguments. Though it's a minor point, this can catch errors if you call main recursively (yes, that's legal). A more major point is that the C Standard says that int main(void) is valid; it doesn't say the same about int main(). I've written about this at some length here. Commented May 20, 2016 at 22:58
  • @KeithThompson I was curious to know, thanks I will look into that
    – Andreas DM
    Commented May 20, 2016 at 22:59
1

the left shift operator << has higher precedence in C than =.

So first the value 1 (0x00000001) is shifted left the number of bits (per the count in i.

The result is then assigned back into i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.