some observations need explanation

hitesh_best's Avatar, Join Date: Aug 2007
Newbie Member
Hello friends,
I am a newbie,
Just did some simple experiments but found some weird observations.

Lets see this code
Code:
main()
{
  int i;
  printf("%d",i);
}

Yes I have not initialized the variable 'i'
This code gives output 0

lets see this code
Code:
main()
{
  int i;
  --i;
  printf("%d",i);
}
This code gives answer 813;

Why this the output 813, when the initial value in variable i is 0;

and have a look at this code
Code:
main()
{
  int i;
  printf("%d",i);
  getch();
}
Now this code does not give output as 0 but it gives -28710

Code:
main()
{
  int i;
  --i;
  printf("%d",i);
  getch();
}
Now this code gives the output 841.


Even using the function clrscr() give different results.

Can anuone explain these observations,

Thanks a lot
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Sure. But since you know that i is uninitialized and you're farbling with it anyway, you won't be satisfied. Your code results in what the standard describes as undefined behavior.

Your time would be much better invested in learning the language properly. If that's boring, at the moment, perhaps you should READ the standard.

Now, if you'll excuse me, I'm going to go bombard various parts of my body with random levels of radiation. Should be an interesting experiment.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You try printing the address of i in each run of your program and see what is the value at that location and you will get the answer to your query.
0
vignesh1988i's Avatar
Banned
Good morning ,

since you have not implicitly given the value of 'i' or u have not initilized 'i' , so in 'i' every time u run the program , this 'i' will contain different garbage values.........

thank u