在jupyternotebook中写C/C++


在jupyter notebook中写C/C++,最大的好处就是不用写main()函数,直接调用写好的函数即可执行。

#include
int sum(int a,int b){
    printf("a=%d,b=%d",a,b);
    return a+b;
}

当运行函数时后面可以不用加;,像python一样直接输出函数的返回值;

sum(2,3)
a=2,b=3




5

如果加了;,函数仍然可以正常运行,只是不会像上面一样直接输出结果。

sum(2,3);
a=2,b=3

在一个单元格里只能定义一个函数,若定义两个函数就会报错,

int sum1(int a,int b){
    return a+b;
}
int sum2(int a,int b){
    return a+b;
}
input_line_49:5:23: error: function definition is not allowed here
 int sum2(int a,int b){
                      ^



Interpreter Error: