sizeof operators and size_t types

Source: Internet
Author: User
Tags strlen
sizeof operators and size_t types

The role of *sizeof

sizeof is one of the operators of C, which is used to get the amount of memory space that the operand is allocated in byte units.

The operands here can be variables or data types such as int,float. So it's possible to get a range of the basic types defined by the local C library.

The use of *sizeof

1. For general variables, form 2 kinds: sizeof A or sizeof (a);

2. For data types, you must use parenthesized methods, such as sizeof (int).

Description of *size_t

SIZE_T is defined in the standard C library and should be unsigned int, which is long unsigned int in a 64-bit system.

sizeof returns must be an unsigned shape, in standard C the return value type is defined as size_t by a typedef.

If you output the size_t type with printf, the format character%zd is defined in C99, and you can try%u or%lu if the compiler does not support it.

* Comparison with Strlen

sizeof, gets the number of bytes of memory space occupied by the operand, and returns the type size_t;

Strlen, gets the number of bytes actually used by the character array, does not contain the end of the array ' and ', the return type size_t;

As shown:

#include <stdio.h>
#include <string.h>
int main (void)
{
int a = 1;
Char b[4] = "AAA";
printf ("%zd%zd%lu\n", sizeof a,sizeof (int), sizeof a);
printf ("%zd%zd\n", sizeof B,strlen (b));
return 0;
}

Results:

4 4 4
4 3

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.