GeekInterview.com
  I am new, Sign me up!
 
Home C
 

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

 
Category: C
Comments (4)


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.



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



 

 

Comments


Mahesh Babu said:

  I think when a variable is declared outside main() then it becomes global variable so it can be used anywhere in the program.
April 30, 2008, 10:43 am

ShalabhSoni said:

  it simply takes a garbage value in case if declared within the main()
May 27, 2008, 5:30 pm

1 said:

  It shows error that the variable is not initialised
April 28, 2009, 1:25 am

sandesh k said:

  when a variable is not declared in the main function ,you can't use the varaible in the program unless it is a global variable.
initialisation is required to give some value to the declared variable.(it depends upon the logic you are using to solve the problem.) if you dont initialise , first it will store a garbage value it. if is is not overwritten in the course of the program, then when you print the variable a garbage value will be printed.................
May 2, 2009, 12:33 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape