我们在编写IOS程序时会用到C中的输入流(scanf( );)输出流(printf();)。
下面是一段简单的代码,让我们来分析一下
#import <Foundation/Foundation.h>
#include <stdlib.h>
int main (int argc, const char * argv[]) {
// NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
int magic;
int guess;
magic=rand();
NSLog(@"Hello, World!");
printf("Guess the magic number");
scanf("%d",&guess);
if(guess==magic){printf("guess 和 magic 相等");}
else if(guess>magic){
printf("guess >magic");
}
else{
printf("guess < magic");
}
// [pool drain];
return 0;
}
运行后会看到如下界面:
执行到这时:
printf("Guess the magic number");
scanf("%d",&guess);
这两句代码执行了,guess the magic number 是printf("Guess the magic number");这段代码的执行出来的,语句
printf();是将一段代码输出出来。
光标处为 scanf("%d",&guess);这段代码执行效果,为guess变量通过键盘输入值。
在光标处打入数字,程序继续执行,如图:
到此程序执行完毕。
此外函数rand();是随机生成一个数字。并且需要<stdlib.h> 头文件的支持。