输入格式控制
输入格式:printf( % [flag] type)
- type
scanf("%i",&a); //030
printf("a=%d\n",a); //a=24
char s[100]={0};
scanf("%[^/]",s); //hello word/
printf("%s",s); //hello word
- [flag]
scanf("%*d%d",&b); //1 2
printf("%d",b); //2
scanf("%5s",s); //abcdefgh
printf("%s",s); //abcde
输出格式控制
输出格式:printf( % [flag] [width] [precision] [hIL] type)
- type
printf("%d\n",-1); //-1
printf("%u\n",1); //1
printf("%o\n",8); //10
printf("%x\n",10); //a
printf("%X\n",10); //A
printf("%f\n",1.1223); //1.122300
printf("%e\n",1223); //1
printf("%c\n",'r'); //r
printf("%s\n","hello"); //hello
int a;
printf("%p\n",&a); //000000000062FE1C
- [hIL]修饰符
#include<stdio.h>
int main()
{
printf("sizeof(char)=%ld\n",sizeof(char)); //sizeof(char)=1 =>256
printf("sizeof(short)=%ld\n",sizeof(short)); //sizeof(short)=2 =>65536
printf("sizeof(int)=%ld\n",sizeof(int)); //sizeof(int)=4 =>4294967296
printf("sizeof(long)=%ld\n",sizeof(long)); //sizeof(long)=4 =>4294967296
printf("sizeof(long long)=%ld\n",sizeof(long long)); //sizeof(long long)=8 =>18446744073709551616
printf("sizeof(long double)=%ld\n",sizeof(long double)); //sizeof(long double)=16 =>3.4e+38
//hh=>(short)
printf("hh\n");
printf("256=%hhd\n",256);
printf("65536=%hhd\n",65536);
printf("4294967296=%hhd\n",4294967296);
printf("18446744073709551616=%hhd\n",18446744073709551616);//[Warning] integer constant is too large for its type
printf("\n");
//h=>(short)
printf("h\n");
printf("256=%hd\n",256);
printf("65536=%hd\n",65536);
printf("4294967296=%hd\n",4294967296);
printf("18446744073709551616=%hd\n",18446744073709551616);//[Warning] integer constant is too large for its type
printf("\n");
//l=>()
printf("l\n");
printf("256=%ld\n",256);
printf("65536=%ld\n",65536);
printf("4294967296=%ld\n",4294967296);
printf("18446744073709551616=%ld\n",18446744073709551616);//[Warning] integer constant is too large for its type
printf("\n");
//ll=>()
printf("ll\n");
printf("256=%lld\n",256);
printf("65536=%lld\n",65536);
printf("4294967296=%lld\n",4294967296);
printf("18446744073709551616=%lld\n",18446744073709551616);//[Warning] integer constant is too large for its type
printf("\n");
//L=>()
printf("L\n");
printf("256=%Ld\n",256);
printf("65536=%Ld\n",65536);
printf("4294967296=%Ld\n",4294967296);
printf("18446744073709551616=%Ld\n",18446744073709551616);//[Warning] integer constant is too large for its type
return 0;
}
测试结果:
(测试环境:Dev C++ 5.11 , 64位编译器)
sizeof(char)=1
sizeof(short)=2
sizeof(int)=4
sizeof(long)=4
sizeof(long long)=8
sizeof(long double)=16
hh=>short
256=256
65536=0
4294967296=0
18446744073709551616=0
h=>short
256=256
65536=0
4294967296=0
18446744073709551616=0
l=>long
256=256
65536=65536
4294967296=0
18446744073709551616=0
ll=>long long
256=256
65536=65536
4294967296=4294967296
18446744073709551616=0
L=>long
256=256
65536=65536
4294967296=0
18446744073709551616=0
- [precision]小数精度
printf("%.2f\n",10.123456);//10.12
printf("%.*f\n",3,10.123456);//10.123
int n=5;
printf("%.*f\n",n,10.123456);//10.12346
- [width]输出字节长度
printf("%6d\n",1); // 1
printf("%*d\n",9,1); // 1
- [flag]标志符
printf("%6d\n",1); // 1
printf("%-6d\n",1); //1
printf("%+d\n",1); //+1
printf("%09d\n",1); //000000001
字符输入
scanf
scanf("%c",&c);
printf("%c\n",c);
//6
//6
getchar
- getchar函数原型:int getchar (void)
- putchar函数原型:int putchar(int c)
getchar 将shell输入按字符读入,同时返回字符对应数值
putchar(a) 将a按字符输出,同时返回a
int a=getchar(); //1
int b=putchar(a); //1
printf("\n");
printf("a=%d\n",a); //a=49
printf("b=%d",b); //b=49
字符串输入
scanf
- 输入以 空格 或者 回车 结束,空格回车留在缓冲区
char str1[1000]="\0";
scanf("%s",str1);
printf("%s",str1);
//123
//123
//123 456
//123
- 以指定字符结束
char str1[1000]="\0";
scanf("%[^'5']",str1);
printf("%s",str1);
//123 456
//123 4
gets
- 以 回车 结束
char str1[3]="\0";
gets(str1);
printf("%s",str1);
//123 456
//123 456
windows cmd命令行重定位
输出重定位 test.c>b.txt
源文件
#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
cmd 命令行操作
C:\Users\gaope\Documents\CodeBlock\Practice>test.exe>b.txt
1594862
b.txt内容
1594862
输入重定位 test.exe<b.txt
源代码
#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
b.txt 内容
1594862
cmd命令行操作
C:\Users\gaope\Documents\CodeBlock\Practice>test.exe<b.txt
1594862