这是C中的一个简单程序,用于说明C如何为变量分配内存。我的想法是每次重新编译程序时都会显示一个新的内存地址。
以下是该计划:
#include<stdio.h>
main()
{
int a;
int b = 10;
float c = 5.8;
printf("the address is %u \n", &a);
printf("the address is %u \n", &b);
printf("the address is &u \n", &c);
getch();
}
当我运行此gcc program_name.c
时,我收到一系列错误:
program_name.c:3:1: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
main()
^
program_name.c:9:33: warning: format specifies type 'unsigned int' but the
argument has type 'int *' [-Wformat]
printf("the address is %u \n", &a);
~~ ^~
program_name.c:10:33: warning: format specifies type 'unsigned int' but the
argument has type 'int *' [-Wformat]
printf("the address is %u \n", &b);
~~ ^~
program_name.c:11:33: warning: format specifies type 'unsigned int' but the
argument has type 'float *' [-Wformat]
printf("the address is %u \n", &c);
~~ ^~
program_name.c:13:2: warning: implicit declaration of function 'getch' is
invalid in C99 [-Wimplicit-function-declaration]
getch();
^
5 warnings generated.
Undefined symbols for architecture x86_64:
我无法理解为什么会抛出这些错误。当然%u
是一个完全合适的格式说明符。我错过了什么?如何重新格式化代码以阻止这些错误?
编辑:根据回复,似乎正确的代码应该是
#include<stdio.h>
int main()
{
int a;
int b = 10;
float c = 5.8;
printf("the address is %p \n", &a);
printf("the address is %p \n", &b);
printf("the address is %p \n", &c);
return 0;
}
这确实运行没有错误。