scanf在c语言中的用法(实例运行scanf函数)
C语言中使用printf()可以打印输出结果,那读取输入结果使用什么?使用scanf(),它可以读取不同的输入结果。scanf()将输入的字符串转换为整数、浮点数、字符或字符串。
scanf()使用
scanf()使用格式字符串和参数列表,但scanf和printf的区别在于参数列表中,scanf()使用指针,printf()使用变量,常量,表达式。如何使用scanf(),看如下示例
#include<stdio.h>intmain(void){ intnum; floatvalue; printf("请输入num和value:n"); scanf("%d %f",&num,&value); printf("num=%d,value=%.2f",num,value); return0; }
运行结果
请输入num和value:
199 201
num=199,value=201.00
使用scanf()读取基本变量类型的值时,需要在变量之前加&。
scanf()函数使用空白把输入分成多个字段,在匹配转换说明和字符时跳过空格。