c语言常用语法1


1、const 和 define 的最大不同是: define在编译时只进行字符的替换, 而const 则定义了一个变量, 并且它的值是固定的, 可以得到这个变量的地址.,如果仅仅定义常量,两者并没有本质的区别,但是const还可修饰函数形式参数、返回值和类的成员函数等。从而提高函数的健壮性。因为const修饰的东西能受到c/c++的静态类型安全检查机制的强制保护,防止意外的修改。

2、char *gets(char *str)

str -- This is the pointer to an array of chars where the C string is stored.

This function returns str on success, and NULL on error or when end of file occurs while no characters have been read.     Null is defined 0

测试用例无限输入,每个测试用例占用一行

char str[N] ;
while(gets(str))
{
        int n = strlen(str) ;
        if(0==n)    //空行跳过
        {
              printf("\n") ;  
              continue ;
        }
}


3、void * memset ( void * ptr, int value, size_t num );

#include <string.h>

Sets the first num bytes of the block of memory pointed by ptr to the specified value 

4、测试用例以0结束

double d ;
while(1==scanf("%lf",&d)&&d)
{
}
或者

while (true)
{
    double d ;
    scanf("%lf",&d) ;
    if(d==0.0) break ;
}

5、printf格式字符串

  1. long long定义方式可以用于gcc/g++,不受平台限制,但不能用于VC6.0。
  2. __int64是Win32平台编译器64位长整型的定义方式,不能用于Linux。
  3. "%lld"用于Linux i386平台编译器,"%I64d"用于Win32平台编译器。
  4. cout只能用于C++编译,在VC6.0中,cout不支持64位长整型
  5. long long一般是Linux下申明方式,_int64一般是Windows下使用64位的申明方式

long long a[n] ;
double d ;
printf(“%6.2lf%17I64d\n”,d,a[n]) //d小数部分两位,向右对齐,宽度为6,数组也向右对齐,宽度为17
向左对齐,右边填空格,只需要加-
printf("%-6.2lf\n",5.2) ;
向右对齐,左边以前导0填充,只需要加0
printf("%06.2lf\n",5.2) ;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值