News Leaflets
A leading news portal.

Pre-Decrement and Post-Decrement in C

0

Pre-decrement and post-decrement are the two ways of using the decrement operator to decrement the value of a variable by 1. They can be used with numeric data type values such as int, float, double, etc. Pre-decrement and Post-decrement perform similar tasks with minor distinctions.

In this article, we will discuss the pre-decrement and post-decrement operators in C.

Pre-Decrement in C

In pre-decrement operation, the decrement operator is used as the prefix of the variable. The decrement in value happens as soon as the decrement operator is encountered.

Syntax of Pre-Decrement

--variable_name

For example,

var = --a;

The  above statement is equivalent to:

a = a - 1;
var = a;

Let’s understand the pre-decrement operator with the help of a C program.

Example of Post-Decrement

C

#include <stdio.h>

  

int main()

{

  

    int num1 = 5;

  

    

    

    int num2 = --num1;

  

    printf("num1 = %d", num1);

  

    

    printf("\n");

  

    printf("num2 = %d", num2);

  

    return 0;

}

Explanation: In this example, we define an integer ‘num1’ with value 5. After that, we decrement the ‘num1’ and store the value of num1 in num2. whenever we use a pre-decrement operator it decrements the value of the variable first and then returns that value. Here firstly num1 is decremented by 1 and then store in the variable num2. So, values of both num1 and num2 become 4 after decrement as seen in the output.

Post-Decrement in C

In post-decrement operation, the value is decremented after all the other operations are performed i.e. all the other operators are evaluated. The decrement operator is used as the suffix to the variable name.

Syntax of Post-Decrement

variable_name--

For example,

var = a--;

The above expression is equivalent to:

var = a;
a = a - 1;

Here, we can see the difference between pre-decrement and post-decrement operators.

Example of Post-Decrement

C

#include <stdio.h>

  

int main()

{

  

    int num1 = 5;

  

    

    

    int num2 = num1--;

  

    printf("num1 = %d", num1);

  

    

    printf("\n");

  

    printf("num2 = %d", num2);

  

    return 0;

}

Explanation: This example is the same as example 1 but in this example, we have used the post decrement operator. The post-decrement operator first returns the value and then decremented the value by 1. We store the post-decrement value of num1 in num2 and then we print both num1 and num2 and we can see in the output the value of num1 is decremented by 1 but that decremented value is not stored in num2 so, the value of num2 is 5. This is because of the concept we discussed that it first returns the value and then decrements the value by 1.

Example of Pre-Decrement and Post-Decrement

C

#include <stdio.h>

  

int main()

{

  

    int num1 = 5, num2 = 10;

  

    int postNum = num1--;

    int preNum = --num2;

  

    

    

    printf("postNum = %d \n", postNum);

    printf("num1 = %d", num1);

    

    

    printf("\n");

    

    

    

    printf("preNum = %d \n", preNum);

    printf("num2 = %d", num2);

  

    return 0;

}

Output

postNum = 5 
num1 = 4
preNum = 9 
num2 = 9


Explanation: In the above example, we have applied post decrement on ‘num1’ and pre decrement on ‘num2’ and assigned their value to ‘postNum’ and ‘preNum’ respectively. In the output, we can see that value of ‘postNum’ is 5 which is not the decremented value of ‘num1’ because post decrement assigns the value before the decrement value, and when we print the value of ‘num1’ it is decreased by 1. On the other side value of ‘preNum’ and ‘num2’ is 9 which is decreased by 1 because pre decrement operator first decreased the value by 1 and then assigns its value to another variable or used that value in an expression.

Precedence and Associativity of Pre-decrement and Post-Decrement

  • The precedence of post-decrement is more than pre-decrement post-decrement comes under the postfix category and pre-decrement is treated as unary operator.
  • The associativity of pre-decrement is from right to left while that of post-decrement is from left to right.

FAQs on Pre-Decrement and Post-Decrement in C

Q1. What happens when we use pre-decrement and post-decrement with numeric literals?

Answer:

The use of decrement operator with constants results in error because the decrement operator only works on rvalues (the values to which we can assign some constant values) such as variables and numeric literals are rvalues.

Example:

C

#include <stdio.h>

  

int main()

{

  

    int a, b;

  

    a = 10 --;

    b = --10;

  

    printf("a=%d, b=%d", a, b);

  

    return 0;

}

Output

sum.c: In function 'main':
sum.c:7:9: error: lvalue required as decrement
operand
a = 10--;
^~
sum.c:8:7: error: lvalue required as decrement
operand
b = --10;

In the above example, we have applied the decrement operator to literals and we get the error “lvalue required as decrement operand” which means a variable should be used so that decremented value is stored in that variable because “10 = 10 -1” is a wrong statement and we know that “a–” is equivalent to “a = a-1”.

Q2. What is the difference between Pre-Decrement and Post-Decrement in C?

Answer:

There are only two major differences between the pre-decrement and post-decrement operators in C. They are as follows:

  1. Syntax: The pre-decrement is added as prefix to the variable, while the post-decrement is appended as suffix.
  2. Operation: The dercement is immediately performed in case of pre-decrement while in post-decrement, all the other operations are performed first, only then decrement is done.

Last Updated :
06 Jun, 2023

Like Article

Save Article

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! News Leaflets is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment
vulvatube.com teenpornvideo.mobi desi girls sexy
sex video in bus tubekitty.mobi kamapichachi
the broken marriage vow march 12 full episode teleseryepisode.com mateo lorenzo net worth
telugu hd sex pornflex.org bluefilmtamil
indian sex stories lesbian indaporn.com best sex mms
hinde xnxx video redwap2.com bangla chudachudi
طيز خليجي filmstreamingporno.com التحرش بالمنقبات
affair sex video ultraporn.mobi deshimagi
tubb99 nuporn.mobi mumbaixvideo
agimat ng agila cast watchpinoyteleserye.com stl today result
age of tamanna pimpmovs.com xxxxx inden
indan xvideo com xxx-tube-list.net indina six video
gonzo xxx sunny leone eporner.name desi pirn
بنت تلعب في كسها teentubeonline.com سكس امهات اسيوي
largeporn film tubepatrol.porn kama katai