C 程序的输出 |第 54 集
1. 下面程序的输出是什么?
C++
#include
#define GEEKS 100
int main()
{
#define GEEKS 100
printf("%d", GEEKS);
return (0);
}
Output:
- 100
C++
#include
#include
int main()
{
printf("%d%d", sizeof("GEEKSFORGEEKS"), strlen("GEEKSFORGEEKS"));
return (0);
}
C++
#include
int main()
{
int a = - - 4;
printf("%d", a);
return (0);
}
C++
#include
int main()
{
char c[] = "GFG";
int i;
for (i = 0; c[i]; i++) {
printf("%c%c%c%c", c[i], *(c + i), *(i + c), i);
}
return (0);
}
C++
#include
int main()
{
int a = 055, b = 55;
printf("%d%d", a, b);
return (0);
}
选项:
1. 100
2.编译错误
3.无输出
4.异常终止
The answer is option(1).
说明:作为 GEEKS 宏,将 GEEKS 替换为 100 令牌。
参考:https://www.geeksforgeeks.org/interesting-facts-preprocessors-c/
2. 以下程序的输出是什么?
C++
#include
#include
int main()
{
printf("%d%d", sizeof("GEEKSFORGEEKS"), strlen("GEEKSFORGEEKS"));
return (0);
}
选项:
1. 1314
2. 1134
3. 1413
4. 3114
The output is option(3).
说明:众所周知, strlen()函数返回长度不计算 \0(null)字符,而 sizeof()函数返回带有 \0字符的字符串长度。
参考:https://www.geeksforgeeks.org/difference-strlen-sizeof-string-c-reviewed/
3. 猜输出?
C++
#include
int main()
{
int a = - - 4;
printf("%d", a);
return (0);
}
选项:
1. -4
2.编译错误
3.运行时错误
4. 4
The answer is option(4).
说明:当我们使用一元减(-)运算符两次时,编译器将其视为加号。
4. 输出是什么?
C++
#include
int main()
{
char c[] = "GFG";
int i;
for (i = 0; c[i]; i++) {
printf("%c%c%c%c", c[i], *(c + i), *(i + c), i);
}
return (0);
}
GGGGFFFFGGGG
选项:
1.编译时错误
2. GGGFFFGGG
3.GFG
4.无输出
The answer is option(2).
解释:这里所有的 c[i], *(c+i), *(i+c), i 代表同一个东西。在第一个循环中,所有四个说明符都指向 G,这就是为什么在第一个循环中它打印 GGG,在第二个循环中打印 FFF,最后在第三个循环中打印 GGG。
5. 以下程序的输出是什么?
C++
#include
int main()
{
int a = 055, b = 55;
printf("%d%d", a, b);
return (0);
}
选项:
1. 4555
2. 5545
3. 5545
4. 5554
The answer is option(1).
说明:任何以 0 为前缀的整数表示为八进制数。现在将 055 转换为十进制数后,我们得到 45,b 是 55。因此答案是 4555。