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

What happens when a variable is not declared in function definition?

 
Category: C
Comments (1)

Generally in C program the function definition and calling takes the form as given below:


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


sample(x1,y1)
int x1,y1;
{
int z1;
z1= x1 - y1;
return(z1);
}



Here what happens is the values x, y gets passed to x1,y1 and the value z1 is calculated in sample function and the value z1 is returned which is got in z. This value namely z is then printed.


Now let us see if the variables are not declared before the braces in the function and if it declared as below:


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


sample(x1)
{
int x1;
int z1;
z1= x1+20;
return(z1);
}


This would give an error. Surprised!, Yes because the variable x1 is not declared before the braces of the function and so by default would be assumed to be a integer variable. Again the declaration of integer variable x1 inside the function braces would give an error as Redeclaration of variable x1


Hence care must be taken not to do as above.



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



 

 

Comments


nethra14 said:

  you will get an error because you are trying to declare x1 twice or because you haven't specified the type of x1

otherwise it works fine
July 19, 2009, 3:14 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