我的代码是:
#include <stdio.h>
#include <string.h>
void main()
{
char string[10];
int A = -73;
unsigned int B = 31337;
strcpy(string, "sample");
// printing with different formats
printf("[A] Dec: %d, Hex: %x, Unsigned: %u\n", A,A,A);
printf("[B] Dec: %d, Hex: %x, Unsigned: %u\n", B,B,B);
printf("[field width on B] 3: '%3u', 10: '%10u', '%08u'\n", B,B,B);
// Example of unary address operator (dereferencing) and a %x
// format string
printf("variable A is at address: %08x\n", &A);
我正在使用Linux Mint终端编译,但当我尝试使用gcc编译时,出现以下错误信息:basicStringFormatting.c: In function ‘main’:
basicStringFormatting.c:18:2: warning: format ‘%x’ expects argument
of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("variable A is at address: %08x\n", &A);
我所尝试做的就是打印变量A在内存中的地址。
void main()
应该改成int main(void)
。如果你的书、教程或者导师告诉你使用void main()
,那么找一个更好的。 - Keith Thompsonvoid
关键字,并规定主函数可以定义为int main(void) { /* ... */ }
。它没有提到void main()
,符合标准的实现可能会拒绝它。有许多书籍和教程建议使用void main()
;这强烈表明作者对语言不是很熟悉。(这是针对托管实现的情况。对于自由实现,程序入口点是实现定义的,程序不在操作系统下运行。) - Keith Thompson