使用Xcode可创建Command Line Tool项目,语言选择C来测试写的C函数


main.c默认输出"Hello, World!"
自定义一个两个数字相加的C函数:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
int result = add(10, 20);
printf("result = %d\n", result);
return 0;
}
PS: 因为C语言是顺序执行的,定义的add函数需要在调用的上面,否则会报错: Implicit declaration of function 'add' is invalid in C99
command + R执行稍等片刻就会看到控制台输出:
Hello, World!
result = 30
Program ended with exit code: 0
定义的两个整型数字相加的函数:
int add(int a, int b)
从左至右,第一个int代表返回值类型,add为函数名,括号中a、b是两个形参,他们的类型是int
网友评论