Operator Precedence / Priority: C

Often times we use arithmetic operations, relational operators, logical operators and assignment operators together in a instruction / statement. We must know the precedence / priority of the operator so that we can write proper code.

Related Read:
Basic Arithmetic Operations In C
Relational Operators In C
Logical Operators In C

Operator Hierarchy / Precedence / Priority

PriorityOperatorType
1!Logical NOT Operator
2* / %Arithmetic Operator
3+ –Arithmetic Operator
4< > <= >=Relational Operator
5== !=Relational Operator
6&&Logical AND Operator
7||Logical OR Operator
8=Assignment Operator

Note: Write down above table of operator priority on a piece of paper and have it handy while writing the code. You’ll get used to it after some time – until then, keep revisiting the hierarchy of operators.

Operator Precedence / Priority / Hierarchy: C


[youtube https://www.youtube.com/watch?v=hdbLSF5xQDI]

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


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

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

Post-increment and Pre-increment Operator: C Program

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

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

Post-increment and Pre-increment Operator: C Program


[youtube https://www.youtube.com/watch?v=zdd7_hOorn0]

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


Pre-increment 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 = 11
a = 11

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

#include < stdio.h >

int main()
{
    int a = 10;

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

    return 0;
}

Output:
a = 11
a = 11

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

Post-increment 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 = 11

Here first value of a(i.e., 10) is assigned to b and then value of a is incremented. So b = 10 and a = 11 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 = 11

Here in the first printf statement a value gets printed after that its value gets incremented, 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

Fibonacci Series using While loop: C Program

Today lets see how to generate Fibonacci Series using while loop in C programming.

First Thing First: What Is Fibonacci Series ?
Fibonacci Series is a series of numbers where the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Its recurrence relation is given by Fn = Fn-1 + Fn-2.

Below are a series of Fibonacci numbers(10 numbers):
0
1
1
2
3
5
8
13
21
34

How Its Formed:
0 <– First Number (n1)
1 <– Second Number (n2)
1 <– = 1 + 0
2 <– = 1 + 1
3 <– = 2 + 1
5 <– = 3 + 2
8 <– = 5 + 3
13 <– = 8 + 5
21 <– = 13 + 8
34 <– = 21 + 13

Generate Fibonacci Series using While loop: C Program


[youtube https://www.youtube.com/watch?v=LhM2pvYwMDs]

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


Source Code: Fibonacci Series using While loop: C Program

#include < stdio.h >

int main()
{
    int n1 = 0, n2 = 1, n3, count;

    printf("Enter the limit\n");
    scanf("%d", &count);

    printf("\n%d\n%d\n", n1, n2);

    count = count - 2;

    while(count)
    {
        n3 = n1 + n2;
        printf("%d\n", n3);
        n1 = n2;
        n2 = n3;
        count = count - 1;
    }


    return 0;
}

Output:
Enter the limit
10

0
1
1
2
3
5
8
13
21
34

Logic to Generate Fibonacci Series in C Programming Language

We know that the first 2 digits in fibonacci series are 0 and 1. So we directly initialize n1 and n2 to 0 and 1 respectively and print that out before getting into while loop logic. Since we’ve already printed two fibonacci numbers we decrement the value of variable count by 2.

Inside while loop
We already know that C program treat any non-zero number as true and zero as false. So once the value of variable count is zero, the execution of while loop stops. So the condition while(count) is equal to writing while(count == 0). We decrement the value of count by 1 each time the loop executes. So once count is 0 the loop execution stops.

As per definition of Fibonacci series: “..each subsequent number is the sum of the previous two.” So we add n1 and n2 and assign the result to n3 and display the value of n3 to the console.

Next, we step forward to get next Fibonacci number in the series, so we step forward by assigning n2 value to n1 and n3 value to n2.

While loop keeps repeating above steps until the count is zero. If the user entered limit/count as 10, then the loop executes 8 times(as we already printed the first two numbers in the fibonacci series, that is, 0 and 1).

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

Even or Odd Number using Ternary Operator and without using Modular Division: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD, without using modular division(%) operator and by using Ternary Operator / Conditional Operator.

Related Read:
Even or Odd Number without using Modular Division: C Program
Ternary Operator / Conditional Operator In C

Please visit the links I’ve posted above without fail before watching the video posted below.

Even or Odd Number: Source Code

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter a integer number\n");
    scanf("%d", &n);

    ( (n/2)*2 == n ) ?
    (printf("%d is Even\n", n)) :
    (printf("%d is Odd\n", n));


    return 0;
}

Output 1:
Enter a integer number
10
10 is Even

Output 2:
Enter a integer number
5
5 is Odd

Even or Odd Number using Ternary Operator and without using Modular Division: C Program


[youtube https://www.youtube.com/watch?v=4-AoNsgYJQ4]

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


Note: Division of an integer number by 2(which is also an integer number) always returns integer number.

Example 1:
If user enters n = 2;
Applying n = 2 to ( (n/2)*2 == n ).
( (2/2)*2 == 2 )
( (1)*2 == 2 )
( 2 == 2 ) // true

So user entered value, that is, 2 is even number.

Example 2:
If user enters n = 3;
Applying n = 3 to ( (n/2)*2 == n ).
( (3/2)*2 == 3 )
( (1)*2 == 3 )
( 2 == 3 ) // false

2 is not equal to 3. So user entered value, that is, 3 is odd number.
In above example, 3/2 gives back 1 and not 1.5 as 3 is divided by integer which returns only the integer part and discards the decimal part.

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