w3resource

C Exercises: Demonstrates the difference between predecrementing and postdecrementing using the decrement operator

C Basic Declarations and Expressions: Exercise-78 with Solution

Demonstrate predecrementing vs postdecrementing

Write a C program to demonstrate the difference between predecrementing and postdecrementing using the decrement operator --.

Sample Solution:

C Code:

#include<stdio.h>
int main()
{
    int x = 10; // Initialize variable x with value 10
    
    // Predecrementing
    printf("Predecrementing:\n");
    printf("x   = %d\n", x); // Print current value of x
    printf("x-- = %d\n", x--); // Print x and then decrement it
    printf("x   = %d\n\n", x); // Print updated value of x
    
    x = 10; // Reset x to 10
    
    // Postdecrementing
    printf("Postdecrementing:\n");
    printf("  x = %d\n", x); // Print current value of x
    printf("--x = %d\n", --x); // Decrement x and then print it
    printf("  x = %d\n", x); // Print updated value of x
    
    return 0;
}

Sample Output:

Predecrementing:
x   = 10
x-- = 10
x   = 9

Flowchart:

C Programming Flowchart: Demonstrates the difference between predecrementing and postdecrementing using the decrement operator.

C programming Code Editor:

Previous:Write a C program that accepts principal amount, rate of interest and days for a loan and calculate the simple interest for the loan, using the following formula.
Next: Write a C program using looping to produce the following table of values.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-78.php