can anyone tell me I am a beginner How its working?

Viewed 61

C How it's working its printing 1-10, In the other case when I am putting only "a" in the printf function its printing 0-9?

It's giving 1-10 output:

#include <stdio.h>

int main()
{
  for (int a = 0; a < 10; a++)
  {
    printf("The value of a is %d \n",a+1);
  }

  return 0;
}

It's giving 0-9 output:

#include <stdio.h>

int main()
{
  for (int a = 0; a < 10; a++)
  {
    printf("The value of a is %d \n",a);
  }

  return 0;
}
1 Answers

Corrected code...

It's giving 1-10 output-

#include <stdio.h>
int main()
{
  for (int a = 0; a < 10; a++)
  {
    printf("The value of a + 1 is %d \n",a+1); // <== Correction here
  }
    return 0;
}

You can also change the loop boundaries:

#include <stdio.h>
int main()
{
    for (int a = 1; a <= 10; a++) // <== notice
    {
        printf("The value of a is %d \n",a);
    }
    return 0;
}

There is nothing wrong with your "two space" indent, but that practice will make more complex code more difficult to follow (and unusual). The 'norm' seems to be using "four spaces" of indent to highlight code blocks. (Just a recommendation.)

Related