问题描述
您好,我刚刚在Microsoft Visual Studio 2010中用C语言编写了一个简单的基本程序。在程序中出现了如下问题。
Hello, I was just writing a simple basic program in C language in Microsoft Visual Studio 2010. There is a problem occurred in it as follows in a program.
//Program showing all the binary operators used in C.
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,c,d;
printf("Enter The Value of X and Y : ");
scanf("%d %d",&x,&y);
printf("\n\nThe integers are %d & %d\n",x,y);
printf("\nThe Addition gives:- %d\n",x+y);
printf("\nThe Subtraction gives:- %d\n",x-y);
printf("\nThe Multiplication gives:- %d\n",x*y);
printf("\nThe Division gives:- %d\n",x/y);
printf("\nThe Modulus gives:- %d\n",x%y);
printf("\nThe Increment gives:- %d & %d\n",x++,y++);
printf("\nThe Decrement gives:- %d & %d\n",x--,y--);
getch();
}
然后我得到了输出。
输入X和Y的值: 6 5
整数 6& 5
加法给出: - 11
减法给出: - 1
乘法给出: - 30
该司给出: - 1
模数给出: - 1
增量给出: - 6& 5
递减给出: - 7& 6
问题在于递增和递减操作。这是软件故障还是我错过了什么!但我认为在Visual Studio 2010中尝试了一切,但输出不会改变。
Thanx。
Then I got the Output.
Enter The Value of X and Y : 6 5
The integers are 6 & 5
The Addition gives:- 11
The Subtraction gives:- 1
The Multiplication gives:- 30
The Division gives:- 1
The Modulus gives:- 1
The Increment gives:- 6 & 5
The Decrement gives:- 7 & 6
The problem is in Increment and Decrement operation. Is that a software fault or I missed something! But I think tried everything in Visual Studio 2010 but output wont change.
Thanx.
推荐答案
x++
是 后增量 操作 - 它获取值然后将源增加1并使用它开始的值。
所以
is a postincrement operation - it gets the value then increases the source by one and uses teh value it started with.
So
printf("\nThe Increment gives:- %d & %d\n",x++,y++);
相当于:
Is the equivalent of:
printf("\nThe Increment gives:- %d & %d\n",x,y);
x = x + 1;
y = y + 1;
如果您想尝试,请设置 preincrement 运算符:
There is a preincrement operator if you want to try that instead:
printf("\nThe Increment gives:- %d & %d\n",++x,++y);
这相当于:
That is the equivalent of:
x = x + 1;
y = y + 1;
printf("\nThe Increment gives:- %d & %d\n",x,y);
这可能是yopu所期待的。
Which might be what yopu were expecting.
这篇关于C语言问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!