1.scanf函数相关应用
#include <stdio.h>
int main( )
{
int i=0;
float f;
char c1,c2;
//scanf("%3d%f",&i,&f);//3d表示只取3个位
//printf("i=%010d,f=%010.2f\n",i,f);//前面的0表示剩余的空位用0补齐。
int w;
//for (w=1;w<5;w++)
//printf("%c,%*c\n",'a',w,'a');//中间的*代表每次输入时打印w-1个空格(若w-1<0,以0计算)。
//scanf("%3d%3d",&i,&w);//截取3位,如果在这3位中有空白字符,停止。
//printf("i=%d,w=%d\n",i,w);
//scanf("%*d%d",&i,&w);//*表示禁止,第一次输入的数字忽略。
//printf("i=%d,w=%d\n",i,w);
//scanf("%[a-d]%c",&c1,&c2);//输入[a-d]之间的字符,如果没有输入a-d之间的字符,
//程序不能正常取数值,注意,输入字符时,不能过滤空白字符。[^a-d]表示非(a-z)之间字符
//printf("c1=%c,c2=%c\n",c1,c2);
scanf("%*[^\n]");
scanf("%*c");//清空输入缓冲区。(==fflush(stdin))
char str1[20],str2[20];
//scanf("%*[^\n]");scanf("%*c");
//scanf("%[a-z]%[^a-z^\n]", str1, str2);//后面的^\n表示输入的数据会立即生效。
scanf("%[a-z]%[^a-z^\n]", str1, str2);//a-z表示只提取a-z之间字符,若输入为abc123ty,str1=abc.
printf("str1=%s, str2=%s\n", str1, str2);
return 0;
}
2.在一串字符中取出小数
#include <stdio.h>
int main()
{
float farray[20];int i=0;
FILE *pf=fopen("1.txt","r");
float d;
char ch;
while (ch=getc(pf),ch!=EOF)//没有取到文件结尾时
{
if (ch<='9' && ch>='1')
{
ungetc(ch,pf);//使用ungetc才能将小数的第一位数取到,不然会错过一位。因为当ch中为数字时,指针已经向后走了一位,此时后面的scanf会错过一位,所以要用ungetc。
fscanf(pf,"%f",&d);
farray[i++]=d;
printf("%f\n",d);
}
}
fclose(pf);
return 0;
}
#include <stdio.h>
#include <ctype.h>
//把整数取出来
void main( void )
{
int ch;
int result = 0;
printf( "Enter an integer: " );
while ( (ch=getchar())!=EOF && isdigit(ch)!=0 )
{result=result*10+ch;}
if (ch!=EOF)
{ungetc(ch,stdin);}
printf("%c\n",getchar());
}
3.两个文件copy
//从命令行输入源文件名和目标文件名,完成copy
#include <stdio.h>
int main( int argc,int *argv[] )
{
FILE *psoruce,*pdest;char c;
if (argc!=3)//输入格式错误
{
fprintf(stderr,"the usage is: %s source dest\n",argv[0]);
return 1;
}
if ( psoruce=fopen(argv[1],"r"),NULL==psoruce )
{
fprintf(stderr,"open %s fail!\n",argv[1]);
return 1;
}
if ( pdest=fopen(argv[2],"w"),NULL==pdest )
{
fprintf(stderr,"open %s fail!\n",argv[2]);
fclose(psoruce);
return 1;
}
while (c=getc(psoruce),c!=EOF)
{putc(c,pdest);}
fclose(psoruce);fclose(pdest);
}
附:
int *restrict a//restrict 是在间接寻址的时候进行优化,在c99中新增的,这个关键字只能用于修饰指针,因为指针才有间接寻址。
register int x=10;//register是指将变量请求放到cpu寄存器中,这样对它操作的速度会很快,但是只是请求,程序不一定会放在寄存器中。
volatile int y=20;//volatile表示此变量是一个易变变量,在用它的时候要小心,可以阻止编译器的一些优化操作,
4.指针一些操作
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void pt(int (*a)[4],int n)
{
int i,j;
for (i=0;i<n;i++)
{
for (j=0;j<4;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
//double x;
//strcpy((char *)&x,"abcde");
//printf("%s\n",&x);
//int y;
//char *pc=(char*)&y;
//*pc='a';
//*(pc+1)='b';
//*(pc+2)='c';
//*(pc+3)='d';
//printf("%#x\n",y);//#x输出就是0x
// char *p=(char *)malloc(8);
// strcpy(p,"abcde");
// printf("%s\n",p);
// free(p);
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
// double *pd=(double*)a;
// int i=0;
// for(i=0;i<6;i++)
// pd[i]=i+0.12;
// for (i=0;i<6;i++)
// {
// printf("%lf\t",pd[i]);
// }
char *pc=(char*)a;
strcpy(pc,"abcdefg");
printf("%s\n",pc);//分配的内存可以存储给double型,也可以给char来应用 。
//int (*p)[4];//p和*p都是一样的,因为p是一个数组指针,指向一个数组,所以它的*p和p是一样的,p+1指向下组数据,p+1=*(p+1).只不过类型不一样。
//p=a;
//char **pa=(char **)a;//windows的编译检查得到了加强,要做强制类型转换才可以。
//*pa=1;*(pa+1)=2;
// printf("%p\n",p);
// printf("%p\n",*p);
// printf("%d\n",**p);
// printf("%p\n",p+1);
// printf("%p\n",*p+1);
// printf("%d\n",**(p+1));
//printf("%c %c\n",*pa,*(pa+1)); //pa指向二维数组展开的一维数组的首地址,pa+1是向后走4个字节(若在int 型中,刚好指向下一个字节,若在char和double中,不会是这种情况)。
//pt(a,3);
return 0;
}
5. stdout、stdin、stderr
#include <stdio.h>
//每一个进程都默认的打开这三个文件:
//FILE *stdin; //标准输入设备文件(键盘)
//FILE *stdout; //标准输出设备文件(显示器)
//FILE *stderr; //标准错误输出设备文件(显示器)
int main()
{
fprintf(stdout,"%s","hello world!\n");//此句同2句是一样的。
//printf("%s","hello world!\n");
fprintf(stderr,"%s","hello world!\n");
int x;
fscanf(stdin,"%d",&x);//此句同下句是一样的。
scanf("%d",&x);
/* stdout和stderr的区别:
1.stdout有输出缓冲区,stderr没有输出缓冲区。
2.stdout可以重定向,stderr没有重定向(也就是stdout可以把输出结果送到其它地方,stderr不可以);
3.什么时候stdout会清空缓冲区:
当输出换行时、缓冲区满时、用fflush刷新时、等待用户输入时、程序运行结束时,当缓冲区清空时,缓冲区的内容会送到显示器*/
return 0;
}
6.sscanf、sprintf函数
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=100;
int y=200;
char c;
double d;
char s[100]={0};
//sprintf(s,"%d-%d",x,y);//sprint 可以向得到想要的字符串类型。
//printf("%s\n",s);
//char *ps="100";
//int z=atoi(ps);//atoi将一个字符串转化成一个整数
//printf("%d\n",z);
//double d=3.14;
//sprintf(s,"%7.3f",d);
//printf("%s\n",s);
char *ps="a 10 3.14 string";
sscanf(ps,"%c%d%lf%s",&c,&x,&d,s);
printf("%c-%d-%lf-%s",c,x,d,s);
return 0;}