Skip to content

Instantly share code, notes, and snippets.

@zneo
Created December 18, 2014 07:41
Show Gist options
  • Select an option

  • Save zneo/2d46b551c1ddcab9ae8d to your computer and use it in GitHub Desktop.

Select an option

Save zneo/2d46b551c1ddcab9ae8d to your computer and use it in GitHub Desktop.
cpp
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
void main()
{
for (int i =1;i<5; ++i)
{
static int a = i;
cout << a <<endl;
}
}
@prabeenkumar1
Copy link
Copy Markdown

include <iostream.h>

include <string.h>

include <stdlib.h>

void main()
{
for (int i =1;i<5; ++i)
{
static int a = i;
std::cout << a <<std::endl;
}
}
it always print 0 fore times
And

include <iostream.h>

include <string.h>

include <stdlib.h>

void main()
{
for (int i =1;i<5; ++i)
{
static int a = i;
std::cout << a <<std::endl;
a++;
}
}
it print:- 1 2 3 4 in all iteration

@ruch1
Copy link
Copy Markdown

ruch1 commented Mar 9, 2016

include

include <string.h>

include <stdlib.h>

using namespace std;
int main()
{
for (int i =1;i<5; ++i)
{
static int a=i;
cout << a <<endl;

}
return 0;
}

it always prints the no assigned to it during declaration ie 1;
so u have increment it like

include

include <string.h>

include <stdlib.h>

using namespace std;
int main()
{
for (int i =1;i<5; ++i)
{
static int a=i;
cout << a <<endl;
a++;

}
return 0;
}
output:1
2
3
4

for this output is

include

include <string.h>

include <stdlib.h>

using namespace std;
int main()
{
for (int i =1;i<5; ++i)
{
static int a;
cout << a <<endl;
a++;

}
return 0;
}
output : 0
1
2
3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment