这个程序的输出是什么?
#include<iostream> using namespace std; int x; // Global x int main() { int x = 10; // Local x cout << "Value of global x is " << ::x << endl; cout << "Value of local x is " << x; return 0; } |
输出:
Value of global x is 0 Value of local x is 10
在C++中,全局变量(如果我们有同名的局部变量),可以使用范围解析运算符(::)访问。
这个程序的输出是什么?
#include <iostream> using namespace std; int a = 90; int fun( int x, int *y = &a) { *y = x + *y; return x + *y; } int main() { int a = 5, b = 10; a = fun(a); cout << a << " " << b << endl; b = fun(::a,&a); cout << a << " " << b << endl; return 0; } |
100 10 195 290
有两个名为“a”的变量,一个是全局变量,另一个是局部变量。当我们打电话时 a=乐趣(a); ,它调用int fun(int x,int*y=&a),在这里,指向全局变量(a=90)的指针被分配给y。 *y=x+*y;//变成5+90 返回x+*y;//变成5+95
这个程序的输出是什么?
#include <iostream> using namespace std; int a = 2; int fun( int *a) { ::a *= *a; cout << ::a << endl; return *a; } int main() { int a = 9; int &x = ::a; ::a += fun(&x); cout << x; } |
输出:
4 8
每次使用::a时,函数fun都会访问全局变量。局部变量值不会影响a的值。
这个程序的输出是什么?
#include <iostream> using namespace std; int main() { char *A[] = { "abcx" , "dbba" , "cccc" }; char var = *(A+1) - *A+1; cout << (*A + var); } |
输出:
bba
这里的数组表示是[0]=“abcx”,A[1]=“dbba”,A[2]=“cccc”。(指针)*>(二进制)+的优先级,“*”的执行顺序是从右到左。如果“A”的地址是“x”,那么“*(A+1)”的地址是“x+6”,而“*A+1”的地址是“x+1”。所以var的整数值=6(两点(x+6)-(x+1)+1之间的字符总数)。在打印过程中,运算符“+”被重载,现在指针指向“x+7”。由于这个原因,程序的输出。 这个程序的输出是什么?
#include <iostream> using namespace std; int main() { char a = 'a' , b = 'x' ; char c = (b ^ a >> 1 * 2) +(b && a >> 1 * 2 ); cout << " c = " << c; } |
输出:
c = 97
整数值a=97(0110001),整数值b=120(01111000),优先级为“*”>“>>”>“^”>“&&&”。 所以表达式是“((b^(a>>(1*2))–(b&(a>>(1*2)))”。表达式的整数值 a>>1*2=24b^a>>1*2=96b&&a>>1*2=1,即97。
这个程序的输出是什么?
#include <iostream> using namespace std; int main() { int i = 5, j = 3; switch (j) { case 1: if (i < 10) cout << "case 1" ; else if (i > 10) case 2: cout << "case 2" ; else if (i==10) case 3: default : cout << "case 3" ; cout << "hello" ; } } |
输出:
case 3hello
因为j=3满足条件,所以在情况3中也是如此。案例3中没有任何内容,也没有中断。所以默认值被执行。请参考 C语言中的switch语句 详细信息。
本文由 普尼特、斯姆里蒂·萨蒂亚纳拉亚纳、阿卡什·蒂瓦里、萨加尔三重病 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。