[习题12]数组和大小 :sizeof(以字节为单位查看大小)

使用教材

《“笨办法” 学C语言(Learn C The Hard Way)》
https://www.jianshu.com/p/b0631208a794

ex12.c

#include 

int main(int argc, char *agrv[]) 
{
    int areas[] = {10,12,13,14,20};
    char name[] = "Zed";
    char full_name[] = {
        'Z','e','d',
        ' ','A','.',' ',
        'S','h','a','w','\0'
    };

    //WARNING:On some systems you may have to change the
    //%ld in this code to a %u since it will use unsigned ints
    printf("The size of an int: %ld\n", sizeof(int));
    printf("The size of areas (int []): %ld\n", sizeof(areas));
    printf("The number of ints in areas: %ld\n", sizeof(areas)/sizeof(int));
    printf("The first area is %d, the 2nd %d.\n", areas[0], areas[1]);

    printf("The size of a char: %ld\n", sizeof(char));
    printf("The size of name (char[]): %ld\n", sizeof(name));
    printf("The number of chars: %ld\n", sizeof(name)/sizeof(char));

    printf("The size of full_name (char[]): %ld\n", sizeof(full_name));
    printf("The number of chars: %ld\n", sizeof(full_name)/sizeof(char));

    printf("name=\"%s\" and full_name=\"%s\"\n", name, full_name);

    return 0;
}

Makefile

CC=clang
CFLAGS=-Wall -g

clean:
    rm -f ex12

run

anno@anno-m:~/Documents/mycode/ex12$ make ex12
clang -Wall -g    ex12.c   -o ex12
anno@anno-m:~/Documents/mycode/ex12$ ./ex12
The size of an int: 4
The size of areas (int []): 20
The number of ints in areas: 5
The first area is 10, the 2nd 12.
The size of a char: 1
The size of name (char[]): 4
The number of chars: 4
The size of full_name (char[]): 12
The number of chars: 12
name="Zed" and full_name="Zed A. Shaw"

Note

  • 关键词sizeof字节为单位来查看东西的大小;
  • 这台机器认为int的大小是4字节
  • C语言就是一门关于内存大小内存位置以及如何处理内存的语言;

你可能感兴趣的:([习题12]数组和大小 :sizeof(以字节为单位查看大小))