C Operator sizeof - sonkoni/Koni-Wiki GitHub Wiki

Operator | Precedence | sizeof | Bitwise

sizeof

sizeof ν‘œν˜„μ‹  sizeof(μžλ£Œν˜•)  sizeof(ν‘œν˜„μ‹)

μžλ£Œν˜•μ΄ λͺ‡ Byte 인지 size_t ν˜•μœΌλ‘œ λ°˜ν™˜ν•œλ‹€.
μ‹œμŠ€ν…œμ— 따라 1 Byte λŠ” CHAR_BIT 둜 μ •μ˜ν•œλ‹€. 일반적으둜 8λΉ„νŠΈκ°€ 1λ°”μ΄νŠΈμ΄λ‹€.

sizeof λŠ” μ—°μ‚°μžμ΄λ‹€. ν•¨μˆ˜ λͺ¨μ–‘을 ν•˜κ³  μžˆμ§€λ§Œ μ—°μ‚°μžκ°€ λ§žλ‹€. ν•¨μˆ˜κ°€ μ‹€ν–‰μ‹œμ μ—μ„œ ν˜ΈμΆœλ˜λŠ” κ²ƒκ³ΌλŠ” 달리 sizeof λŠ” μ—°μ‚°μžμ΄κΈ° λ•Œλ¬Έμ— μ»΄νŒŒμΌμ‹œμ μ—μ„œ μ—°μ‚°λœλ‹€.

#include <stdio.h>

int main(int argc, char *argv[]) {
    
    short x;
    // type argument:
    printf("sizeof(float)          = %zu\n", sizeof(float));
    printf("sizeof(void(*)(void))  = %zu\n", sizeof(void(*)(void)));
    printf("sizeof(char[10])       = %zu\n", sizeof(char[10]));
    
    // expression argument:
    printf("sizeof 'a'             = %zu\n", sizeof 'a');       // type of 'a' is int
    printf("sizeof &main           = %zu\n", sizeof &main);
    printf("sizeof \"hello\"         = %zu\n", sizeof "hello"); // type is char[6]
    printf("sizeof x               = %zu\n", sizeof x);         // type of x is short
    printf("sizeof (x+1)           = %zu\n", sizeof(x+1));      // type of x+1 is int
    
    return 0;
}
// sizeof(float)          = 4
// sizeof(void(*)(void))  = 8
// sizeof(char[10])       = 10
// sizeof 'a'             = 4
// sizeof &main           = 8
// sizeof "hello"         = 6
// sizeof x               = 2
// sizeof (x+1)           = 4
⚠️ **GitHub.com Fallback** ⚠️