简体   繁体   English

C程序加2的整数会产生异常输出

[英]C Program to add 2 integers produce abnormal output

I've already looked all over the internet but haven't found anything related to this. 我已经在互联网上看了很多,但没有找到任何与此相关的信息。

Here's the output 这是输出

Enter First Number: 2
Enter Second Number: 3
Total: 6422308Press any key to continue . . .

Here's the code 这是代码


#include <stdio.h>
#include <stdlib.h>

int main() {

    int num1;
    int num2;

    int total;

    printf("Enter First Number: ");
    scanf("%i", &num1);

    printf("Enter Second Number: ");
    scanf("%i", &num2);

    total = num1 + num2;

    printf("Total: %i", &total);
    system("pause");

    return 0;
}

Well, I expect the output to be proper 好吧,我希望输出是正确的

Your Problem The line printf("Total: %i", &total); 您的问题printf("Total: %i", &total); should be changed to printf("Total: %i", total); 应该更改为printf("Total: %i", total);

The Reason The & unary operator in C gets the address of the operand(variable). 原因 C中的&一元运算符获取操作数(变量)的地址。 Notice in the usage of scanf you need the '&' operator. 请注意,在使用scanf您需要使用'&'运算符。 The reason why you need it for scanf is because the variable needs to be modified outside of the scope of the calling function(the scope of main() in our case). 之所以需要scanf的原因是因为需要在调用函数的范围之外(在本例中为main()的范围)修改变量。 On the other hand printf just needs the value of the variable, so just the variable name total is needed. 另一方面, printf只需要变量的值,因此只需要变量名total

To Clarify 0x6422308 is the address in memory where the variable total is located which is equivalent to &total . 要澄清 0x6422308是变量total所在的内存地址,该地址等于&total

You are currently printing the memory address of total, and not what the value stored in that memory address is. 当前正在打印的内存地址为total,而不是该内存地址中存储的值。 This is because c utilizes pointers, allowing you to set memory addresses, access information from memory addresses, and use pointer arithmetic to sift through memory. 这是因为c利用了指针,允许您设置内存地址,从内存地址访问信息以及使用指针算法在内存中进行筛选。 When prefixing a variable with the & operator, you are essentially declaring that you want the memory address that variable is stored in. If you wanted the value of a memory address, you would have to dereference the memory address with the * operator. 当使用&运算符为变量加上前缀时,实际上是在声明您要存储该变量的内存地址。如果想要一个内存地址的值,则必须使用*运算符来取消引用该内存地址。 I haven't tried this, but you could do something like printf("Total %i",*(&total)) to dereference the memory address of total to get the value. 我没有尝试过,但是您可以执行诸如printf("Total %i",*(&total))来取消引用total的内存地址以获得值。

The above is a way too complicated for this application however, and you do not need to mess with memory addresses in printing for this case. 上面对于此应用程序来说太复杂了,在这种情况下,您无需在打印时弄乱内存地址。 Therefore, you can just pass the variable total to printf and everything will be good (since total is not a pointer). 因此,您只需将变量total传递给printf,一切都会很好(因为total不是指针)。

Remove the & inside of your total printf and everything should work correctly. 删除总printf中的& ,一切应该正常工作。 You can see how to use printf and scanf here 您可以在此处查看如何使用printf和scanf

&x stands for the address of the variable x . &x代表变量x的地址。 So scanf(..., &x) makes sense because the function scanf requires an address of a variable for putting data. 所以scanf(..., &x)很有意义,因为函数scanf需要变量的地址来放置数据。 And printf(&x) outputs an address of the variable x , instead of value of this variable. 然后printf(&x)输出变量x的地址,而不是该变量的值。 You should use printf(x) to obtain the expected result. 您应该使用printf(x)获得预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM