Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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.


XML
//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();
}


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.
Posted

There is no a problem at all. It all is calculated as expected. Make sure you understand what a pre-increment should do, know the difference between pre-increment and post-increment.

—SA
 
Share this answer
 
Comments
VISH_a_CODE 21-Jul-14 13:05pm    
Thnx for your clarification sir, and I am using post increment and decrement operation. But still why doesn't it shows right output. Which would be 7 and 8 for increment and 5 and 4 for decrement.
Sergey Alexandrovich Kryukov 21-Jul-14 13:25pm    
It is a right output. If you need different output (you never defined what it should be), use preincrement, or do it in separate steps as Solution 2 shows.
I recommend you sort things in your head and then accept both Solution 1 and 2 (green "Accept" button).
—SA
VISH_a_CODE 21-Jul-14 21:38pm    
Thnx...:)
Sergey Alexandrovich Kryukov 21-Jul-14 23:11pm    
You are welcome.
—SA
No, that's right.
C#
x++
is a postincrement operation - it gets the value then increases the source by one and uses teh value it started with.
So
C++
printf("\nThe Increment gives:-     %d  &  %d\n",x++,y++);
Is the equivalent of:
C++
printf("\nThe Increment gives:-     %d  &  %d\n",x,y);
x = x + 1;
y = y + 1;
There is a preincrement operator if you want to try that instead:
C++
printf("\nThe Increment gives:-     %d  &  %d\n",++x,++y);
That is the equivalent of:
C++
x = x + 1;
y = y + 1;
printf("\nThe Increment gives:-     %d  &  %d\n",x,y);
Which might be what yopu were expecting.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jul-14 13:27pm    
Looking at the last OP's comment to my answer, OP really needed that clarification (my 5), but still did not get it. :-(
—SA
VISH_a_CODE 21-Jul-14 21:38pm    
I think I get it Thankx...:)
OriginalGriff 22-Jul-14 1:50am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900