Post-decrement and Pre-decrement Operator: C Program


In this video tutorial we show the differences and working of post-decrement and pre-decrement operators.

Note: In pre-decrement, first the value of the variable is decremented after that the assignment or other operations are carried. In post-decrement, first assignment or other operations occur, after that the value of the variable is decremented.

Note:
a- -;
– -a;
a = a – 1;
a -= 1;

are all same if used independently.

Post-decrement and Pre-decrement Operator: C Program


[youtube https://www.youtube.com/watch?v=eaGXQ-GgLPw]

YouTube Link: https://www.youtube.com/watch?v=eaGXQ-GgLPw [Watch the Video In Full Screen.]


Pre-decrement Operator: C Program

#include < stdio.h >

int main()
{
    int a = 10, b;

    b = --a;

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

    return 0;
}

Output:
b = 9
a = 9

Here first the value of a decrements and then is assigned to variable b. So both a and b value will be 9.

#include < stdio.h >

int main()
{
    int a = 10;

    printf("a = %d\n\n", --a);
    printf("a = %d\n", a);

    return 0;
}

Output:
a = 9
a = 9

Here in the first printf statement a value gets decremented and then printed out.

Post-decrement Operator: C Program

#include < stdio.h >

int main()
{
    int a = 10, b;

    b = a--;

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

    return 0;
}

Output:
b = 10
a = 9

Here first value of a(i.e., 10) is assigned to b and then value of a is decremented. So b = 10 and a = 9 is printed.

#include < stdio.h >

int main()
{
    int a = 10;

    printf("a = %d\n\n", a--);
    printf("a = %d\n", a);

    return 0;
}

Output:
a = 10
a = 9

Here in the first printf statement a value gets printed after that its value gets decremented, which is shown in second printf statement.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *