c语言函数局部代码块代码块
C programming Functions and Blocks Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Functions and Blocks – Scope of the block, function’s declarations, function’s definition, function’s calling etc.
C编程功能和块能力问题解答:在本节中,您将找到有关功能和块的C能力问题解答–块的范围,函数的声明,函数的定义,函数的调用等。
1) What will be the output of following program ?
#include <stdio.h>
int main()
{
int var=100;
{
int var=200;
printf("%d...",var);
}
printf("%d",var);
return 0;
}
Answer
Correct Answer - 4
200...100
You can define blocks with in the funtion body by using { .. }, int var=200; is global in main function for block defined in main() body and int var=100; is local for same block.
200...100
You can define blocks with in the funtion body by using { .. }, int var=200; is global in main function for block defined in main() body and int var=100; is local for same block.
1)以下程序的输出是什么?
回答
正确答案-4
200 ... 100
您可以使用{..}, int var = 200;在函数主体中使用定义块 。 在main()主体中定义的块的main函数中是全局变量,并且 int var = 100; 对于同一块是本地的。
200 ... 100
您可以使用{..}, int var = 200;在函数主体中使用定义块 。 在main()主体中定义的块的main函数中是全局变量,并且 int var = 100; 对于同一块是本地的。
2) What will be the output of following program ?
#include <stdio.h>
char* fun1(void)
{
char str[]="Hello";
return str;
}
char* fun2(void)
{
char *str="Hello";
return str;
}
int main()
{
printf("%s,%s",fun1(),fun2());
return 0;
}
Answer
Correct Answer - 4
Garbage,Hello
fun1() suffers from the dangling pointer, The space for "Hello" is created dynamically as the fun1() is called, and is also deleted on the exiting of the function fun1(), so that data is not available in calling function (main()).
Garbage,Hello
fun1() suffers from the dangling pointer, The space for "Hello" is created dynamically as the fun1() is called, and is also deleted on the exiting of the function fun1(), so that data is not available in calling function (main()).
2)以下程序的输出是什么?
回答
正确答案-4
垃圾,你好
fun1()受 指针悬空的影响 ,“ Hello”的空间是在调用fun1()时动态创建的,并且在函数fun1()退出时也会被删除,因此在调用函数(主要())。
垃圾,你好
fun1()受 指针悬空的影响 ,“ Hello”的空间是在调用fun1()时动态创建的,并且在函数fun1()退出时也会被删除,因此在调用函数(主要())。
3) What will be the output of following program ?
#include <stdio.h>
int fooo(void)
{
static int num=0;
num++;
return num;
}
int main()
{
int val;
val=fooo();
printf("step1: %d\n",val);
val=fooo();
printf("step2: %d\n",val);
val=fooo();
printf("step3: %d\n",val);
return 0;
}
Answer
Correct Answer - 2
step1: 1
step2: 2
step3: 3
The life time of a static variable is the entire program i.e. when we call fooo() again num will not declare again.
step1: 1
step2: 2
step3: 3
The life time of a static variable is the entire program i.e. when we call fooo() again num will not declare again.
3)以下程序的输出是什么?
回答
正确答案-2
步骤1:1
步骤2:
第三步:3
静态变量的生命周期是整个程序,即,当我们再次调用fooo()时, num不会再次声明。
步骤1:1
步骤2:
第三步:3
静态变量的生命周期是整个程序,即,当我们再次调用fooo()时, num不会再次声明。
4) What will be the output of following program ?
#include <stdio.h>
int main()
{
int anyVar=10;
printf("%d",10);
return 0;
}
extern int anyVar;
Answer
Correct Answer - 2
10
The extern variable cannot initialize with block scope, and you are free to declare them outside of block scope, extern makes the variable accessible in other files.
10
The extern variable cannot initialize with block scope, and you are free to declare them outside of block scope, extern makes the variable accessible in other files.
4)以下程序的输出是什么?
回答
正确答案-2
10
extern变量无法使用块作用域进行初始化,您可以在块作用域之外声明它们,extern使该变量可在其他文件中访问。
10
extern变量无法使用块作用域进行初始化,您可以在块作用域之外声明它们,extern使该变量可在其他文件中访问。
翻译自: https://www.includehelp.com/c-programs/c-functions-and-blocks-aptitude-questions-and-answers.aspx
c语言函数局部代码块代码块