Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the sizeof operator and the size_t type in Candlestick +

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use the sizeof operator and size_t type in CAccord Cure +". The explanation in the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn how to use the sizeof operator and size_t type in CAccord +.

The role of sizeof

Sizeof is one of the operators of c, which is used to get the memory space allocated by operands, expressed in bytes.

The operands referred to here can be variables or data types, such as int,float, etc. So you can use it to get the scope of the basic types defined by the local c library.

The use of sizeof

1. For general variables, there are two forms: sizeof an or sizeof (a)

two。 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 and long unsigned int in 64-bit systems.

The sizeof must return an unsigned integer. In standard c, the return type is defined as size_t. Typedef.

If you use printf to output size_t types, the format character% zd; is defined in C99. If the compiler does not support it, you can try% u or% lu.

Sizeof and size_t

It is often thought that sizeof is a function in sizeof +, because it is usually used with parentheses "()". In fact, the sizeof in CUniverse + is an operator.

Its Operand can be a specific data object (such as a variable name) or a data type, and if the Operand is a data type, it must be enclosed in parentheses.

# include "stdio.h" int main (void) {int n = 10; / / the following two words are correct: printf ("% d\ n", sizeof (int)); printf ("% d\ n", sizeof n); return 0;} / / the output result is: / / 4sizeof 412345678910111213141516

In the C language, the result of the sizeof operator is size_t, which is a "new" type defined by the typedef mechanism.

When using size_t types, the compiler replaces standard types according to different systems, making the program portable.

/ / typedef uses size_t as an alias for unsigned int or unsigned long / / size_t is defined as / / stddef.h// Copyright (c) Microsoft Corporation. All rights reserved.// The C Standard Library header.//#pragma once#define _ INC_STDDEF#include _ CRT_BEGIN_C_HEADER#ifdef _ cplusplus namespace std {typedef decltype (_ _ nullptr) nullptr_t;} using:: std::nullptr_t;#endif#if _ CRT_FUNCTIONS_REQUIRED _ ACRTIMP int* _ _ cdecl _ errno (void); # define errno (* _ errno ()) _ ACRTIMP errno_t _ cdecl _ set_errno (_ In_ int _ Value) _ ACRTIMP errno_t _ _ cdecl _ get_errno (_ Out_ int* _ Value) # endif / / _ CRT_FUNCTIONS_REQUIRED#if defined _ MSC_VER & &! defined _ CRT_USE_BUILTIN_OFFSETOF # ifdef _ cplusplus # define offsetof ((size_t) & reinterpret_cast (s*) 0)-> m)) # else # define offsetof ((size_t) & ((s*) 0)-> m)) # endif#else # define offsetof (sMagna m) _ builtin_offsetof (s) M) # endif_ACRTIMP extern unsigned long _ _ cdecl _ _ threadid (void) # define _ threadid (_ _ threadid ()) _ ACRTIMP extern uintptr_t _ _ cdecl _ _ threadhandle (void); _ CRT_END_C_HEADER123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

We can simply understand that size_t has the following two definitions

Typedef unsigned int size_tthpedef unsigned long size_t12

We can use% zd (added to the C99 standard),% u,% lu conversion instructions for printf () to display values of type size_t

# include "stdio.h" int main (void) {size_t intsize = sizeof (int); printf ("% zd\ n", sizeof (int)); printf ("% zd\ n", intsize); printf ("% u\ n", intsize); printf ("% lu\ n", intsize); return 0 } / / the output result is: / / 4 sizeof +, thank you for your reading, above is the content of "how to use the sizeof operator and size_t type in size_t +". After the study of this article, I believe you have a deeper understanding of how to use the sizeof operator and size_t type in CAccord +, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report