格式输入输出函数

合集下载

格式化输入、输出函数

格式化输入、输出函数

格式化输入/输出函数格式输出函数(printf)格式字符表格式字符说明d或i 以十进制形式输出带符号整数(正数不输出符号)o 以八进制形式输出无符号整数(不输出前缀0)x,X 以十六进制形式输出无符号整数(不输出前缀0x),对于x用abcdef输出;对于X用ABCDEF输出u 以十进制形式输出无符号整数f 以小数形式输出单、双精度实数,隐含输出6位小数e,E 以指数形式输出单、双精度实数,数字部分小数位数为6位小数,指数部分占5位,用“E”时,指数以大写表示g,G 以%f或%e中较短的输出宽度输出单、双精度实数,不输出无意义的0,用“G”时,则指数以大写表示c 输出单个字符s 输出字符串p 输出标量的内存地址未指定宽度和指定输出宽度时的输出结果输出语句输出结果printf(“%3d\n”,4321); 4321(按实际位数输出)printf(“%f\n”,123.54); 123.540000(按实际需要宽度输出)printf(“%12f\n”,123.54); 凵凵123.540000(输出右对齐,左边填空格)printf(“%e\n”,123.54); 1.235400e+002(按实际需要宽度输出)printf(“%14e\n”,1213.54); 凵1.235400e+002(输出右对齐,左边填空格)printf(“%g\n”,123.5); 123.5(%f格式比采用%e格式输出宽度小)printf(“%8g\n”,123.5); 凵凵凵123.5(输出右对齐,左边填空格)指定精度时的输出结果输出语句输出结果printf(“%8.3f\n”,123.55); 凵123.550printf(“%8.1f\n”,123.55); 凵凵凵123.6printf(“%8.0f\n”,123.55); 凵凵凵凵凵124printf(“%g\n”,123.56789); 123.568printf(“%.7g\n”,123.56789); 123.5679printf(“%.5s\n”,”abcdefg”); abcde注:在VC中float类型有7位有效数字,double类型有16位有效数字标志及其意义标志意义-输出结果左对齐,右边填空格;缺省则输出结果右对齐,左边填空格+输出符号(正号或负号)空格输出值为正时冠以空格,为负时冠以负号﹟对c,s,d,u类无影响;对o类,在输出时加前缀0,对x类,在输出时加前缀0x 0 对数值格式,在指定宽度的同时,输出数据左边空格处填以数字0标志的用法输出语句输出结果printf(“%6d\n”,111); 凵凵凵111printf(“%-6d\n”,111); 111凵凵凵printf(“%+d\n”,111); +111printf(“% d\n”,111);/*%和d之间有一个空格*/ 凵111printf(“% d\n”,-111);/*%和d之间有一个空格*/ -111printf(“%#o\n”,10); 012printf(“%#x\n”,16); 0x10printf(“%06.2f\n”,1.6); 001.60格式输入函数(scanf)Scanf格式字符格式字符说明d,i 输入有符号的十进制整数o 输入无符号的八进制整数x,X 输入无符号的十六进制整数u 输入无符号的十进制整数f,e 输入实型数(用小数形式或指数形式)c 输入单个字符s 输入字符串,结果存入字符数组中。

基本输入输出函数格式输出函数printf格式

基本输入输出函数格式输出函数printf格式

正确语句:
printf( “ Input a,b,c=“);
scanf( “%d, %d, %d”, &a, &b, &c);
printf( “\n” );
/*可省略*/

scanf( “%5f “, &x);
应特别注意输入格式: 如:scanf( “a=%d,b=%d,c=%d \n”,&a,&b,&c); 执行时应输入:
如果第二句改为:
printf(“|%15s|%012f\n”,”LI CHANG”,234.45);
运行结果:|ZHANG WEI
|165.13|
|
LI CHANG|00234.450000|
注意:格式字符与对应输出项类型要一致。
【例】若float y=1234.9999;printf(“%d”,y);则输出结 果为何?
a=10,b=20,c=30 <enter>
三、非格式化输入、输出函数 1.字符输出函数putchar(c)
其中 c为字符型/整型的常量/变量/表达式 【例】以下程序运行结果是什么? #include <stdio.h>
main() { char x='a'; clrscr(); putchar(x); putchar('b'); putchar(x+2); putchar('b'+2); }
结果:abcd
2.字符输入函数
格式:
getchar( ) getche( ) getch( )
功能:
从键盘上读入一个字符,但后两个函数输入后无需回车。 getch( )还有一个功能,即不把读入的字符回显到屏幕上, 常用于密码输入或菜单选择。

5-输出函数printf,putchar,输入函数scanf,getchar

5-输出函数printf,putchar,输入函数scanf,getchar
• 若数据位数大于m,则按实际位数输出。
例如:
printf(“%-5d,%-5d,%-2d”,123,456,1234);
输出结果:
123_ _, 456_ _,1234
D、%ld:用于输出long型数据。尤其范围超过 int型范围的数据。 E、%mld:指定long型数据的输出宽度,左补 空格。 F、%-mld:指定long型数据的输出宽度,右补 空格。
(1)该函数只能输出一个字符。
(2)函数的使用形式:putchar(表达式);
(3)功能:在屏幕上输出表达式对应的字符。
(4)表达式的类型:

字符变量或字符常量: putchar('$');/*输出字符常量*/ char ch;ch=‘B' ; putchar(ch); 整型变量或整型常量: 0~255之间: int i=65;putchar(i);
输入缓冲区 1
2 3 a 2 3 2 3 .
5 6 \n
程序从输入缓冲区中读取数据: a=123 b=a c=23.56

字符输入函数:getchar()
(1)功能:接收从键盘输入的一个字符
(2)函数的返回值:为输入字符的ASCII码
(3)使用形式:
A、char/int变量=getchar(); B、putchar(getchar());/*作为函数的参数*/ C、getchar();/*接收数据,但不做任何处理等价
(7)f格式符:
A、%f:以小数形式输出一个实数。系统在输 出时规定:

整数位:全部输出 小数点:占屏幕1列 小数位:为6位
例如:
double a=12345678.89;
float b=12345678.89;

C语言输入输出函数printf与scanf的用法格式

C语言输入输出函数printf与scanf的用法格式

C语言输入输出函数printf与scanf的用法格式C语言中的输入输出函数printf和scanf是我们在编程中经常用到的两个函数。

它们分别用于向屏幕输出数据和从键盘输入数据。

1.printf函数printf函数是C语言中用于输出的标准函数,其基本格式如下:printf("format string", argument);其中,format string是格式化字符串,它可以是任何你希望输出的文本或特定格式的文本,argument是你要输出的参数,可以是任何数据类型,例如int, float, double等。

例如,如果你想输出一个整数,你可以这样写:int x = 10;printf("The value of x is: %d", x);在这个例子中,%d是一个格式说明符,它表示一个整数。

在输出的时候,%d 会被x的值替换。

printf函数还支持许多其他类型的格式说明符,例如:•%f:浮点数•%c:字符•%s:字符串•%p:指针的值•%x:十六进制数等等。

你可以根据需要选择适合的类型。

2.scanf函数scanf函数是C语言中用于输入的标准函数,其基本格式如下:scanf("format string", argument);与printf类似,format string是格式化字符串,argument是你要输入的参数。

但是,scanf的格式说明符与printf略有不同。

例如,如果你想输入一个整数,你可以这样写:int x;scanf("%d", &x);在这个例子中,%d是一个格式说明符,它表示一个整数。

在输入的时候,你需要在%d前面输入一个数字,然后scanf会把这个数字存储到&x所指向的内存地址中。

注意,这里的&是取地址运算符,表示我们想要的是变量x的内存地址。

scanf函数还支持许多其他类型的格式说明符,例如:•%f:浮点数•%c:字符•%s:字符串(到第一个空格为止)•%p:指针的值•%x:十六进制数(必须以0x或0X开头)等等。

C语言输入输出函数格式详解

C语言输入输出函数格式详解

1、输入和输出:输入:输入也叫读,数据由内核流向用户程序输出:输出也称写、打印,数据由用户程序流向内核以下介绍一些输入输出函数,尽管都是一些有缺陷的函数,但比较适合初学者使用2、p rintf用法(其缺陷在于带缓存)解释:第一幅图没有加'\n',不会刷新缓存区,则不会打印出来;第二幅图是因为主函数结束时刷新了缓存区,但由于没有换行符,所以没有换行便显示了后面的内容;第三幅图时正常打印。

变量定义的是什么类型,在printf打印时就需要选择什么格式符,否则会造成数据的精度丢失(隐式强转),甚至会出现错误(1)格式输出函数的一般形式函数原型:int prin tf(char * format[,argume nt, …]);函数功能:按规定格式向输出设备(一般为显示器)输出数据,并返回实际输出的字符数,若出错,贝U返回负数。

A、它使用的一般形式为:printf(" 格式控制字符串",输出项列表);B、语句中"输出项列表"列出要输出的表达式(如常量、变量、运算符表达式、函数返回值等),它可以是0个、一个或多个,每个输出项之间用逗号(,)分隔; 输出的数据可以是整数、实数、字符和字符串。

C、"格式控制字符串"必须用英文的双引号括起来,它的作用是控制输出项的格式和输出一些提示信息,例如:int i=97; printf("i=%d,%c\n",i,i); 输出结果为:i=97,a语句printf("i=%d,%c\n",i,i); 中的两个输出项都是变量i ,但却以不同的格式输出,一个输出整型数97,另一个输出的却是字符a,其格式分别由"%d" 与"%c"来控制。

语句printf("i=%d,%c\n",i,i); 的格式控制字符串中"i=" 是普通字符, 他将照原样输出;"%d"与"%c"是格式控制符;"\n"是转义字符,它的作用是换行。

格式化输入输出函数(Formattedinputoutputfunction)

格式化输入输出函数(Formattedinputoutputfunction)

格式化输入输出函数(Formatted input output function)1.1.1 formatted input output functionThe Turbo C2.0 standard library provides two console formats, input and output functions, printf (), andScanf () these two functions can read and write data in a variety of formats on a standard input / output device.The printf () function is used to write data to a standard output device (screen); the scanf () function is used to enter data from a standard inputRead data on device (keyboard). The use of these two functions is described in detail below.1. Printf () functionThe printf () function is a formatted output function that is typically output to standard output devices in a specified formatInformation. This is often used when writing programs. The call format of the printf () function is:Printf (< formatted string >, < parameter table >);The formatted string consists of two parts: one is the normal character, the other is the original characterAnother part is formatting the required characters, beginningwith '%', followed by one or more specified characters,Used to determine the format of the output content.A parameter table is a series of parameters that need to be output, and the number must be the output specified by the formatted stringA number of parameters, among the parameters "," separate and sequential correspondence, otherwise there will be unexpectedErrors that cannot be reached.1. formatting specificationsThe formatting specifications provided by Turbo C2.0 are as follows:Thought of thought - thought of thoughtSymbolic action- - - - - - - - - - - - - - - -%d decimal signed integer%u decimal unsigned integer%f floating point numbers%s string%c single characterThe value of the%p pointerFloating point numbers in the%e exponent%x,%X unsigned integer represented by sixteen decimal%0 unsigned integer represented by octal notationThe%g automatically selects the appropriate representationThought of thought - thought of thoughtExplain:(1) you can insert numbers between "%" and "letters" to represent the maximum field width.For example:%3d means output 3 bit integers, not 3 bit, right justified.%9.2f represents the floating-point number of the output field width of 9, in which the decimal digit is 2 and the integer bit is 6,The decimal point is one, not 9, right justified.%8s stands for a string of 8 characters, not 8 characters, right justified.If the length or integer number of the string exceeds the indicated field width, it will be output at its actual length.But for floating point numbers, if the integer bits exceed the indicated integer bit width, they are output by actual integer;If the decimal portion exceeds the indicated decimal width, the output is output from four to five at the width of the description.In addition, if you want to add some 0 before the output value, you should add a 0 before the width of the event.For example:%04d means that when you output a value less than 4 bits, you will fill 0 in front to make the total widthFor 4.If floating-point numbers are used to represent the character or integer output format, the digits after the decimal point represent the maximum width,The number before the decimal point represents the minimum width.For example,%6.9s means a string that is no longer than 6 and not greater than 9. If greater than 9, thenAfter ninth characters, the contents will be deleted.(2) you can add a lowercase letter L between `% 'and' letter ', which means that the output is long.For example:%ld means output long integers%lf means output double floating point numbers(3) you can control the output left aligned or right aligned,i.e., add a "-" number between "%" and "letter"Indicates that the output is left justified, otherwise right justified.For example:%-7d means output 7 bit integers left justified%-10s stands for output, 10 characters left justified2. some special charactersThought of thought - thought of thoughtCharacter action- - - - - - - - - - - - - - - -\n line feed\f screen and paging\r carriage return\t Tab symbol\xhh means that a ASCII code is represented by 16 inputs,Among them, HH is 1 to 2 16 hexadecimal numbersThought of thought - thought of thoughtThe printf () function learned in this section, and the data types learned in the previous section, prepare the following procedureOrder to deepen understanding of the Turbo C2.0 data type.In 1 cases#include#includeInt, main (){Char, C, s[20], *p;Int, a=1234, *i;Float f=3.141592653589;Double x=0.12345678987654321;P= "How do you do"";Strcpy (s, Hello, Comrade);*i=12;C='\x41';Printf (a=%d\n, a); / * output decimal integer a=1234*/ Printf (a=%6d\n, a); / * the output 6 decimal number a= 1234*/ Printf (a=%06d\n, a); / * the output 6 decimal number a=001234*/Printf ("a=%2d\n", "a"); /*a more than 2 bits, output a=1234*/ at actual valuePrintf (*i=%4d\n, *i); / * output 4 decimal integer *i= 12*/Printf (*i=%-4d\n, *i); / * output left aligned 4 decimal integer *i=12*/Printf (i=%p\n, I); / * i=06E4*/ output.Printf (f=%f\n, f); / * output floating-point numberf=3.141593*/Printf (f=6.4f\n, f); / * output 6 the decimal floating-point number 4F=3.1416*/Printf (x=%lf\n, x); / * output long floating pointx=0.123457*/Printf (x=%18.16lf\n, x); / * output 18 the 16 decimal floating-point longNumber x=0.1234567898765432*/Printf (c=%c\n, c); / * output character c=A*/Printf (c=%x\n, c); / * c=41*/ the output character of the ASCII code valuePrintf (s[]=%s\n, s); / * output string array s[]=Hello, Comrade*/Printf (s[]=%6.9s\n, s); / * s[]=Hello string output of up to 9 characters,Co*/Printf (s=%p\n, s); / * the first character string array output address s=FFBE*/Printf (*p=%s\n, P); / * p=How do you do*/ output string pointerPrintf (p=%p\n, P); / * pointer output value p=0194*/Getch ();Retunr 0;}The address values in the above results may differ on different computers.In example 1., the first statement "#include" means to call another file, stdio.h, thisIs a header file that includes data type definitions and function descriptions for all standard input and output library functions.Turbo C2.0 defines and explains the variables and function types that each library function usesIn the header file "*.h", users must use #include<*.h> or #include "* H" when using these functions"The statement calls the appropriate header file for the connection. If this statement is not used, the connection will go wrongError.Two, scanf () functionThe scanf () function is a formatted input function that reads input information from a standard input device (keyboard).Its call format is:Scanf (< formatted string >, < address table >);A formatted string consists of the following three different characters;1.: format specifier format specifier and printf () function in the same format specifier.2. blank characters: blank character will make scanf (omitted) function in the read operation input in one or moreBlank character.3. non white space characters: a non blank character will make the scanf () function tick off when readThe same character as a blank character.The address table is the address of all variables that need to be read, not the variables themselves. This is associated with the printf () functionIt's quite different. Pay special attention to it. The addresses of each variable are separated from "" "" "" "" "" "".Cases of 2:{Int, I, j;Printf ("I, j=, \n");Scanf ("%d", "%d", "&i", "&j");}In this example, the scanf () function to read an integer, then the next input comma out of theThen read into another integer. If this particular character is not found, the scanf () function terminates. ifThe spaces between arguments are spaces, and one or more spaces must be entered between the arguments.Explain:(1) for strings, arrays, or string pointer variables, because the array name and pointer variable name itselfIs the address, so when you use the scanf () function, you don't need to add the '&' operator in front of them.In 3 cases{Char, *p, str[20];Scanf (%s, P); / * * / from the keyboard input stringScanf ("%s", STR);Printf (%s\n, P); / * * / string output to the screenPrintf ("%s\n", STR);}(2) you can add an integer between the "%" formatting specifications in the formatted stringThe maximum number of bits in any read operation.For example 3, if you can only enter 10 characters for string pointer P, then the first scanf () function statementForScanf ("%10s", P);When the program is running, once the number of characters is greater than 10, the P is no longer read in, while the latter one readsThe entry function, that is, scanf ("%s", "STR"), is read from the Eleventh character.There is a problem with the actual use of the scanf () function, as illustrated below:When multiple scanf () functions are used to continuously enter multiple character variables, for example:(main){Char, C1, c2;Scanf ("%c", &c1);Scanf ("%c", &c2);Printf (C1, is,%c, C2, is,%c, c2\1, C2);}Run the program, enter a character A, enter (to complete the input must return), in the implementation of scanfWhen ("%c", "&c1"), assign "A" to the variable C1, but the carriage return remains in the buffer and executes the input statementWhen scanf ("%c", "&c2"), the variable C2 output is a blank line. If you enter AB and then enter, then the output nodeThe results are: C1, is, A, C2, is, B.To solve the above problem, you can add the clearing function fflush () to the input functionThe method will be described at the end of this section. Modify the above program to become:#include(main){Char, C1, c2;Scanf ("%c", &c1);Fflush (stdin);Scanf ("%c", &c2);Printf (C1, is,%c, C2, is,%c, C1, C2);}1.1.2 non formatted input output functionThe non formatted input output function can be replaced by the Standard formatted input output function described above, butThese functions are less compiled and relatively small in memory, thus increasing the speed and using them at the same timeConvenient. The following are described separately.1. Puts () and gets () functions1. puts () functionThe puts () function is used to write strings to the standard output device (screen) and to wrap them:Puts (s);Where s is a string variable (string, array name, or string pointer).The function of the puts () function is the same as the language printf ("%s\n", "s").Cases of 4:(main){Char s[20], *f; / * string array and pointer variables.Strcpy (s, "Hello Turbo C2.0!"); / * * / string array variable assignmentF= "Thank you"; / * * / string pointer variable assignmentPuts (s);Puts (f);}Explain:(1) the puts () function can only output strings, cannot output values, or format transformations.(2) you can write strings directly to the puts () function. Such as:Puts ("Hello, Turbo, C2.0");2. gets () functionThe gets () function is used to read strings from the standard input device (keyboard) until the carriage returns, but carriage returnsDoes not belong to this string. Its call format is:Gets (s);Where s is a string variable (string, array name, or string pointer).The gets (s) function is similar to scanf ("%s", "&s"), but not exactly the same, using scanf ("%s", "&s")When a function enters a string, there is a problem that if the space is entered, the input string is considered to be over,The character after the space will be processed as the next entry, but the gets () function will accept the entire input characterString until carriage returns.In 5 cases(main){Char, s[20], *f;Printf ("What's, your, name, \n");Gets (s); / * * / wait until enter the end of the input stringPuts (s); / * * / the input string outputPuts ("How, old, are, you,...");Gets (f); Puts (f); }。

C语言的格式输入输出

C语言的格式输入输出


字符输入函数

getchar ()
无参数 函数值为从输入设备接收的字符
#include <stdio.h> int main() { char ch;
printf("Press a key and then press Enter:");
ch = getchar(); printf("You pressed "); putchar(ch); putchar('\n'); return 0;
格式输入函数
输入数据时,遇以下情况时该数据认为结束

遇空格、回车Biblioteka TAB 键 遇宽度 :%3d 遇非法输入
scanf格式字符
d 以带符号十进制形式输入整型数据
o x c s
f e
以八进制无符号形式输入(无前导0) 以十六进制无符号形式输入(无前导0x) 以字符形式输入单个字符 输入字符串,以非空字符开始,遇第一个 空白字符结束 以小数形式输入浮点数 以标准指数形式输入
printf("printf WITH width and precision specifications:\n"); printf("%5.2f\n%6.1f\n%3.0f\n", f1, f2, f3); printf("%5d\n%6d\n%3d\n", n1, n2, n3);}
格式输入函数
scanf附加格式说明符
l
L h m *
加在d、o、x、u前:输入长整型 加在f、e 前:输入双精度型 加在f、e 前:输入long double型 加在d、o、x 前:输入短整型 表示数据占用的宽度 本输入项在读入后不赋给相应的变量

格式输入输出函数

格式输入输出函数
则左端补以空格,若大于m,则按实际位数输出。 例: printf(″%4d,%4d″,a,b);
若a=123,d=12345,则输出结果为 □123,12345 ③ %ld:输出长整型数据。 例: long a=135790;/* 定义a为长整型变量*/ printf(″%ld″,a);
(2)f格式符。用来以小数形式输出实数(包括单双精度)
右侧,左补空格。 ⑤ %-m.ns,n个字符输出在m列的左侧,右补空格,若n〉m,m
动取n值。
(4)s格式符 输出字符串.
① %s。例如: printf(″%s″,″CHINA″) 输出字符串“CHINA”(不包括双引号)。
② %ms,输出的字符串占m列,若串长大于m,则全部输出,若串 小于m,则左补空格。
③ %-ms,若串长小于m,字符串向左靠,右补空格。 ④ %m.ns,输出占m列,只取字符串中左端n个字符,输出在m列的
一、格式输入函数scanf
例1 输入一个十进制整数并输出。 #include<stdio.h> main() {
int a; scanf(“%d”,&a); printf(“a的值是:%d”,a); }
• 函数作用:按照变量在内存的地址将变量值存进 去。
• 一般格式:scanf(“格式控制”,地址表列)
%d::输入带符号的十进制整数; %o:输入无符号的八进制整数; %x:输入无符号的十六进制整数; %f:输入实数; %e:与f作用相同,可以相互替换; %c:输入单个字符; %s:输入字符串。
是由若干个地址组成 的表列,可以是变量 的地址,或字符串的
首地址
例2 用scanf函数输入数字据符。
#include<stdio.h>
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

二、格式输出函数printf
• 函数作用:向终端(或系统隐含指定的输出设备) 输出若干个任意类型的数据。 • 一般格式:printf(”格式字符串”,[输出项表]) 同scanf函数
格式指示符 转义字符 普通字符
(1) d格式符。用来输出十进制整数。
几种用法: ① %d:按十进制整型数据的实际长度输出。 ② %md:m为指定的输出字段的宽度。如果数据的位数小于m, 则左端补以空格,若大于m,则按实际位数输出。 例: printf(″%4d,%4d″,a,b); 若a=123,d=12345,则输出结果为 □123,12345 ③ %ld:输出长整型数据。 例: long a=135790;/* 定义a为长整型变量*/ printf(″%ld″,a);
(4)s格式符
输出字符串.
① %s。例如: printf(″%s″,″CHINA″) 输出字符串“CHINA”(不包括双引号)。 ② %ms,输出的字符串占m列,若串长大于m,则全部输出,若串长 小于m,则左补空格。 ③ %-ms,若串长小于m,字符串向左靠,右补空格。 ④ %m.ns,输出占m列,只取字符串中左端n个字符,输出在m列的 右侧,左补空格。 ⑤ %-m.ns,n个字符输出在m列的左侧,右补空格,若n〉m,m自 动取n值。
使用scanf函数时应注意的问题 : (1)scanf函数中的“格式控制”后面应当是变量地址,而不应 是变量名。 (2) 如果在“格式控制”字符串中除了格式说明以外还有其他字符, 则在输入数据时在对应位置应输入与这些字符相同的字符。 (3) 在用“%c”格式输入字符时,空格字符和“转义字符”都作为 有效字符输入 (4) 在输入数据时,遇以下情况时认为该数据结束。 ① 遇空格,或按“回车”或“跳格”(Tab)键; ② 按指定的宽度结束,如“%3d”,只取3列; ③ 遇非法输入。
• 函数作用:按照变量在内存的地址将变量值存进 去。 • 一般格式:scanf(“格式控制”,地址表列) %d::输入带符号的十进制整数; %o:输入无符号的八进制整数; %x:输入无符号的十六进制整数; %f:输入实数; %e:与f作用相同,可以相互替换; %c:输入单个字符; %s:输入字符串。
③%-m.nf与%m.nf基本相同,只是使输出的数值向左端 靠,右端补空格。
(3)c格式符,用来输出一个字符。 如:char d=′a′; printf(″%c″,d); 输出字符′a′. 一个整数,只要它的值在0~255范围内,可以用 “%c”使之按字符形式输出,在输出前,系统会将该整数 作为ASCII码转换成相应的字符;一个字符数据也可以用 整数形式输出。
3.2(一)格式输入函数
一般格式:scanf(“格式控制”,地址列表) ; 格式控制符:%d,%f,%c……
一、格式输入函数scanf
例1 输入一个十进制整数并输出。 #include<stdio.h> main() { int a; scanf(“%d”,&a); printf(“a的值是:%d”,a); }
是由若干个地址组成 的表列,可以是变量 的地址,或字符串 字符 a在内存中的地 #include<stdio.h> 址 main() &是地址运算符 { int char a,b,c; scanf(“%d,%d,%d”,&a,&b,&c); %c%c%c printf(“%d,%d,%d\n”,a,b,c); %c%c%c }
(2)f格式符。用来以小数形式输出实数(包括单双精度)
有以下几种用法: ① %f。不指定字段宽度,由系统自动指定字段宽度,使整数 部分全部输出,并输出6位小数。应当注意,在输出的数字中 并非全部数字都是有效数字。单精度实数的有效位数一般为7位。 ②%m.nf。指定输出的数据共占m列,其中有n位小数。如果 数值长度小于m,则左端补空格。
相关文档
最新文档