C语言拾遗(一)

来源:互联网 发布:买家如何开通淘宝客 编辑:程序博客网 时间:2024/05/16 17:01

一:(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      5sizeof(str1)        is      9strlen(str2)        is      5sizeof(str2)        is      6strlen(str3)        is      5sizeof(str3)        is      8strlen(str4)        is      0sizeof(str4)        is      8sizeof("")        is      1sizeof("hello")   is      6


原创粉丝点击