Abstruct:

    最近在学习niosII的相关软件联系,发现JTAG UART还是很好用的,但是自己对用到的一些输入输出函数等有些生疏了,所以网上搜了搜资料,整理了一下

Introduction:

字符串输入:
gets()
    char
name[20];
    printf("Hi,What's your name?\n");
    gets(name);
   
printf("Nice name %s",name);
它使用一个地址把字符串赋予name。

    char
name[20];
    char *p;
    printf("\nEn,What's your name?\n");
   
p=gets(name);
    printf("%s?Oh,Nice name %s\n",name,p);
get()的代码使用return
关键字返回字符串的地址,程序把这个地址分配给指针p。

fgets()
fgets()是为文件I/O而设计的,处理键盘输入不是特别方便。

   
printf("\nHi,What's your name?\n");
    p=fgets(name,20,stdin);
   
printf("%s?Oh,Nice name
%s\n",name,p);
*fgets()的第二个参数说明最大读入的字符数。如果这个参数值为n,那么fgets()就会读取最多n-1个字符或读完一个换行符为止。两个条件满足任意一个结束。
*fgets()读取到换行符,就会把它存到字符串里,而不是想gets()那样丢弃它。
*fgets()的第三个参数说明读哪个文件。从键盘上读数据时,可以使用stdin(代表standard
input)作为参数。

scanf()
    char name1[11],
name2[11];
    int count;
    printf("\nPlease write down 2
names...\n");
    count=scanf("%5s %6s",name1,name2);
   
printf("\nname1:%s\nname2:%s",name1,name2);

scanf()允许指定输入字符串长度等格式。上面的程序如果输入"liujiajia
liujiajia",程序将输出"name1:liuji   name2:liujia";

字符串输出
puts()
   
char str[15]="hello world";
    const char *str2="HELLO WORLD";
   

    puts(str);
    puts(str2);
    puts(&str[5]);
   
puts(str2+2);
puts()显示字符串时自动在其后添加一个换行符。
puts(&str[5]);将输出从str的第六个元素开始到字符串结束。
puts(str2+2);将输出从str2的地址向后移动两个字符开始到字符串结束。

fputs()
   
fputs(str,stdout);
    fputs(str2,stdout);
   
fputs(&str[5],stdout);
   
fputs(str2+2,stdout);
*fputs()第二个参数表示要写的文件。可以使用stdout(代表standard
output)作为参数。
*fputs()不自动输出换行符,这与puts()不太相同。

printf()

posted on 2012-05-19 15:44  lbyzsf  阅读(912)  评论(0编辑  收藏  举报