在C++中,3.6.1主要功能
(3.6.1/5)main中的return语句具有离开main函数(销毁具有自动存储持续时间的任何对象)并以返回值作为参数调用exit的效果.如果控制到达main的末尾而没有遇到return语句,则效果是执行return 0;
我可以在C99中执行以下操作而不返回0吗?
int main() { }
Run Code Online (Sandbox Code Playgroud) 我不太明白这种情况下的if语句是如何工作的.它评估x != 0语句,当它不再是真的时,它会分配z给if语句y然后分配breakif语句?
int main()
{
int x, y, z, i;
x = 3;
y = 2;
z = 3;
for (i = 0; i < 10; i++) {
if ((x) || (y = z)) {
x--;
z--;
} else {
break;
}
}
printf("%d %d %d", x, y, z);
}
Run Code Online (Sandbox Code Playgroud) 我编写了一个递归函数,用于查找数组中的最小数,效果很好。
/*01*/ int minimo(int array[], int n)
/*02*/ {
/*03*/ static int min = 0;
/*04*/
/*05*/ if (n == N)
/*06*/ {
/*07*/ return array[n-1];
/*08*/ }
/*09*/ else
/*10*/ {
/*11*/ min = minimo(array, n+1);
/*12*/ if(array[n]<min){
/*13*/ min = array[n];
/*14*/ }
/*15*/ }
/*16*/ }
Run Code Online (Sandbox Code Playgroud)
唯一的问题是它不应该工作,因为它不会将“min”返回给调用者......
int main()
{
//Var
int array[N] = {10, 2, 5, 1, 7};
printf("Min: %d\n", minimo(array, 0));
}
Run Code Online (Sandbox Code Playgroud)
我的担忧实际上是一个问题,但不是在我的机器上,该功能可以正常工作;这是我朋友的笔记本电脑和 IDE 上的一个问题,我尝试在朋友的 Macbook 上复制到 XCode,但如果“return min;”这一行就无法工作。未在函数末尾添加。
在第 15-16 行之间,我必须添加 …
一时兴起,我尝试使用clang 2.9将main函数定义为模板函数:
template <typename T = void>
int main(int argc, char **argv)
{
}
Run Code Online (Sandbox Code Playgroud)
并收到以下错误.
error: 'main' cannot be a template
int main(int argc, char **argv)
^
Run Code Online (Sandbox Code Playgroud)
有谁知道标准的哪一部分禁止这个,以及相关的文字是什么?
我一直在使用C中的main方法
void main(){ // my code }
Run Code Online (Sandbox Code Playgroud)
它对我来说效果很好.我也知道其他的 int返回类型:
int main(void)
int main()
int main(int argc, char *argv[])
Run Code Online (Sandbox Code Playgroud)
但是我找不到任何可以void用作返回类型的资源.每本书都建议返回类型必须int或者省略.然后为什么void main()工作?
这取决于我使用的C版本吗?或者它是否有效,因为我使用的是C++ IDE?请具体回复C而不是C++.
这个问题出于好奇; 在为测试程序编写main时,我没有返回任何内容main(没有return声明main).但我宣称主要是int main().它编译成功.
如果有任何其他函数用int返回类型编写并且实际上没有返回int,我会得到一个错误
'函数名'必须返回值
那么为什么编译器不会为main功能抱怨相同?
这是一个非常基本的C问题,来自Kernighan和Ritchie的第18页.
我编译了这个非常简单的代码来计算从键盘输入的字符:
#include <stdio.h>
/* count characters in input; 1st version */
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%1d\n", nc);
}
Run Code Online (Sandbox Code Playgroud)
这个编译很好,运行正常,并且表现得非常符合预期,即如果我输入"Hello World",当我按下CTRLD以给出EOF字符时,它返回值11 .
令我困惑的是,如果我犯了错误,我可以使用退格键删除字符并重新输入它们,它只返回终端在调用EOF时显示的字符数.
如果代码计算每个字符,包括特殊字符,如果我输入四个字符,删除两个,再输入另外两个,那么输出不应该是8个字符(4个字符+2个del + 2个字符),而不是4个字符?
我显然误解了C如何处理退格,以及代码如何/何时递增变量nc?
昨天我参加了一次采访,在那里我看到了一个奇怪的节目片段.
初步一瞥我决定Snippet中有编译错误.但是当回到家并在C编译器中手动尝试时,我发现我完全错了
见面试代码
#include<stdio.h>
void main()
{
for(int i=0;i=5;i=5)// here condition and increment are assigned
//wrongly and where I thought it is compilation
//error during interview which isn't wrong
{
printf("helloworld \n");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
.
.
.
.(goes on and on)
Run Code Online (Sandbox Code Playgroud)
C++中的输出类似于C
但,
当我们在java编译器中运行此代码时
public class Forhottest {
public static void main(String args[])
{
for(int i=0;i=5;i=5)// here it throws compilation error
{
System.out.println("helloworld");
}
}
}
Run Code Online (Sandbox Code Playgroud)
同样我在PHP中试过,同样的问题出现在java中.为什么C和C++在"for循环"中允许这种奇怪的条件语句.它背后的原因是什么
我的书中有一个问题:
#include<stdio.h>
void main()
{
int a=5, b=-7, c=0, d;
d = ++a && ++b || ++c;
printf("\n%d%d%d%d",a,b,c,d);
}
Run Code Online (Sandbox Code Playgroud)
这个问题问我代码的输出是什么.我跑了,屏幕上的结果是6-601.我明白为什么a=6和b=-6,但我不明白为什么c=0和d=1?