关于scanf函数和printf函数的用法与区别(菜鸟级提问!!)

请仔细讲解有关两种函数在C语言中的使用方法以及区别

1.区别:scanf与printf函数一样,都被定义在头文件stdio.h里,因此在使用scanf函数时要加上#include <stdio.h>。它是格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量之中。而printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息。

2.scanf用法:int scanf(const char *format,...);

例子:

#include<stdio.h>
int main(void)
{
int a,b,c;
printf("输入a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
printf("a=%d,b=%d,c=%d\n",a,b,c);
fflush(stdin);
return 0;
}

3.printf用法:extern void printf(const char *format,...);

例子:

#include<stdio.h>
int main()
{
int a=1;
float b=1.0;
char str[12]="Hello World";
printf("This is an example of printf:\n");
printf("a is %d,b is %f,and a+b=%f",a,b,a+b);
printf("I want to say,%s",str);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-15
scanf("%d",&num);
printf("%d",num);
因为scanf and printf是格式化输出,所以分两个部分,第一部分用分号扩起,%加格式,%d代表整数十进制,后面是相关数据,但scanf 将取得数据 放到num地址 所以加取地址符号&
而printf使用是 直接跟想输出的数的变量名字num

#include <stdio.h>

void main( void )
{
int i, result;
float fp;
char c, s[81];
wchar_t wc, ws[81];

printf( "\n\nEnter an int, a float, two chars and two strings\n");

result = scanf( "%d %f %c %C %s %S", &i, &fp, &c, &wc, s, ws );
printf( "\nThe number of fields input is %d\n", result );
printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws);

wprintf( L"\n\nEnter an int, a float, two chars and two strings\n");

result = wscanf( L"%d %f %hc %lc %S %ls", &i, &fp, &c, &wc, s, ws );
wprintf( L"\nThe number of fields input is %d\n", result );
wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp, c, wc, s, ws);
}

输出

Enter an int, a float, two chars and two strings
71
98.6
h
z
Byte characters

The number of fields input is 6
The contents are: 71 98.599998 h z Byte characters

Enter an int, a float, two chars and two strings
36
92.3
y
n
Wide characters

The number of fields input is 6
The contents are: 456 92.300003 y n Wide characters
第2个回答  推荐于2017-10-15
函数名: printf
功 能: 产生格式化输出的函数
用 法: int printf(char *format...);
程序例:

#include <stdio.h>
#include <string.h>

#define I 555
#define R 5.5

int main(void)
{
int i,j,k,l;
char buf[7];
char *prefix = buf;
char tp[20];
printf("prefix 6d 6o 8x 10.2e "
"10.2f\n");
strcpy(prefix,"%");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
for (k = 0; k < 2; k++)
for (l = 0; l < 2; l++)
{
if (i==0) strcat(prefix,"-");
if (j==0) strcat(prefix,"+");
if (k==0) strcat(prefix,"#");
if (l==0) strcat(prefix,"0");
printf("%5s |",prefix);
strcpy(tp,prefix);
strcat(tp,"6d |");
printf(tp,I);
strcpy(tp,"");
strcpy(tp,prefix);
strcat(tp,"6o |");
printf(tp,I);
strcpy(tp,"");
strcpy(tp,prefix);
strcat(tp,"8x |");
printf(tp,I);
strcpy(tp,"");
strcpy(tp,prefix);
strcat(tp,"10.2e |");
printf(tp,R);
strcpy(tp,prefix);
strcat(tp,"10.2f |");
printf(tp,R);
printf(" \n");
strcpy(prefix,"%");
}
}
return 0;
}

函数名: scanf
功 能: 执行格式化输入
用 法: int scanf(char *format[,argument,...]);
程序例:

#include <stdio.h>
#include <conio.h>

int main(void)
{
char label[20];
char name[20];
int entries = 0;
int loop, age;
double salary;

struct Entry_struct
{
char name[20];
int age;
float salary;
} entry[20];

/* Input a label as a string of characters restricting to 20 characters */
printf("\n\nPlease enter a label for the chart: ");
scanf("%20s", label);
fflush(stdin); /* flush the input stream in case of bad input */

/* Input number of entries as an integer */
printf("How many entries will there be? (less than 20) ");
scanf("%d", &entries);
fflush(stdin); /* flush the input stream in case of bad input */

/* input a name restricting input to only letters upper or lower case */
for (loop=0;loop<entries;++loop)
{
printf("Entry %d\n", loop);
printf(" Name : ");
scanf("%[A-Za-z]", entry[loop].name);
fflush(stdin); /* flush the input stream in case of bad input */

/* input an age as an integer */
printf(" Age : ");
scanf("%d", &entry[loop].age);
fflush(stdin); /* flush the input stream in case of bad input */

/* input a salary as a float */
printf(" Salary : ");
scanf("%f", &entry[loop].salary);
fflush(stdin); /* flush the input stream in case of bad input */
}

/* Input a name, age and salary as a string, integer, and double */
printf("\nPlease enter your name, age and salary\n");
scanf("%20s %d %lf", name, &age, &salary);

/* Print out the data that was input */
printf("\n\nTable %s\n",label);
printf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
printf("-----------------------------------------------------\n");
for (loop=0;loop<entries;++loop)
printf("%4d | %-20s | %5d | %15.2lf\n",
loop + 1,
entry[loop].name,
entry[loop].age,
entry[loop].salary);
printf("-----------------------------------------------------\n");
return 0;
}本回答被提问者采纳
第3个回答  2012-03-15
...printf是输出,scanf用来接用户输入,通过printf输出
第4个回答  2019-03-12
scanf
和printf函数都是包含在
stdio.h的头文件里的

TC里面可以不用写
#include<stdio.h>
而在VC里面必须要写
相似回答