How Does This Simple C Program Work? - TechRepublic
Question
April 25, 2011 at 11:39 AM
sanks4545

How Does This Simple C Program Work?

by sanks4545 . Updated 15 years, 2 months ago

I know this may not be the right website to post coding questions but hey, I’m signed up at 2000000 different websites, so I’m avoiding signing up somewhere else!

Anyway, how does this program calculate the given table of 2^n power?

#include
#define N 20

int main(void) {
int n;
int val = 1;

printf(“\t n \t 2^n\n”);
printf(“\t================\n”);

for(n=0; n<=N; n++) { printf("\t %d \t %d\n", n, val); val = 2*val; } return 0; } What I don't understand is how val*2, which I understand to mean val times 2, computes 2^n power. Why does it do that and not 2*val? Thanks!

This discussion is locked

All Comments