zoukankan      html  css  js  c++  java
  • sizeof 和 strlen 的printf 问题

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main(void){
     5     char temp[3];
     6     scanf("%s", temp);
     7     printf("%d
    ", sizeof(temp));
     8     printf("%d
    ", strlen(temp));
     9     printf("%s
    ", temp);
    10 }

    编译时出错:

    test_scanf.c: In function ‘main’:
    test_scanf.c:7:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
      printf("%d
    ", sizeof(temp));
      ^
    test_scanf.c:8:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat=]
      printf("%d
    ", strlen(temp));
      ^

    sizeof() 和 strlen() 返回值类型为size_t 

     In C99's *printf, z is a length modifier like h or l, not a conversion specifier like d or u. 

    将代码改为:

    printf("%zu
    ", sizeof(temp));
    printf("%zu
    ", strlen(temp));

     另外:

    strlen 计算到 出现为止。

    sizeof 保证能容纳实现所建立的最大对象的字节大小。

  • 相关阅读:
    Mybatis懒加载
    Mybatis 动态SQL
    Mybatis的多表查询
    linux selinux
    linux find/vi复制粘贴
    01-oracle限定查询-20190404
    awk
    sed
    windows删除指定日期前的文件
    win10 sshsecureshellclient删除profile保存的信息
  • 原文地址:https://www.cnblogs.com/iowl/p/4049955.html
Copyright © 2011-2022 走看看