简体   繁体   English

为什么以下程序的输出每次都不同(随机值)?

[英]why the output of the following program different (random value) every time?

Can we assign the string value to the int variable?我们可以将字符串值分配给 int 变量吗? what conversion is happening at line int a="aa";在 int a="aa" 行发生了什么转换; I am running this C program and the value is printing different for each program execution.我正在运行这个 C 程序,并且每个程序执行的值都不同。

 int main() 
{
   int a="aa";
   printf("%d",++a);
   return 0;
 }

In C, the string literal "aa" is a char * (in C++ a const char * ) to a read-only memory location of the string "aa" .在 C 中,字符串文字"aa"是一个char * (在 C++ 中为const char * )到字符串"aa"的只读内存位置。 You are converting this pointer to an integer, ie the memory address to a number.您正在将此指针转换为整数,即将内存地址转换为数字。 Depending on the platform you are using, this memory address may change every time your program runs.根据您使用的平台,每次程序运行时,此内存地址可能会更改。

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

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