Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   some observations need explanation (http://www.go4expert.com/forums/observations-explanation-t5686/)

hitesh_best 5Aug2007 17:13

some observations need explanation
 
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

DaWei 5Aug2007 19:27

Re: some observations need explanation
 
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.

shabbir 5Aug2007 20:51

Re: some observations need explanation
 
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.

vignesh1988i 3Oct2009 10:17

Re: some observations need explanation
 
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:pleased:


All times are GMT +5.5. The time now is 03:27.