我是C初学者,已经开始在c中编写代码。我对变量的范围有所怀疑。当任何变量写入块内时,它的作用域就在该块内。但是,当使用返回字时,在块外部访问变量的方式如何?如何使用“返回”时访问块外的任何变量?
实施例:
int add(int a, int b)
{
int c;//scope of c is within this block
c=a+b;
return c;
} //it ends here
void main()
{
int answer;
answer=add(2,3);//how we gets value of "c " here
printf("%d",answer);
}
您忘记了'javascript'和'php'标签。 –
在C和C++中,函数在语义上返回变量的一个副本。至少在C++中,编译器可以自由地删除副本。 – juanchopanza