I am trying to print:

0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
17 131072
18 262144
19 524288
20 1048576
21 2097152
22 4194304
23 8388608
24 16777216
25 33554432
26 67108864
27 134217728
28 268435456
29 536870912
30 1073741824
31 -2147483648
32 0

Thsi is my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int powerof2(int n);
int main(void)
{
  for (int n=0; n<32 ;n++)
    {
      printf("n 2^n\n");  
      printf("%d %d\n", n, powerof2);
    }
}

int powerof2(int n)
{
  int power=1;
  for(int j=0;j<n;++j)
    {
      if (j == 0)                                             
    power = 1;                                               
      else   
    power *= 2;
    }
  return power;
}

I have to use function - int powerof2(int n).
But it gives me an error:
power1.c: In function ‘main’:
power1.c:11: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int ()(int)’
power1.c:11: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int (
)(int)’

Recommended Answers

All 6 Replies

I am not sure what you mean?

Line 11 of your program doesn't call the function, and as a result the compiler assumes you want that function's address. You need to include the argument list to call it:

printf("%d %d\n", n, powerof2(n));

You have 2 errors here:

:24:2: error: unknown type name 'i'
]i
^

:24:3: error: expected identifier or '('
]i
^

commented: No code in this thread would cause these error messages. +0

Ok I solved the errors. But now how to do it with left shift command instead of multiplication?

But now how to do it with left shift command instead of multiplication?

Ask yourself the following: what is the result of 1 << 0, 1 << 1, 1 << 2, 1 << 3 etc
Do you notice the pattern?

I got it nullptr. Thanks'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.