This is the 10th day of my participation in the August Text Challenge.More challenges in August

directory

  • Sizeof is different from strlen
    • 1. Get the string length – for strings
    • 2. Get pointer/array length – for pointer/array
    • 3. Sizeof Obtains the memory size
    • 4. Classic cases
  • Two. Guess you like it

C/C++ learning Directory >> C language basics

Sizeof is different from strlen

In C, strlen and sizeof both take the length of a string, but what’s the difference between them?

1. Get the string length – for strings

sizeofThe length obtained by the number of functions is the length of the entire memory size, and the length returned includes'\ 0';strlenFunction to obtain the length of'\ 0'At the end, the returned length does not include'\ 0';

A. Get the normal string length – strlen and sizeof are the same

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @data :C language tutorial - C language sizeof and strlen function difference // @timingfunction: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ char p[4] = "abc"; Printf (" string: %s sizeof length: %d\n", p, sizeof(p)); Printf (" string: %s strlen length: %d\n", p, strlen(p)); String: ABC sizeof Length: 4 String: ABC strlen Length: 3 */Copy the code

sizeofIt’s the length of the string in memory, so it’s plus the last'\ 0'So in generalsizeofThe length of the function is going to be greater thanstrlenfunctionThe length of theta is 1 more.

B. Get unconventional string length – useStrlen and sizeof are different

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @data :C language tutorial - C language sizeof and strlen function difference // @timingfunction: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ char str[1char str[100] = {0}; strcpy(str, "abcd"); int str_len = strlen(str); int str_size = sizeof(str); printf("strlen(str) = %d\n", Printf ("sizeof(STR) = %d\n", (str_size)); strlen(STR) = 4 sizeof(STR) = 100 */Copy the code

This example illustrates: Strlen evaluates the string to'\ 0'The size of the position,sizeofThe amount of memory that the string takes up, this is alsostrlenFunctions and[sizeof](https://www.codersrc.com/archives/7851.html)Differences;

2. Get pointer/array length – for pointer/array

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @data :C language tutorial - C language sizeof and strlen function difference // @timingfunction: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ char p1[] = "abcdefg"; // array printf(" string: %s sizeof length: %d\n", p1, sizeof(p1)); Printf (" string: %s strlen length: %d\n", p1, strlen(p1)); printf("----------------------------\n"); char* p2 = "abcdefg"; Printf (" string: %s sizeof length: %d\n", p2, strlen(p2)); Printf (" string: %s strlen Length: %d\n", p2, sizeof(p2)); /* Output: string: abcdefg sizeof Length: 8 String: abcdefg strlen Length: 7 ---------------------------- String: abcdefg sizeof length: 7 String: abcdefg sizeof length: 7 Abcdefg strlen Length: 4 */Copy the code

Note that Pointers always take up 4 bytes of memory;

3. Sizeof Obtains the memory size

The strlen function can only calculate the length of a string. Sizeof can measure the sizeof an int, float, bool, char, etc.

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @data :C language tutorial - C language sizeof and strlen function difference // @timingfunction: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / printf("int=%d\nshort=%d\ndouble=%d\n", sizeof(int), sizeof(short), sizeof(double)); /* Output: int=4 short=2 double=8 Please press any key to continueCopy the code

4. Classic cases

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @data :C language tutorial - C language sizeof and strlen function difference // @timingfunction: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ #include "stdafx.h" #include<stdlib.h> #include<stdio.h> void main() { int arr[] = { 1, 2, 3 }; printf("sizeof(arr) : %d\n",sizeof(arr)); for (int i = 0; i < sizeof(arr); i++){ printf("%d,", arr[i]); } printf("\n"); system("pause"); } /* sizeof(arr) : 1, 2, 3, 12-858993460969168117762-5,1,13017304,13018680,123799719,11735310,11735310, press any key to continue... * /Copy the code

Isn’t that confusing? In addition to printing 1,2,3, it’s printing random numbers, but it’s printing 12. Because the memory of the array is allocated dynamically, everything after element 3 is newly allocated, not necessarily an empty, random number.

Because the array is a continuous space, 3 possible elements of space is a data, the C language will read out, is of course some meaningless random Numbers, but you don’t want to go, otherwise may cause data disorder, so it’s possible you run several times, the value will not change. The correct version is as follows:

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @data :C language tutorial - C language sizeof and strlen function difference // @timingfunction: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ int arr[] = { 1, 2, 3 }; printf("sizeof(arr)/sizeof(int) : %d\n", sizeof(arr) / sizeof(int)); for (int i = 0; i < sizeof(arr) / sizeof(int); i++){ printf("%d,", arr[i]); } /* output: sizeof(arr)/sizeof(int) : 3 1,2,3, press any key to continue.. */Copy the code

Two. Guess you like it

  1. Install Visual Studio
  2. Install the Visual Studio plug-in, Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 Uninstall
  5. Set the Visual Studio font/background/line number
  6. C format controller/placeholder
  7. C language logic operator
  8. C language ternary operator
  9. C language comma expression
  10. C (++ I/I ++)
  11. C language for loop
  12. C language break and continue
  13. C while loop
  14. C does while and while loops
  15. C switch statement
  16. C language GOto statement
  17. C char Character string
  18. C strlen function
  19. C sizeof
  20. C sizeof and strlen are different

C language sizeof and strlen function difference

[Like (3)](javascript:😉 [tip](javascript:😉

This article is published by the blog – Ape Say Programming Ape Say programming!