You are not logged in. Please login at www.codechef.com to post your questions!

×

Looping and control structure

#include<stdio.h>
int main()
{
    int i=3;
    while(i--)
    {
        int i=100;
        i--;
        print("%d",i);
    }
  return 0;
 }

My output was coming infinite as 99 will repeat again and again but the answer was just 3 99s i.e. 99 99 99. How?!

asked 14 Aug '18, 13:29

raghav_00's gravatar image

0★raghav_00
1
accept rate: 0%


It is because variable i declared inside and outside of while loop are different variables with different scopes.

Variable i declared outside of the the loop is used as counter variable for the loop(it keeps on decrementing until it becomes 0), while one declared inside the loop is used for printing(it is redeclared and reassigned to 100 everytime loop runs).

link

answered 14 Aug '18, 15:21

tanweer919's gravatar image

3★tanweer919
11
accept rate: 0%

edited 14 Aug '18, 15:23

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,491
×966
×191
×139
×135

question asked: 14 Aug '18, 13:29

question was seen: 158 times

last updated: 14 Aug '18, 13:29