#include <stdio.h>
void numberisone()
{
int number = 1;
}
int main()
{
numberisone();
printf("%d", number);
return 0;
}
I'm fairly new to programming so make the explanation as simple as possible :) Thanks in advance!
CodePudding user response:
The scope of the declaration of the variable number is the outermost block of the function numberisone
void numberisone()
{
int number = 1;
}
Outside the function the variable is not alive and visible.
So the used identifier number in main is undeclared
printf("%d", number);
To make your program more or less meaningful declare the function like
int numberisone( void )
{
return 1;
}
and in main write
int number = numberisone();
CodePudding user response:
#include <stdio.h>
int numberisone()
{
int number = 1;
return number;
}
int main()
{
int num_1 = 0;
num_1 = numberisone();
printf("%d", num_1);
return 0;
}
CodePudding user response:
Variables declare inside a function have a scope inside that function only that is the variable can be use only inside the function. If you nee you can return the value from the function as like,
#include <stdio.h>
int numberisone()
{
int number = 1;
return number;
}
int main()
{
printf("%d", numberisone());
return 0;
}
