当前位置:文档之家› c语言输出格式控制

c语言输出格式控制

1.转换说明符%a(%A) 浮点数、十六进制数字和p-(P-)记数法(C99)%c 字符%d 有符号十进制整数%f 浮点数(包括float和doulbe)%e(%E) 浮点数指数输出[e-(E-)记数法]%g(%G) 浮点数不显无意义的零"0"%i 有符号十进制整数(与%d相同)%u 无符号十进制整数%o 八进制整数 e.g. 0123%x(%X) 十六进制整数0f(0F) e.g. 0x1234%p 指针%s 字符串%% "%"2.标志左对齐:"-" e.g. "%-20s"右对齐:"+" e.g. "%+20s"空格:若符号为正,则显示空格,负则显示"-" e.g. "% 6.2f"#:对c,s,d,u类无影响;对o类,在输出时加前缀o;对x类,在输出时加前缀0x;对e,g,f 类当结果有小数时才给出小数点。

3.格式字符串(格式)[标志][输出最少宽度][.精度][长度]类型"%-md" :左对齐,若m比实际少时,按实际输出。

"%m.ns":输出m位,取字符串(左起)n位,左补空格,当n>m or m省略时m=ne.g. "%7.2s" 输入CHINA输出" CH""%m.nf":输出浮点数,m为宽度,n为小数点右边数位e.g. "%3.1f" 输入3852.99输出3853.0长度:为h短整形量,l为长整形量printf的格式控制的完整格式:% - 0 m.n l或h 格式字符下面对组成格式说明的各项加以说明:①%:表示格式说明的起始符号,不可缺少。

②-:有-表示左对齐输出,如省略表示右对齐输出。

③0:有0表示指定空位填0,如省略表示指定空位不填。

④m.n:m指域宽,即对应的输出项在输出设备上所占的字符数。

N指精度。

用于说明输出的实型数的小数位数。

为指定n时,隐含的精度为n=6位。

⑤l或h:l对整型指long型,对实型指double型。

h用于将整型的格式字符修正为short型。

---------------------------------------格式字符格式字符用以指定输出项的数据类型和输出格式。

①d格式:用来输出十进制整数。

有以下几种用法:%d:按整型数据的实际长度输出。

%md:m为指定的输出字段的宽度。

如果数据的位数小于m,则左端补以空格,若大于m,则按实际位数输出。

%ld:输出长整型数据。

②o格式:以无符号八进制形式输出整数。

对长整型可以用"%lo"格式输出。

同样也可以指定字段宽度用“%mo”格式输出。

例:main(){ int a = -1;printf("%d, %o", a, a);}运行结果:-1,177777程序解析:-1在内存单元中(以补码形式存放)为(1111111111111111)2,转换为八进制数为(177777)8。

③x格式:以无符号十六进制形式输出整数。

对长整型可以用"%lx"格式输出。

同样也可以指定字段宽度用"%mx"格式输出。

④u格式:以无符号十进制形式输出整数。

对长整型可以用"%lu"格式输出。

同样也可以指定字段宽度用“%mu”格式输出。

⑤c格式:输出一个字符。

⑥s格式:用来输出一个串。

有几中用法%s:例如:printf("%s", "CHINA")输出"CHINA"字符串(不包括双引号)。

%ms:输出的字符串占m列,如字符串本身长度大于m,则突破获m的限制,将字符串全部输出。

若串长小于m,则左补空格。

%-ms:如果串长小于m,则在m列范围内,字符串向左靠,右补空格。

%m.ns:输出占m列,但只取字符串中左端n个字符。

这n个字符输出在m列的右侧,左补空格。

%-m.ns:其中m、n含义同上,n个字符输出在m列范围的左侧,右补空格。

如果n>m,则自动取n值,即保证n个字符正常输出。

⑦f格式:用来输出实数(包括单、双精度),以小数形式输出。

有以下几种用法:%f:不指定宽度,整数部分全部输出并输出6位小数。

%m.nf:输出共占m列,其中有n位小数,如数值宽度小于m左端补空格。

%-m.nf:输出共占n列,其中有n位小数,如数值宽度小于m右端补空格。

⑧e格式:以指数形式输出实数。

可用以下形式:%e:数字部分(又称尾数)输出6位小数,指数部分占5位或4位。

%m.ne和%-m.ne:m、n和”-”字符含义与前相同。

此处n指数据的数字部分的小数位数,m表示整个输出数据所占的宽度。

⑨g格式:自动选f格式或e格式中较短的一种输出,且不输出无意义的零。

---------------------------------------关于printf函数的进一步说明:如果想输出字符"%",则应该在“格式控制”字符串中用连续两个%表示,如:printf("%f%%", 1.0/3);输出0.333333%。

---------------------------------------对于单精度数,使用%f格式符输出时,仅前7位是有效数字,小数6位.对于双精度数,使用%lf格式符输出时,前16位是有效数字,小数6位.######################################拾遗 ########################################由高手指点对于m.n的格式还可以用如下方法表示(例)char ch[20];printf("%*.*s\n",m,n,ch);前边的*定义的是总的宽度,后边的定义的是输出的个数。

分别对应外面的参数m和n 。

我想这种方法的好处是可以在语句之外对参数m和n赋值,从而控制输出格式。

--------------------------------------------------------------------------------今天(06.6.9)又看到一种输出格式 %n 可以将所输出字符串的长度值赋绐一个变量, 见下例:int slen;printf("hello world%n", &slen);执行后变量被赋值为11。

头文件:string.h格式:strlen (字符数组名)功能:计算字符串s的(unsigned int型)长度,不包括'\0'在内说明:返回s的长度,不包括结束符NULL。

string.h文件中函数的详细用法下面为string.h文件中函数的详细用法,附加实例:1、strcpy函数名: strcpy 功能: 拷贝一个字符串到另一个用法: char *strcpy(char *destin, char *source); 程序例: #include <stdio.h> #include <string.h> int main(void) { char string[10]; char *str1 = "abcdefghi"; strcpy(string, str1);printf("%s\n", string); return 0; }2、strcat函数名: strcat 功能: 字符串拼接函数用法: char *strcat(char *destin, char *source); 程序例: #include <string.h> #include <stdio.h> int main(void) { char destination[25]; char *blank = " ", *c = "C++", *Borland = "Borland"; strcpy(destination, Borland); strcat(destination, blank); strcat(destination, c); printf("%s\n", destination); return 0; }3、strchr函数名: strchr 功能: 在一个串中查找给定字符的第一个匹配之处\ 用法: char *strchr(char *str, char c); 程序例: #include <string.h> #include <stdio.h> int main(void) { char string[15]; char *ptr, c = 'r'; strcpy(string, "This is a string"); ptr = strchr(string, c); if (ptr) printf("The character %c is at position: %d\n", c, ptr-string); else printf("The character was not found\n"); return 0; }4、strcmp函数名: strcmp 功能: 串比较用法: int strcmp(char *str1, char *str2); 看Asic码,str1>str2,返回值> 0;两串相等,返回0 程序例: #include <string.h> #include <stdio.h> int main(void) { char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; int ptr; ptr = strcmp(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); else printf("buffer 2 is less than buffer 1\n"); ptr = strcmp(buf2, buf3); if (ptr > 0) printf("buffer 2 is greater than buffer 3\n"); else printf("buffer 2 is less than buffer 3\n"); return 0; }5、strnicmp函数名: strnicmp 功能: 将一个串中的一部分与另一个串比较, 不管大小写用法: int strnicmp(char *str1, char *str2, unsigned maxlen); 程序例: #include <string.h> #include <stdio.h> int main(void) { char *buf1 = "BBB", *buf2 = "bbb"; int ptr; ptr = strnicmp(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0) printf("buffer 2 is less than buffer 1\n"); if (ptr == 0) printf("buffer 2 equals buffer 1\n"); return 0; }6、strcpy函数名: strcpy 功能: 串拷贝用法: char *strcpy(char *str1, char *str2); 程序例: #include <stdio.h> #include <string.h> int main(void) { char string[10]; char *str1 = "abcdefghi"; strcpy(string, str1); printf("%s\n", string); return 0; }7、strcspn函数名: strcspn 功能: 在串中查找第一个给定字符集内容的段用法: int strcspn(char *str1, char *str2); 程序例: #include <stdio.h> #include <string.h> #include <alloc.h> int main(void) { char *string1 = "1234567890"; char *string2 = "747DC8"; int length; length = strcspn(string1, string2); printf("Character where strings intersect is at position %d\n", length); return 0; }8、strdup函数名: strdup 功能: 将串拷贝到新建的位置处用法: char *strdup(char *str); 程序例: #include <stdio.h> #include <string.h> #include <alloc.h> int main(void) { char *dup_str, *string = "abcde"; dup_str = strdup(string); printf("%s\n", dup_str); free(dup_str); return 0; }9、stricmp函数名: stricmp 功能: 以大小写不敏感方式比较两个串用法: int stricmp(char *str1, char *str2); 程序例: #include <string.h> #include <stdio.h> int main(void) { char *buf1 = "BBB", *buf2 = "bbb"; int ptr; ptr = stricmp(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0) printf("buffer 2 is less than buffer 1\n"); if (ptr == 0) printf("buffer 2 equals buffer 1\n"); return 0; }10、strerror函数名: strerror 功能: 返回指向错误信息字符串的指针用法: char *strerror(int errnum); 程序例: #include <stdio.h> #include <errno.h> int main(void) { char *buffer; buffer = strerror(errno); printf("Error: %s\n", buffer); return 0; }11、strcmpi函数名: strcmpi 功能: 将一个串与另一个比较, 不管大小写用法: int strcmpi(char *str1, char *str2); 程序例: #include <string.h> #include <stdio.h> int main(void) { char *buf1 = "BBB", *buf2 = "bbb"; int ptr; ptr = strcmpi(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0) printf("buffer 2 is less than buffer 1\n"); if (ptr == 0) printf("buffer 2 equals buffer 1\n"); return 0; } 函数名: strncmp 功能: 串比较用法: int strncmp(char *str1, char *str2, int maxlen); 程序例: #include <string.h> #include <stdio.h> int main(void) { char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; int ptr; ptr = strncmp(buf2,buf1,3); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); else printf("buffer 2 is less than buffer 1\n"); ptr = strncmp(buf2,buf3,3); if (ptr > 0) printf("buffer 2 is greater than buffer 3\n"); else printf("buffer 2 is less than buffer 3\n"); return(0); }12、strncpy函数名: strncpy 功能: 串拷贝用法: char *strncpy(char *destin, char *source, int maxlen); 程序例: #include <stdio.h> #include <string.h> int main(void) { char string[10]; char *str1 = "abcdefghi"; strncpy(string, str1, 3); string[3] = '\0'; printf("%s\n", string); return 0; }13、strnicmp函数名: strnicmp 功能: 不注重大小写地比较两个串用法: int strnicmp(char *str1, char *str2, unsigned maxlen); 程序例: #include <string.h> #include <stdio.h> int main(void) { char *buf1 = "BBBccc", *buf2 = "bbbccc"; int ptr; ptr = strnicmp(buf2, buf1, 3); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0) printf("buffer 2 is less than buffer 1\n"); if (ptr == 0) printf("buffer 2 equals buffer 1\n"); return 0; }14、strnset函数名: strnset 功能: 将一个字符串前n个字符都设为指定字符用法: char *strnset(char *str, char ch, unsigned n); 程序例: #include <stdio.h> #include <string.h> int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x'; printf("string before strnset: %s\n", string); strnset(string, letter, 13); printf("string after strnset: %s\n", string); return 0; }15、strpbrk函数名: strpbrk 功能: 在串中查找给定字符集中的字符用法: char *strpbrk(char *str1, char*str2); 程序例: #include <stdio.h> #include <string.h> int main(void) { char *string1 = "abcdefghijklmnopqrstuvwxyz"; char *string2 = "onm"; char *ptr; ptr = strpbrk(string1, string2); if (ptr) printf("strpbrk found first character: %c\n", *ptr); else printf("strpbrk didn't find character in set\n"); return 0; }16、strrchr函数名: strrchr 功能: 在串中查找指定字符的最后一个出现用法: char *strrchr(char *str, char c); 程序例: #include <string.h> #include <stdio.h> int main(void) { char string[15]; char *ptr, c = 'r'; strcpy(string, "This is a string"); ptr = strrchr(string, c); if (ptr) printf("The character %c is at position: %d\n", c, ptr-string); else printf("The character was not found\n"); return 0; }17、strrev函数名: strrev 功能: 串倒转用法: char *strrev(char *str); 程序例: #include <string.h> #include <stdio.h> int main(void) { char *forward = "string"; printf("Before strrev(): %s\n", forward); strrev(forward); printf("After strrev(): %s\n", forward); return 0; }18、strset函数名: strset 功能: 将一个串中的所有字符都设为指定字符用法: char *strset(char *str, char c); 程序例: #include <stdio.h> #include <string.h> int main(void) { char string[10] = "123456789"; char symbol = 'c'; printf("Before strset(): %s\n", string); strset(string, symbol); printf("After strset(): %s\n", string); return 0; }19、strspn函数名: strspn 功能: 在串中查找指定字符集的子集的第一次出现用法: int strspn(char *str1, char *str2); 程序例: #include <stdio.h> #include <string.h> #include <alloc.h> int main(void) { char *string1 = "1234567890"; char *string2 = "123DC8"; int length; length = strspn(string1, string2); printf("Character where strings differ is at position %d\n", length); return 0; }20、strstr函数名: strstr 功能: 在串中查找指定字符串的第一次出现用法: char *strstr(char *str1, char *str2); 程序例: #include <stdio.h> #include <string.h> int main(void) { char *str1 = "Borland International", *str2 = "nation", *ptr; ptr = strstr(str1, str2); printf("The substring is: %s\n", ptr); return 0; }21、strtod函数名: strtod 功能: 将字符串转换为double型值用法: double strtod(char *str, char **endptr); 程序例: #include <stdio.h> #include <stdlib.h> int main(void) { char input[80], *endptr; double value; printf("Enter a floating point number:"); gets(input); value = strtod(input, &endptr); printf("The string is %s the number is %lf\n", input, value); return 0; }22、strtok函数名: strtok 功能: 查找由在第二个串中指定的分界符分隔开的单词用法: char *strtok(char *str1, char *str2); 程序例: #include <string.h> #include <stdio.h> intmain(void) { char input[16] = "abc,d"; char *p; /* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, ","); if (p) printf("%s\n", p); /* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token */ p = strtok(NULL, ","); if (p) printf("%s\n", p); return 0; }23、strtol函数名: strtol 功能: 将串转换为长整数用法: long strtol(char *str, char **endptr, int base); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { char *string = "87654321", *endptr; long lnumber; /* strtol converts string to long integer */ lnumber = strtol(string, &endptr, 10); printf("string = %s long = %ld\n", string, lnumber); return 0; }24、strupr函数名: strupr 功能: 将串中的小写字母转换为大写字母用法: char *strupr(char *str); 程序例: #include <stdio.h> #include <string.h> int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; /* converts string to upper case characters */ ptr = strupr(string); printf("%s\n", ptr); return 0; }25、swab函数名: swab 功能: 交换字节用法: void swab (char *from, char *to, int nbytes); 程序例: #include <stdlib.h> #include <stdio.h> #include <string.h> char source[15] = "rFna koBlrna d"; char target[15]; int main(void) { swab(source, target, strlen(source)); printf("This is target: %s\n", target); return 0; 原型:extern char *strstr(char *haystack, char *needle); *所在头文件:#include <string.h> *功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。

相关主题