Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Explain, please, why "i = 50"?

unsigned char i = 0, j(70), k = 10;
for (i = 8; i < 48; i += 7) {
    j += k++;
}
printf("i = %d; j = %03d; k = %03d", i, j, k);
return 24;

Explain, please, why "i = 50"?

I thought it would i = 0;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Why would it be zero? You do start by assigning zero to it, but then you promptly assign 8 to it. Each pass of the loop adds 7 to it until it reaches 50 at the end of the 6th pass.

You might understand things better if you placed a copy of the printf statement at the start of the loop body.


Note that it would be zero if you had

unsigned char i = 0, j = 70, k = 10;
for (unsigned char i = 8; i < 48; i += 7) {
    j += k++;
}

printf("i = %d; j = %03d; k = %03d", i, j, k);

As this creates a second variable named i that is scoped to the for loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading