第四章 其他

sizeof和strlen()区别sizeof以字节为单位给出数据的大小,strlen()函数以字符为单位给出字符串的长度。使用strlen()函数要加#include<string>头文件。Sizeof计算字符时会将标志字符串结束的不可见的空字符计算在内。

定义符号常量

方法一:#define NAME value

优点:一个名字比数字告诉的信息更多;

     若要改变常量的值,只需改变符号常量的定义。

用法:1,培养用大写常量的习惯;

      2,可用于定义字符和字符串常量。

方法二:const NAME=value

*修饰符

/*29.c--使用可变宽度的输出字段*/

#include<stdio.h>

int main()

{

  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);

  return 0;

}

运行结果:What field width?

6

The number is:   256:

Now enter a width and a precision:

8 3

Weight = 242.500

如果事先不想指定字段宽度,可用*代替数字,不过要用参数来告诉函数的字段宽度。

/*30.c--跳过输入的头两个数*/

#include<stdio.h>

int main()

{

   int n;

     printf("Please enter three integers:\n");

     scanf("%*d %*d %d",&n);

     printf("The last integer was %d\n",n);

return 0;

}

运行结果:Please enter three integers:

1234 767 234

The last integer was 234

在函数scanf()中,当*放在%和说明符字母之间时,它使函数跳过相应的输入项目。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值