C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-07-2009, 10:48 AM   #1
Registered User
 
Join Date: Feb 2009
Posts: 10
question about variable initializations

let's say we have this code

Code:
main()
{
int k;
while (k!=0)
{
scanf("%d",&k);
printf("%f\n",100/k);
}
}
our teacher said that this will stop because k would have value of 0

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   Reply With Quote
Old 11-07-2009, 10:52 AM   #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   Reply With Quote
Old 11-07-2009, 10:55 AM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by jackhasf View Post
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?
I believe the C standard does guarantee uninitialized GLOBAL variables are set to 0/NULL -- but I'm not positive, since I always set it myself if that's what I want.

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   Reply With Quote
Old 11-07-2009, 11:19 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:36 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22