c语言 sizeof size_t,C/C++中的sizeof运算符和size_t类型的详解
sizeof的作用
sizeof是c的運(yùn)算符之一,用于獲取操作數(shù)被分配的內(nèi)存空間,以字節(jié)單位表示.
這里指的操作數(shù),可以是變量,也可以是數(shù)據(jù)類型,如int,float等.所以就可以通過它來獲取本地c庫定義的基本類型的范圍。
sizeof的使用
1.對于一般變量,形式2種:sizeof a 或 sizeof(a);
2.對于數(shù)據(jù)類型,必須使用帶括號的方式,如sizeof(int).
size_t的說明
size_t是標(biāo)準(zhǔn)C庫中定義的,應(yīng)為unsigned int,在64位系統(tǒng)中為 long unsigned int。
sizeof返回的必定是無符號整形,在標(biāo)準(zhǔn)c中通過typedef將返回值類型定義為size_t.
若用printf輸出size_t類型時(shí),C99中定義格式符%zd;若編譯器不支持可以嘗試%u或%lu.
sizeof和size_t
常常會有人認(rèn)為 在C/C++中 sizeof 是一個(gè)函數(shù),因?yàn)橥ǔT谑褂?sizeof 的時(shí)候會帶上圓括號” () “。而實(shí)際上, C/C++中的sizeof 是一個(gè)運(yùn)算符。
它的運(yùn)算對象可以是具體的數(shù)據(jù)對象(例如變量名)或者數(shù)據(jù)類型,如果運(yùn)算對象是一個(gè)數(shù)據(jù)類型,則必須使用圓括號將其括起來。
#include "stdio.h"
int main(void)
{
int n = 10;
//以下兩種寫法均正確
printf("%dn",sizeof (int));
printf("%dn",sizeof n);
return 0;
}
//輸出結(jié)果為:
//4
//412345678910111213141516
在C語言的規(guī)定中,sizeof 運(yùn)算符的結(jié)果是 size_t ,它是由 typedef 機(jī)制定義出來的”新”類型。
在使用 size_t 類型時(shí),編譯器會根據(jù)不同系統(tǒng)來替換標(biāo)準(zhǔn)類型,從而讓程序有良好的可移植性。
//C/C++用 typedef 把 size_t 作為 unsigned int或 unsigned long 的別名
//size_t 的定義如下
// 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(s,m) ((size_t)&reinterpret_cast((((s*)0)->m)))
#else
#define offsetof(s,m) ((size_t)&(((s*)0)->m))
#endif
#else
#define offsetof(s,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
我們可以簡單的理解為size_t 有如下兩種定義
typedef unsigned int size_t
thpedef unsigned long size_t12
我們可以用 %zd(C99標(biāo)準(zhǔn)新增)、%u、%lu 轉(zhuǎn)換說明用于 printf() 顯示 size_t 類型的值
#include "stdio.h"
int main(void)
{
size_t intsize = sizeof (int);
printf("%zdn",sizeof (int));
printf("%zdn",intsize);
printf("%un",intsize);
printf("%lun",intsize);
return 0;
}
//輸出結(jié)果為:
//4
//4
//4
//4
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對編程之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
總結(jié)
如果覺得編程之家網(wǎng)站內(nèi)容還不錯,歡迎將編程之家網(wǎng)站推薦給程序員好友。
本圖文內(nèi)容來源于網(wǎng)友網(wǎng)絡(luò)收集整理提供,作為學(xué)習(xí)參考使用,版權(quán)屬于原作者。
小編個(gè)人微信號 jb51ccc
喜歡與人分享編程技術(shù)與工作經(jīng)驗(yàn),歡迎加入編程之家官方交流群!
總結(jié)
以上是生活随笔為你收集整理的c语言 sizeof size_t,C/C++中的sizeof运算符和size_t类型的详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言1到20找最大和最小相邻,一组数据
- 下一篇: vc获取n卡编号_电脑入门知识:通过显卡