/*Program: mem_addr.c*/ #include<stdio.h> intmain(void) { /*declare some integer variables*/ long a=1L; long b=2L; long c=3L; int g=4; /*declare some floating-point variables */ double d=4.0; double e=5.0; double f=6.0; printf("A varible of type long occupies %d byte.",sizeof(long)); printf("nHere are the addresses of some variables of type long:"); printf("nThe address of a is :%p, The address of b is :%p",&a,&b); printf("nThe address of c is :%p",&c); printf("nnA varible of type double occupies %d bytes.", sizeof(double)); printf("nHere are the addresses of somae varibles of type double:"); printf("nThe address of d is :%p, The address of e is :%p",&d,&e); printf("nThe address of f is :%p",&f); printf("nnA varible of type int occupies %d bytes.n", sizeof(int)); return0; }
1.2 编译 mem_addr.c
1
[pg92@redhatB array]$ gcc -o mem_addr mem_addr.c
1.3 执行 mem_addr.c
1 2 3 4 5 6 7 8 9 10 11 12
[pg92@redhatB array]$ ./mem_addr A varible of type long occupies 4 byte. Here are the addresses of some variables of type long: The address of a is :0xbfe26418, The address of b is :0xbfe26414 The address of c is :0xbfe26410 A varible of type double occupies 8 bytes. Here are the addresses of somae varibles of type double: The address of d is :0xbfe26408, The address of e is :0xbfe26400 The address of f is :0xbfe263f8 A varible of type int occupies 4 bytes.
备注: long int 类型占用的字节为 4,并且输出了变量 a,b,c 在内存中的地址; double 类型占用的字节为 8,并且输出了变量 d,e,f 在内存中的地址。