Knowledge Base
Home C

What happens when a variable is not initialized in main function?

Category: C | Comments (2)

When a variable is not initialized in main function it contains garbage value. This can be well seen from the example below

main() { int x; printf(“%d”,x); z= sample() }


sample() { printf(“Testing program”); }

Output is


    x=80


Testing program


The above program prints a garbage value and the output testing program. This is because the variable x is not initialized and so the variable x had garbage value which is printed first then the function sample is called which gave output as testing program. Thus it is essential to initialize variables in main () function.


Next: What is the default return value of a function?


Comments

I think when a variable is declared outside main() then it becomes global variable so it can be used anywhere in the program.
Comment posted by: Mahesh Babu on 2008-04-30T10:43:11
it simply takes a garbage value in case if declared within the main()
Comment posted by: ShalabhSoni on 2008-05-27T17:30:49


Post Comment


Members Please Login

Name:


Email:
 
(Optional. Used for Notification)

Title:

 
Comment:


Validation Code:
 <=>  (Enter this code in text box)
Subscribe





Daily Email Updates

Get Latest Knowledge Base Updates delivered directly to your Inbox...

Enter your email address:

Latest Knowledge Base Updates
 

C Tutorials

 

Related Tutorials