C编程书错误还是编程错误?
它返回的是C编程书错误还是编程错误?,c,printf,stdout,C,Printf,Stdout,它返回的是3570,而不是70,这本书说它应该返回。我做错了什么?程序是正确的。它正在打印它要打印的代码 澄清一下, 您有两个printf()s,打印35和70 在printf()s中没有“分隔符”[例如,新行(\n)]来区分两条打印语句的输出 结果:您将看到最终输出3570是两个打印语句35和70的输出组合 解决方案:在printf()中提供的格式字符串末尾添加一个\n或\t,以便在每个printf()之后添加一个可视分隔符,以避免混淆。只需这样做即可 int main() { // Init
3570
,而不是70
,这本书说它应该返回。我做错了什么?程序是正确的。它正在打印它要打印的代码
澄清一下,
printf()
s,打印35
和70
printf()
s中没有“分隔符”[例如,新行(\n
)]来区分两条打印语句的输出3570
是两个打印语句35
和70
的输出组合
解决方案:在printf()
中提供的格式字符串末尾添加一个\n
或\t
,以便在每个printf()
之后添加一个可视分隔符,以避免混淆。只需这样做即可
int main()
{
// Initialize & Declare variable
int m = 5;
// Allocates memory for storage of an integer variable
int *itemp;
// Stores memory address of variable m in memory address itemp
itemp = &m;
// Notice after declaring pointer you don't need to reference it as a pointer
// asterick is also known as indirection operator
// indirect reference: Accessing the contents of a memory cell through a pointer variable that stores it's address
// We can rewrite the contents in the memory cell as such
*itemp = 35;
printf("%d",*itemp);
// Doubles the value of m
*itemp = 2 * *itemp;
printf("%d",*itemp);
return 0;
}
您将35和70视为同一行中的输出,在它们之间添加空格或换行。您将打印
*itemp
两次。第一次它的值是35
,第二次它的值是70
。所以它的打印3570
。您对指针的注释是错误的。它“分配”内存用于存储指向int
的指针。。。谢谢大家。如果你每天至少不说几次“哇,我真蠢……”,你就不是在编程。只需更改第一个printf(“%d”,*itemp);到printf(“%d\n”,*itemp);看魔术缓冲在这里是不相关的。由于printf之间没有停顿,人类无论如何也看不到在“70”之前打印的“35”。我想他/她是说这与缓冲无关,因为两个printf之间不会发生任何事情,如果不缓冲,它将是相同的。@teppic啊,我明白了。可能是想得太多了。我会简化的。
printf("%d\n",*itemp);