• Linux C 字符串的sizeof与strlen的区别


    测试Case:

    1. #include
    2. #include
    3. void test_sizeof_and_strlen_for_linux_c()
    4. {
    5. char a[10] = "abc";
    6. printf("sizeof(a):%ld strlen(a):%ld\n", sizeof(a), strlen(a));
    7. char b[10] = {'a', 'b', 'c'};
    8. printf("sizeof(b):%ld strlen(b):%ld\n", sizeof(b), strlen(b));
    9. char c[10] = {'a', 'b', 'c', '0'};
    10. printf("sizeof(c):%ld strlen(c):%ld\n", sizeof(c), strlen(c));
    11. char d[10] = {'a', 'b', 'c', '\0'};
    12. printf("sizeof(d):%ld strlen(d):%ld\n", sizeof(d), strlen(d));
    13. char e[10] = {'a', 'b', 'c', 0, 'd'};
    14. printf("sizeof(e):%ld strlen(e):%ld\n", sizeof(e), strlen(e));
    15. char f[10] = "";
    16. printf("sizeof(f):%ld strlen(f):%ld\n", sizeof(f), strlen(f));
    17. char g[10] = {};
    18. printf("sizeof(g):%ld strlen(g):%ld\n", sizeof(g), strlen(g));
    19. char h[10];
    20. printf("sizeof(h):%ld strlen(h):%ld\n", sizeof(h), strlen(h));
    21. char i[] = {};
    22. printf("sizeof(i):%ld strlen(i):%ld\n", sizeof(i), strlen(i));
    23. char j[] = "";
    24. printf("sizeof(j):%ld strlen(j):%ld\n", sizeof(j), strlen(j));
    25. char k[] = "abc";
    26. printf("sizeof(k):%ld strlen(k):%ld\n", sizeof(k), strlen(k));
    27. char l[] = "abc0";
    28. printf("sizeof(l):%ld strlen(l):%ld\n", sizeof(l), strlen(l));
    29. char m[] = "abc\0";
    30. printf("sizeof(m):%ld strlen(m):%ld\n", sizeof(m), strlen(m));
    31. char n[10] = "abc";
    32. n[5] = '\0';
    33. printf("sizeof(n):%ld strlen(n):%ld\n", sizeof(n), strlen(n));
    34. char o[10] = "abc";
    35. o[4] = 'e',
    36. o[5] = '\0';
    37. printf("sizeof(o):%ld strlen(o):%ld\n", sizeof(o), strlen(o));
    38. char p[10] = "abc";
    39. p[3] = 'e',
    40. p[5] = '\0';
    41. printf("sizeof(p):%ld strlen(p):%ld\n", sizeof(p), strlen(p));
    42. char q[10] = "\0abc";
    43. printf("sizeof(q):%ld strlen(q):%ld\n", sizeof(q), strlen(q));
    44. char r[] = "\0abc";
    45. printf("sizeof(r):%ld strlen(r):%ld\n", sizeof(r), strlen(r));
    46. char s[10] = "abcdefghijklm";
    47. printf("sizeof(s):%ld strlen(s):%ld\n", sizeof(s), strlen(s));
    48. }
    49. int main()
    50. {
    51. test_sizeof_and_strlen_for_linux_c();
    52. return 0;
    53. }

    测试结果:

    1. :~/proj/test/test$ ./a.out
    2. sizeof(a):10 strlen(a):3
    3. sizeof(b):10 strlen(b):3
    4. sizeof(c):10 strlen(c):4
    5. sizeof(d):10 strlen(d):3
    6. sizeof(e):10 strlen(e):3
    7. sizeof(f):10 strlen(f):0
    8. sizeof(g):10 strlen(g):0
    9. sizeof(h):10 strlen(h):0
    10. sizeof(i):0 strlen(i):0
    11. sizeof(j):1 strlen(j):0
    12. sizeof(k):4 strlen(k):3
    13. sizeof(l):5 strlen(l):4
    14. sizeof(m):5 strlen(m):3
    15. sizeof(n):10 strlen(n):3
    16. sizeof(o):10 strlen(o):3
    17. sizeof(p):10 strlen(p):4
    18. sizeof(q):10 strlen(q):0
    19. sizeof(r):5 strlen(r):0
    20. sizeof(s):10 strlen(s):10

  • 相关阅读:
    电子商务的三个要素是人、货、场
    Git 中的 HEAD
    React实现在线预览word报告/本地选择报告预览
    安卓百度地图之步行路径规划
    SpringBoot进阶教程(七十七)WebSocket
    mysql中的分页查询的优化方案
    关于python序列对象
    38基于matlab的期货预测,利用PSO优化SVM和未优化的SVM进行对比,得到实际输出和期望输出结果。
    基于JavaEE和SSH框架的网络阅卷任务分发系统
    树莓派65/100 - Pico W初体验,点亮板载的LED灯
  • 原文地址:https://blog.csdn.net/swif_N_F/article/details/127623422