当前位置:网站首页>++ problems in C language

++ problems in C language

2022-06-12 09:05:00 Record dada I

I have a very interesting question today , After reading it, I think it is very meaningful , So record it , Prevent everyone from stepping on the pit .

The grammar problem of the original text is to ask the final apd What is the value of .

#include<iostream>
using namespace std;
int main()
{
    
	int sum=5,apd=5;
	apd = sum++;
	apd++;
	++apd;
	cout<<apd<<sum;
	return 0;
 } 

The problem lies in apd=sum++ On ;
apd=sum++; This statement will be executed first apd=sum; Immediate speaking sum The value is assigned to apd, And then execute sum++;
So the output value apd=7,sum=6;

int sum=5;
sum=sum++;
cout<<sum;

sum=sum++; No matter how many times you cycle , The end result is 5;

#include<iostream>
using namespace std;
int main()
{
    
	int sum=5;
	for(int i=0;i<5;i++)
	{
    
	sum=sum++;
	}
	cout<<sum++;
	cout<<sum;
	return 0;
} 

Output after cycling sum by 5, Then the output sum++ It's also 5;
final sum by 6;
as a result of sum++ Take effect in the next statement ,sum++ Always loop assign to sum, This causes the values in memory to be overwritten in a loop , So it's always been 5;

原网站

版权声明
本文为[Record dada I]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010531527533.html