C语言拾遗(一)

一:(2012年03月19日) strlen 与sizeof

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

int main(void)
{
    char str1[9] = "hello";
    char str2[]  = "hello";
    char *str3  = "hello";
    char *str4 = "";

    printf("strlen(str1)        is      %u\n", strlen(str1));
    printf("sizeof(str1)        is      %u\n", sizeof(str1));
    printf("strlen(str2)        is      %u\n", strlen(str2));
    printf("sizeof(str2)        is      %u\n", sizeof(str2));
    printf("strlen(str3)        is      %u\n", strlen(str3));
    printf("sizeof(str3)        is      %u\n", sizeof(str3));
    printf("strlen(str4)        is      %u\n", strlen(str4));
    printf("sizeof(str4)        is      %u\n", sizeof(str4));
    printf("sizeof(\"\")        is      %u\n", sizeof(""));
    printf("sizeof(\"hello\")   is      %u\n", sizeof("hello"));

    return 0;
}

运行结果:(程序在64位系统下运行)

strlen(str1)        is      5
sizeof(str1)        is      9
strlen(str2)        is      5
sizeof(str2)        is      6
strlen(str3)        is      5
sizeof(str3)        is      8
strlen(str4)        is      0
sizeof(str4)        is      8
sizeof("")        is      1
sizeof("hello")   is      6


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值