A summary of the sizeof C language

Source: Internet
Author: User
Tags microsoft c

First, the concept of sizeof

Sizeof is A single-eye operator of C, such as other C - language operators + +, and so on. It is not a function. the Sizeof operator gives the storage size of its operands in bytes. The operand can be an expression or a type name enclosed in parentheses. The storage size of the operand is determined by the type of the operand.

Second, The use of sizeof method

1. For data type

sizeof use form:sizeof (type)

The data type must be enclosed in parentheses, such as sizeof (int).

2. For variables

sizeof use form:sizeof (var_name) or sizeof Var_name

Variable names can be enclosed without parentheses. such as sizeof (var_name),sizeof var_name , etc. are the correct form. The use of parentheses is more common, and most programmers take this form.

Note: sizeof operator cannot be used for function type, incomplete type, or bit field. An incomplete type is a data type with an unknown storage size, such as an array type of unknown storage size, a struct or union type of unknown content, a void . Span style= "font-family: the song Body;" > type and so on.

such as sizeof (max), the variable max is defined as int max ();sizeof (CHAR_V) , CHA_VR defined as Char Char_v[max] and MAX unknown; sizeof (void) and so on, these are not the right forms.

Iii. Results of sizeof

The result type of the Sizeof operator is size_t, which is a typedef of type unsigned int in the header file . This type guarantees that the byte size of the largest object being built can be accommodated. (This refers to the System)

1. If the operand has a type char,unsigned char , or signed char, the result is equal to 1 .

ANSI C formally stipulates that the character type is 1 bytes, andsizeof (char) =1.

2,int,unsigned int,Short int,unsigned short,Long int,unsigned long,float,Double,long Doubletype ofsizeofin theANSI Cthere is no specific provision, the size depends on the reality, here with --bit system tests, respectively:

sizeof (int) =4

sizeof (unsigned int) =4

sizeof (short int) =2

sizeof (unsigned short) =2

sizeof (long int) =4

sizeof (unsigned long) =4

sizeof (float) =4

sizeof (double) =8

sizeof (long double) =8

3.sizeof relies on the compiler when the operand is a pointer . For example , in Microsoft c/c++7.0 , the Near class pointer byte number is 2,far, The number of pointer bytes for the huge class is 4. The average Unix pointer byte count is 4.

4, when the operand has an array type, the result is the total number of bytes in the array.

such as:int a[10],sizeof (a) =40.

5. sizeof of the union type operand is the number of bytes of its largest byte member.

such as:typedef Union ip{

Char a[1];

int net;

}ip;

sizeof (IP) =4.

6. sizeof of the struct type operand is the total number of bytes of this type of object, including any bedding. Because the address of a variable is typically a multiple of the number of bytes that the variable itself occupies.

multiples of Char 1

multiples of short 2

multiples of int 4

multiples of double 8

typedef struct goods{

Char NAME[18];//18

a multiple of double PRICE;/4, starting from

a multiple of char special;//1, starting from

multiples of int num;//4, starting from

a multiple of short SALED;//2, starting from

}gs;

name |  ....... |  Price |special|  |  | | num |  saled |  | |  name|  | |

0 18 20 28 29 32 36 38 ... new variable, multiples of 4 start

printf ("sizeof (GS) =%d\n", sizeof (GS)) ==>, but run in the system as a , do not understand!!!!!

7.if the operand is an array parameter in a function or a formal parameter of a function type,sizeof gives its size only.

void Demo (char a) {

printf ("sizeof (a) =%d\n", sizeof (a));

}

Call Form Demo (' a ').

The output is:sizeof (a) =4.

Iv. the relationship between sizeof and other operators

Sizeof has a priority level of 2 , and a 3 -level operator with a higher precedence than /,% , and so on . It can form an expression together with other operators. such as i*sizeof (int), where i is a variable of type int.

V. main uses of sizeof

1. One of the main uses of thesizeof operator is to communicate with routines such as storage allocation and I/O systems. For example:

void *malloc (size_t size),

sizeof_t fread (void* ptr, size_t size, size_t nmemb, file* Stream).

2. The other main use ofsizeof is to calculate the number of elements in the array, for example:

void* memset (void* s, int c, sizeof (s)).

VI. recommendations

Because the number of bytes in the operand may change when implemented, it is recommended that sizeof be used instead of constant calculation when it comes to the size of the operand byte .

Vii. examples

A pen test, please write down the following code on the platform of the operation results, and explain the nature of sizeof :

#include <stdio.h>

#include <stdlib.h>

int main () {

Char a[30];

Char *b = (char*) malloc (20*sizeof (char));

printf ("sizeof (a) =%d\n", sizeof (a));

printf ("sizeof (b) =%d\n", sizeof (b));

printf ("sizeof (A[3) =%d\n", sizeof (a[3]));

printf ("sizeof (b+3) =%d\n", sizeof (B+3));

printf ("sizeof (* (b+4)) =%d\n", sizeof (* (b+4)));

return 0;

}

The result is:

sizeof (a) =30

sizeof (b) =4

sizeof (A[3]) =1

sizeof (B+3) =4

sizeof (* (b+4)) =1

Sizeof calculates the number of bytes that are related to the peace table.

A summary of the sizeof C language

Related Article

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.