Menu

Pre Post function call increment and decrement program in C operator

Function call increment and decrement program in C

First, you understand the function call increment and decrement program in C and How to solve increment and decrement operators in C, you need to know about Prefix and Postfix. Let try to understand it Via Small Program.

Post increment and Pre increment in C examples

#include <stdio.h>

int main()
{
int a=6;
printf("%d\n",a++);
printf("%d\n",a);
printf("%d\n",++a);

return 0;
}

Why we use (\t) LearnHere

Output is

6  7  8

Pre increment and Post increment in C questions

A simple task for you guys, we need to create a function call increment which returns nothing it takes an int parameter and it increments the actual value we pass to this function. Try using postfix ++ increment operator. Function call increment and decrement program in C

#include <stdio.h>
void increment(int* addr);
int main()
{
printf("Enter value to increment : \n");
int i=0;
scanf("%d",&i);
printf("you entered %d\n",i);
int *b=&i;
*(b+1)=10;
increment(&i);
printf("value after increment call is %d \n",i);
return 0;
}
void increment(int * addr){
int x=*addr;
printf("\n inside increment function before increment value = %d\n",*addr);
x++;
*addr=x;
printf("\n inside increment after incrementing value is %d\n",*addr);
}
Output

Enter value to increment :
6

you entered 6

inside increment function before increment value = 6

inside increment after incrementing value is 7
value after increment call is 7

Post increment and Pre increment precedence in C

Note:- There are two operators with the same precedence are used in an equation then you have to check how the compiler is executing them, in this case, ++a prefix operators will execute from the right side of the equation to the left side. Where a++ is postfix and it will execute from left side to right side.

 a++ means first get the current value of a and then update itt

Note:- when u have multiple operators of the same precedence I would suggest you use parenthesis ( ) to avoid confusion

( ) parentheses left to right
Function call increment and decrement program in C

Function call increment and decrement program in C

Difference between a++ and ++a in C

Right to left when it is prefix (++a) when it is postfix (a++) then left to right

sometimes it is undefined behavior too like it depends on the implementation of the compiler, how it interprets the equation that’s why I said it is better to use parenthesis when you have used multiple operators of the same precedence in ur equation which one should execute first.

What is the value containing both of them, ++a is prefix, it’s means to add 1 to the value of a before u further evaluate it, a++ means take the current value of a, and then next time when you evaluate a add 1 to it.

END

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Learn For Free.