View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12890 Members
Join this group to post and comment.

Why does this C Program print "10" five times?

#include

  int main()

  {

   int i;

  

  for(i=0;i<5;i++)

  {

   int i=10;

    printf("%d",i);

   }

return 0;

  }

why this program prints 10 ,..5 times...the value of i will be changed after a single time operation, in for loop.....?

Kaustubh Katdare
Kaustubh Katdare • Sep 21, 2018

Inside the loop, you are setting i = 10 and then printing it - 5 times. You'll have to not set i = 10 inside the loop. 

this program prints 10..5 times in this situation also


Kaustubh Katdare
Kaustubh Katdare • Sep 21, 2018

What change did you make to your code? Remove the  int i=10; part inside the for loop and run again. 


#include

  int main()

  {

   int i;

  for(i=0;i<5;i++)

  {

    printf("%d",i);

   }

return 0;

  }

This prints 01234 as expected. 

the code is same,

the output of this code is 1010101010

my question is why this...because once it enters the loop..the value of i will be changed to 10..and the loop will not iterate further because 10 is not less than 5

kumar prasun
kumar prasun • Sep 21, 2018

u have used int i=10 inside the loop....this causes the value of i equal to 10 every time it enters the loop....that is the int i inside the loop will act as another variable with name i.if u delete the int keyword the output will be 10 only.

both i act as different variables.

Kaustubh Katdare
Kaustubh Katdare • Sep 21, 2018

If the code is the same, your output won't change. Here's what you are doing - 

for(i=0;i<5;i++)

  {

   int i=10;

    printf("%d",i);

   }

1. The for loop determines how many times the loop will run. In your case, it will run from 0 to 4 (a total of 5 times).

2. Inside the for loop, you are hard-setting the value of variable 'i' to 10. Note that 'i' is set to 10 and it's scope is limited within the for loop. 

3. Then you are printing the current value of 'i' - which is 10. 

4. In essence, you are printing the hard-set value of 'i' which is 10 in each loop. 

If you remove 'i = 10' inside the loop, you will get output as 01234; which I believe is your expected output. 

The value of 'i' isn't getting change to 10. The checks aren't performed inside the loop. Rather, loop runs a level 'above' what's inside. If that makes it easier to understand. 

yes..u r right..i got it...thanks

Kaustubh Katdare
Kaustubh Katdare • Sep 21, 2018

Perhaps you should look at passing by reference and passing by value to understand this in more detail. 

PS: I no longer code in C. It's been over 1.5 decades since I wrote a working C program. ? 

but i have to study c first.. im in first year in cse branch.. and the questions in exams are basically this type....that's why.....anyways thanks...

i will be asking more question in the coming future...? 

IN this case, study about the scope of the variables that you assign.

Scope will be local. what is meant by local is if you are assigning some value inside a loop the scope is inside the loop.

The same case goes with the scope of the variable in function. If you declare a function and assign some value to a variable the scope will be limited in that function.


but the variable is ..same that is i...

.

Jeffrey Arulraj
Jeffrey Arulraj • Sep 22, 2018

but the variable is ..same that is i...


I have a problem with your code if you are trying to declare a variable with the same name twice and if the scope of the variables overlap it will throw a compilation error and not a valid output.


That will be the case in your code snippet. the variable i is declared twice and with overlapped scope and so it will throw an error

Share this content on your social channels -