CH7-1 输入输出

输入输出库 <stdio.h> <string.h> <ctype.h>
7.1  标准输入输出
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//<和>表示在标准库位置寻找,UNIX下一般在/usr/include中
//stdio.h中 int c = getchar(); putchar(c); printf(); EOF
//ctype.h中 tolower(c); toupper(c);
//stdlib.h中 EXIT_SUCCESS
int main( int argc, char** argv) {
                printf( "Please input the 1st string end with Ctrl+D:\n");
                 int c;                                                        
                 while((c = getchar()) != EOF)         //EOF表示结束,Ctrl+D,//int getchar(void)
                                putchar(tolower(c));   //tolower函数包含在ctype.h头文件中

                printf( "Please input the 2nd string end with Ctrl+D:\n");
                 int ch;
                 while((ch = getchar()) != EOF)
                                putchar(toupper(ch));  //toupper函数包含在ctype.h头文件中

                printf( "EOF's value is: %d\n", EOF);  //EOF定义在stdio.h中,值为-1
                 return (EXIT_SUCCESS);  //EXIT_SUCCESS 一般为0
}
7.2  格式化输出 printf  sprintf
#include <stdio.h>
/* int printf(char *format, arg1, arg2, ...);  //返回打印字符个数
* %[-][width].[precision][conversion]
* %% 当打印%时,须%%         printf("Hello World! %%\n");
* - 表示左对齐,无表示右对齐
* width 至少宽度。若其值不够长,则左端或右端用空格补齐;若超过,则全部显示。
                                printf("%2d\n", 123456); 结果为12345
                                printf("%10d\n", 123456);结果为        123456
* precision        (1)若string类型,则最大打印的字符数,若不足,不补
                                        printf("%.2s\n", "123456789");结果为12
                                (2)若浮点型,则小数点后的精度(缺省为6),若不足用0补齐
                                        printf("%.2f\n", 123.456); 结果为123.45
                                        printf("%.5f\n", 123.456); 结果为123.4500
                                (3)若是整型,为最小显示长度
                                        printf("%.2d\n", 1);    结果为01
*/
int main( int argc, char** argv) {
        printf( "%d\n", -123);     //-123
        printf( "%o\n", 123);         //173
        printf( "%x\n", 123);         //7b
        printf( "%u\n", 123);         //123
        
        printf( "%c\n", 123);         //{
        
        printf( "%s\n", "123");     //123

        printf( "%f\n", 123.456); //123.456000 (缺省为6)
        printf( "%e\n", 123.456); //1.234560E+02(缺省为6)
        printf( "%g\n", 123.456); //123.456
         return 0;
}
 
#include <stdio.h>
// int sprintf(char *string, char *format, arg1, arg2, ...);
//string的长度必须能容下format的长度,包括'\0'.
int main( int argc, char** argv) {
         char str[20];
        sprintf(str, "Score is: %d \n", 34);
        printf( "%s\n", str);
         return 0;
}
7.3 函数参数个数可变举例
#include <stdio.h>
#include <stdarg.h> //va_start,va_arg,va_end
//例子:函数参数个数可变
void minprintf( char *format, ...){ //注意:...表示参数的个数和类型可变
        va_list argumentPointer; //变量list
         char *p;
         int intVal;
         double doubleVal;
         char *strVal;

        va_start(argumentPointer, format); //argumentPointer指向第一个无名参数

         for(p = format; *p; p++){ //遍历format中的每一个字符
                 if(*p != '%'){             //如果不是%,则直接putchar打印出来
                        putchar(*p);
                         continue;
                }
                 switch(*++p){             //如果是%,则判断其下一个字符
                         case 'd':
                                intVal = va_arg(argumentPointer, int); //va_arg以int型取一个参数,并将argumentPointer指向下一个参数
                                printf( "%d", intVal);
                                 break;
                         case 'f':
                                doubleVal = va_arg(argumentPointer, double);
                                printf( "%f", doubleVal);
                                 break;
                         case 's':
                                 for(strVal = va_arg(argumentPointer, char*); *strVal; strVal++)
                                        putchar(*strVal);
                                 break;
                         default:
                                putchar(*p);
                                 break;
                }
        }
        va_end(argumentPointer);
}

int main( int argc, char** argv) {
        minprintf( "My name is: %s, age is: %d, height is: %f\n", "amao", 30, 165.5);
        minprintf( "My name is: %s, age is: %d\n", "stone", 22);
         return 0;
}
7.4  格式化输出 scanf
#include <stdio.h>
/*
* int scanf(char *format, arg1,...) //返回成功保存的参数个数
* 以format将标准输入保存到{参数指向的地址}中,arg1等参数必须为地址指针
*    
* int sscanf(char *string, char *format, arg1, arg2, ...);
* 从string中读取数据
*/
/*    
* d,u,i,o,x的前面可以加 h 表示short,l(ell)表示long
*/
void scanfInteger(){
         int n;
        scanf( "%d", &n);     //按照十进制将整数读入,并存在n的地址空间中
        printf( "%d\n", n);

        scanf( "%u", &n);     //按照十进制将无符号整数读入
        printf( "%d\n", n);

        scanf( "%i", &n);     //按照八进制(0开头)或者十六(0x开头)进制读入
        printf( "%d\n", n); //若输入017,输出15 //若输入0xf,输出15

        scanf( "%o", &n);     //按照八进制(0开头可有可无)读入
        printf( "%d\n", n); //若输入17,输出15 //若输入017,输出15

        scanf( "%x", &n);     //按十六进制(0x开头可有可无)读入
        printf( "%d\n", n); //若输入0xf,输出15 //若输入f,输出15
}
/*
* %c 读取下一个字符,注意有可能是空字符,若要录入第一个非空字符,请用%1s
* %c 相当于 getchar()
*/
int scanfCharString(){
         char ch;
         char str[20];        
        scanf( "%c, %s", &ch, str); //输入a,amao
        printf( "%c %s\n", ch, str); //输出a amao
}
/*
* f,e,g以不同的格式录入float,lf表示录入的double而非float
* 输入:12.34 12.34 12.34 12345.6789
* 输出:12.340000 1.234000e+01 12.34 12345.678900
*/
int scanfFloat(){
         float    f1, f2, f3;
         double d1;
        scanf( "%f %e %g %lf", &f1, &f2, &f3, &d1);    
        printf( "%f %e %g %f\n", f1, f2, f3, d1);
}

void sscanfTest(){
         int n, num;
         float f;
        num = sscanf( "123 12.34", "%d %f", &n, &f);
        printf( "n = %d f = %f num = %d \n", n, f, num);
}

int main1( int argc, char** argv) {
        scanfInteger();
        scanfCharString();
        scanfFloat();
        sscanfTest();                
         return 0;
}
例子:
#include <stdio.h>

void acc(){
         float i, sum = 0;
         while(scanf( "%f", &i) == 1)
                printf( "%.2f\n", sum += i);
}

int getline( char *line, int max){ //fgets从文件中读一行
         if(fgets(line, max, stdin) == NULL) //stdin标准输入
                 return 0;
         else
                 return strlen(line);     //strlen返回字符串line的长度
}

void time(){
         int day, month, year;
         char monthName[20];
        
         char line[20];     //思考:为什么char *line不行???
         while(getline(line, sizeof(line)) > 0){    
                 if(sscanf(line, "%d %s %d", &day, monthName, &year) == 3)
                        printf( "valid: %s\n", line);     //输入31 sep 2001
                 else if(sscanf(line, "%d/%d/%d", &month, &day, &year) == 3)
                        printf( "valid: %s\n", line);     //输入9/31/2001 mm/dd/yy
                 else printf( "invalid: %s\n", line);
        }
}

int main( int argc, char** argv) {
        acc();
        time();
         return 0;
}
注意:
#include <stdio.h>

int main( int argc, char** argv) {
         char str1[20], *str2;     //str1是一段空间的头地址
        scanf( "%s", str1);
        
         //scanf("%s", str2); //错误,原因是str2只是声明为一个char型指针,其还未开辟空间,故不能接收字符串
         //printf("%s %s\n", str1, str2);

        str2 = str1; //str2指向了str1开始的空间
        scanf( "%s", str2);
        printf( "%s %s\n", str1, str2);    
}

你可能感兴趣的:(职场,休闲)