首页 > 代码库 > scanf()用法汇总

scanf()用法汇总

//***什么情况下使用  &
#include<stdio.h>
int main()
{
 printf("\n\n\t//***什么情况下使用  & \n\n");
 int age;
 float assets;
 char pet[30];
 
 printf("Enter the age ,assets,and favorite pet . \n");
 scanf("%d %f",&age,&assets);/*基本变量名前加 & */
 scanf("%s",pet);// 对于 char 数组不适用 &
    printf("%d $%.2f %s\n",age,assets,pet);
   
    /*使用可变宽度的输出字段*/
   
    printf("\n\n\t/*使用可变宽度的输出字段*/ \n\n");
   
    unsigned width,precision;
 int number = 256;
 double weight = 242.5;
 
 printf("what field width ?\n");
 scanf("%d",&width);
 printf("The number is : %*d:\n",width,number);
 printf("Now enter a width and a precision : \n");
 scanf("%d %d",&width,&precision);
 printf("weight = %*.*f\n",width,precision,weight);
 
 /*跳过输入的头两个整数*/
 printf("\n\n\t/*跳过输入的头两个整数*/\n\n");
 int n;
 printf("Enter three integers : \n");
 scanf("%*d %*d %d",&n);//跳过两个整数,并把第三个整数复制给 n
 printf("The last integer was %d\n",n);
 
   
    return 0;
 
}

scanf()用法汇总