00--printf和scanf的星号区别
#include <stdio.h>
int main(void)
{
unsigned a,b;
int c=678;
double d=789.56;
int n;
printf("enter a is:");
scanf("%d",&a);
printf("show %*d\n",a,c);//显示无*对应的数
printf("now enter :");
scanf("%d,%d",&a,&b);
printf("the result is %*.*f\n",a,b,d);//显示有*对应的数
printf("the result is %**f\n",a,b,d);
//printf("the result is %*f\n",a,b,d);
printf("now enter num:");
scanf("%*d,%*d,%d",&n);//只显示最后一个数(显示无*对应的数)
printf("show is %d\n",n);
printf("now enter num:");
scanf("%*d,%d,%*d",&n);
printf("show is %d\n",n);
printf("the result is:");
printf("%5c,%7c,%9c,%11c\n",'67','A','\45',66);
}
在scanf中*表示这个位置占位符对应的输入将被忽略。
例:
%2*c和%*c分别隔过去两个和一个字符。
但若加入逗号,改为
可见此时%2*c不能用,但%*c可用。
若为
printf:
在printf中*表示用后面的值替代*的位置
例
01--printf输出类型
#include<stdio.h>
#define GG 3549
#define SS "JSHDDJS WEYYQWW"
int main(void)
{
const double DD=3457.89;
const int II=43;
printf("is *%d*\n\n",GG);
printf("is *%2d*\n\n",GG);
printf("is *%10d*\n\n",GG);
printf("is *%-10d*\n\n",GG);
printf("is *%d*\n\n\n",GG);
printf("*%f*\n\n",DD);
printf("*%e*\n\n",DD);
printf("*%4.2f*\n\n",DD);
printf("*%3.1f*\n\n",DD);
printf("*%10.3f*\n\n",DD);
printf("*%10.3e*\n\n",DD);
printf("*%+4.2f*\n\n",DD);
printf("*%010.4f*\n\n",DD);
printf("*%.5f\n\n",DD);
printf("*%5.3d*,*%5.4d*,*%05d*,*%05.3d*\n\n",II,II,II,II);
printf("*%s*\n\n",SS);
printf("*%2s*\n\n",SS);
printf("*%30.5s*\n\n",SS);
printf("*%-30.5s*\n\n",SS);
return 0;
}
02--getchar和scanf混合输入
#include<stdio.h>
void disp(char cr,int line,int width);
int main()
{
int ch ;
int row,col;
printf("enter the text :\n");
while((ch=getchar())!='\n')
{
if(scanf("%d %d",&row,&col)!=2)
{
break;
}
disp(ch,row,col);
while(getchar()!='\n')
{
continue;
}
printf("enter again:\n");
printf("fill out\n");
}
printf("bye\n");
}
void disp(char cr,int line,int width)
{
int row,col;
for(row=1;row<=line;row++)
{
for(col=1;col<=width;col++)
{
putchar(cr);
putchar('\n');
}
}
}
03--atexit注册函数
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void isnum();
void nonum();
int main(void)
{
int n;
//功 能: 注册终止函数(即main执行结束后调用的函数)
atexit(isnum);//注册isnum函数
//size_t size;
//size=sizeof(int);
puts("enter the n:\n");
while(scanf("%d",&n)!=1)
{
puts("no number\n");
atexit(nonum);//注册nonum函数,先调用后注册的函数
exit(EXIT_FAILURE);
}
printf("%d is %s\n",n,(n%2==0)? "even":"odd");
//printf("is %d\n",size);
getch();
return 0;
}
void isnum()
{
puts("is num\n");
}
void nonum()
{
puts("no num\n");
}
04--三个特殊符号
#include<stdio.h>
main()
{
int i=0;
printf("this is the three char :??)\n");
printf("this is the three char :??(\n");
printf("this is the three char :??!\n");
printf("this is the three char :??<\n");
printf("this is the three char :??>\n");
printf("this is the three char :??'\n");
printf("\n");
printf("this is the three char :??/ ");
printf("this is the three char :??=\n");
printf("this is the three char :??-\n");
printf("this is the three char :??)\n");
while(i<10)
{
printf("now listen the music:\a\n");
i++;
}
while(i<5)
{
printf("now listen the music:\a\n");
i++;
}
while(i<20)
{
printf("now listen the music:\a\n");
i++;
}
while(i<30)
{
printf("now listen the music:\a\n");
i++;
}
}
05--参数类型数值范围
#include<stdio.h>
#include<limits.h>
#include<float.h>
int main(void)
{
printf("the INT limits is %d--%d\n\n",INT_MIN,INT_MAX);
printf("the UINT limits is %#X\n\n",UINT_MAX);
printf("the LONG IS %ld--%ld\n\n",LONG_MIN,LONG_MAX);
printf("the CHAR limits is %d--%d\n\n",CHAR_MIN,CHAR_MAX);
printf("the SCHAR IS %d--%d\n\n",SCHAR_MIN,SCHAR_MAX);
printf("the uchar is %d\n\n",UCHAR_MAX);
printf("the CHAR BIT IS %d\n\n",CHAR_BIT);
printf("the FLOAT limits is %e--%e\n\n",FLT_MIN,FLT_MAX);
printf("the DIG IS %d\n\n",FLT_DIG);//小数点后保留的位数
printf("the DOUBLE limits is %e--%e\n\n",DBL_MIN,DBL_MAX);
printf("the INT limits is %d--%d\n\n",INT_MIN,INT_MAX);
printf("the INT limits is %d--%d\n\n",INT_MIN,INT_MAX);
}
06--大小写的转换
#include<stdio.h>
#include<ctype.h>
#define uint unsigned int
#define uchar unsigned char
main()
{
uchar *p,*p1,*p2;
uchar *str;
uchar str1[20]="sdf.google.com";//长度要指定
uchar str2[12]="www";
p=toupper('h');//只能改变字符
str=toupper("a");//没法改变字符串
p1=tolower('F');
p2=memmove(str1,str2,3);//,类似于memcpy,strncpy
//不会因NUL而停下
printf("is %c\n",p);
printf("is %c\n",p1);
printf("is %s\n",str);
printf("is %s\n",p2);
}