![]() |
| | #1 |
| Registered User Join Date: Feb 2009
Posts: 10
| question about variable initializations Code: main()
{
int k;
while (k!=0)
{
scanf("%d",&k);
printf("%f\n",100/k);
}
}
but, this is not true cause when i run it, it works for me. so i would like to ask, what are the variable initializations in C? If we give a variable no value, will it have value of 0? thanks |
| jackhasf is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| A global variable will be initialized to zero if you don't explicitly initialize it with something else. A local variable, such as you have, will be indeterminate.
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #3 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Otherwise (eg, in the case of a stack variable local to main(), as you have above) the memory address is not cleared and could contain any value.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #4 |
| Registered User Join Date: Apr 2006
Posts: 1,193
| Technically, Only integer values are guaranteed to be 0 in the global scope. Other types will be equal to whatever all zeros represents for that type. This means that technically pointers and floating point numbers are not guaranteed to be set to NULL/0.0, as the C standard does not specify how these are encoded. They are not garunteed unless you assume IEEE floating point, and that a NULL points to the 0th address. Both of these are solid assumptions for any system.
__________________ It is too clear and so it is hard to see. A dunce once searched for fire with a lighted lantern. Had he known what fire was, He could have cooked his rice much sooner. |
| King Mir is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| swap variable question | Overworked_PhD | C Programming | 8 | 07-04-2008 07:26 PM |
| uninitialized local variable question | raist | C++ Programming | 5 | 12-02-2006 03:00 PM |
| simple scope question | 7stud | C++ Programming | 18 | 01-30-2005 07:45 AM |
| Variable Arguments Question | moonwalker | C Programming | 8 | 08-04-2002 09:08 AM |
| Variable Allocation in a simple operating system | awkeller | C Programming | 1 | 12-08-2001 02:26 PM |