Bool, sizeof

Source: Internet
Author: User

Before using bool, declare # include <stdbool. h> (this header file defines macros such as bool, true, and false) int A [5]; sizeof (A [5]), sizeof is a keyword, and the evaluation value is during compilation, sizeof (A [5]) does not report an error. This value does not exist, but it does not actually access the value of a [5, instead, the value is determined only based on the type of the array element. Sizeof (variable \ constant) sizeof variable \ constant sizeof (data type) sizeof data type -- Error * sizeof

Sizeof is one of the C operators used to obtain the memory space allocated to the operands, expressed in bytes.

The operands can be variables or data types, such as int and float. Therefore, you can use them to obtain the range of basic types defined by the local C library.

 

* Use of sizeof

1. For general variables, there are two forms: sizeof A or sizeof ();

2. For data types, brackets must be used, such as sizeof (INT ).

 

* Description of size_t

The return value of sizeof must be an unsigned integer. In Standard C, the return value type is defined as size_t (namely, unsigned int, defined in stddef. h In the header file). strlen also returns an unsigned integer. If the size_t type is output using printf, the format character % ZD is defined in c99; if the compiler does not support % u or % lu/% ZD.

 

* Comparison with strlen

Sizeof: obtains the number of bytes in the memory space occupied by the operands. The return type is size_t;

Strlen: obtains the actual number of bytes used by the character array. It does not contain the array Terminator '\ 0' and returns the type size_t;

As shown in:

# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Int A = 1;
Char B [4] = "AAA ";
Printf ("% ZD % lu \ n", sizeof A, sizeof (INT), sizeof );
Printf ("% ZD \ n", sizeof B, strlen (B ));
Return 0;
}

Result:

4 4 4
4 3

 

 

Bool, sizeof

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.