The use of sizeof operator in C language and the difference between strlen and _c language

Source: Internet
Author: User

First, the definition:

sizeof is an operator (operator) in C/s + +, which simply functions by returning an object or a type's memory byte count. Its return value type is size_t and is defined in the header file stddef.h. In a 32-bit system:

The sizeof value of char is 1,char is the smallest data type we can use for programming.

The sizeof value of short is 2;
int, float, long is 4;
Double is 8;
All pointers have a sizeof value of 4.

Second, the grammar:

There are three grammatical forms of sizeof, as follows:

1) sizeof (object); sizeof (object);
2) sizeof (TYPE_NAME); sizeof (type);
) sizeof object; sizeof object;

sizeof (2); 2 is of type int, so it is equivalent to sizeof (int);
sizeof (2 +3.14); The type of 3.14 is double,2 also promoted to double, so it is equivalent to sizeof (double);

Third, the example explains:

char* ss = "0123456789";
sizeof (ss) Result 4 = = "SS is a character pointer to a string constant
sizeof (*SS) result 1 = =" *ss is the first character
char ss[] = "0123456789";
sizeof (ss) Result 11 = = SS is an array, computed to the position of the 10+1, so is the
sizeof (*SS) result 1 = = "*ss is the first character
char ss[100] =" 0123456789 ";
sizeof (ss) result is 100 = = "The SS represents the size in memory 100x1
strlen (ss) result is 10 = =" Strlen is a function the internal implementation is to be computed by a loop before the
int ss[100] = " 0123456789 ";
sizeof (ss) Result 400 = = "SS" indicates the size of the 100x4
strlen (ss) Error = = "Strlen parameter can only be char* and must be the" "" "at the end of
char q[]=" ABC "; C13/>char p[]= "a\n";
sizeof (q), sizeof (p), strlen (q), strlen (p);
The result is 4 3 3 2.  


iv. the difference between sizeof and strlen:

1.sizeof is the operator, strlen is a function. The array is passed as a parameter to the function newsletters the pointer rather than the array, the first address of the array, such as: Fun (char [8]), Fun (char []), is equivalent to Fun (char *), so the parameters of the array sizeof are not degraded, and the strlen is degraded to the pointer.

2.sizeof can be used to make parameters, strlen can only be used char* parameters, and must be "" "end.  sizeof can also use functions to do parameters, such as: short f (); printf ("%d\n", sizeof (f ())); The result of the output is sizeof (short), that is, 2.

3. Most compilers have computed sizeof the type or length of the variable at compile time, which is why sizeof (x) can be used to define the array dimension. The results of the strlen are computed at run time, and are used to compute the length of the string, not the type that is the size of the memory.

Char str[20]= "0123456789";
int A=strlen (str); a=10;
int b=sizeof (str); and b=20;


4. When applied to a struct type or variable, the sizeof returns the actual size, and when applied to a static space array, sizeof the dimensions of all the arrays. The sizeof operator cannot return the dimensions of an array or an external array that is dynamically assigned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.