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 保证能容纳实现所建立的最大对象的字节大小。

  • 相关阅读:
    Python import模块
    Python 内置函数
    Python Pickle序列化
    android xml布局文件属性说明
    android 中动画
    Android样式——Styles
    代码家
    Android UI目录
    Android 基本控件
    android and webview 网页应用
  • 原文地址:https://www.cnblogs.com/iowl/p/4049955.html
Copyright © 2011-2022 走看看