Assignment Operators in C

In this video tutorial we show Compound Assignment Operators in C programming language.

Related Read:
Simple Calculator Application In C

Assignment Operators

If a and c are the operands.

c += a; is equal to writing c = c + a;
c -= a; is equal to writing c = c – a;
c *= a; is equal to writing c = c * a;
c /= a; is equal to writing c = c / a;
c %= a; is equal to writing c = c % a;

Assignment Operator +=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c += a ; // c = c + a;

    printf("%d\n\n", c);

    return 0;
}

Output:
50

Assignment Operator -=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c -= a ; // c = c - a;

    printf("%d\n\n", c);

    return 0;
}

Output:
10

Assignment Operator *=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c *= a ; // c = c * a;

    printf("%d\n\n", c);

    return 0;
}

Output:
600

Assignment Operator /=

 
#include < stdio.h >

int main()
{
    int a = 2, c = 20;

    c /= a ; // c = c / a;

    printf("%d\n\n", c);

    return 0;
}

Output:
10

Assignment Operator %=

 
#include < stdio.h >

int main()
{
    int a = 2, c = 20;

    c %= a ; // c = c % a;

    printf("%d\n\n", c);

    return 0;
}

Output:
0

Compound Assignment Operators in C Programming Language


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

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


You can also learn about Logical and Relational Operators in C:
Relational Operators In C
Logical Operators In C

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

C Programming Interview / Viva Q&A: 2

This C program has a integer variable and 2 printf functions. Check the source code below and guess the output.

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int a;

    a = printf("IBM");

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

    return 0;
}

Output 1:
IBM
3

Output 2:
Microsoft
9

Output 3:
Apple
5

Output 4:
Oracle
6

C Programming Interview / Viva Q&A: 2 (printf)


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

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


Important: printf() function returns the number of characters printed by it.

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

C Programming Interview / Viva Q&A List

We’ll keep adding more and more C programming interview / viva question and answers to this list. Stay subscribed to our blog and YouTube channel.

You can also find full C programming video tutorial list here:
C Programming: Beginner To Advance To Expert

  1. C Programming Interview / Viva Q&A: 1 – assignment and eqality operators
  2. C Programming Interview / Viva Q&A: 2 – printf
  3. C Programming Interview / Viva Q&A: 3 (Logical Operator)
  4. C Programming Interview / Viva Q&A: 4 (Logical NOT Operator)
  5. Programming Interview / Viva Q&A: 5 (while loop)

C Programming Interview / Viva Q&A: 1

Guess the output of this simple C program. You can check these 2 video tutorials listed below so that you get to know todays interview / viva question.

Related Read:
if else statement in C
Relational Operators In C

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int num;

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

    if(num = 5)
    {
        printf("You entered number 5\n");
    }
    else
    {
        printf("You entered %d \n", num);
    }

    return 0;
}

If user enters num value as 10. What does the above C Program output?

Answer: Did you answer – You entered 10 as output? If yes, then its wrong.

Note:
1. If at first glance of the source code you didn’t get the answer right, then go through the videos I’ve posted in ‘Related Read” section above.
2. After going through the 2 videos I’ve posted in ‘Related Read’ section above, if you still didn’t get the answer, then please open your editor and type the program and execute it one line at a time.

Hint: assignment operator(=) and equality operator(==) are different and work differently in C programming.

C Programming Interview / Viva Q&A: 1


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

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


Here is the Answer:

As you can see in the condition of if, it’s written as num = 5. = is assignment operator. So here 5 is being assigned to variable num, and thus it returns 5. In C program, any non-zero number is considered as true. So every time the condition in if becomes true, so the block of code present in if gets executed. i.e., value of num is 5.

using equality operator for comparison

 
#include < stdio.h >

int main()
{
    int num;

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

    if(num == 5)
    {
        printf("You entered number 5\n");
    }
    else
    {
        printf("You entered %d \n", num);
    }

    return 0;
}

Output 1:
Enter a number
10
You entered 10

Output 2:
Enter a number
5
You entered number 5

Output 3:
Enter a number
1000
You entered 1000

Note: Both programs are correct syntactically. That’s the reason compiler did not throw any error. It’s a logical error, so we need to be very careful while writing programs. If we do this simple logical error in a big program, it might take a lot of time to get to know the bug and fix it. So careful. Happy coding.

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

Employee Bonus Calculation: C

In this c program, we ask the user/employee to enter current year and his / her year of joining the organization/company. We check if the employee has served the company / organization for more than 3 years. If yes, then we give him/her a bonus of $2500 and if he or she has served for less than 3 years we’ll give $1000 as bonus.

Related Read:
if else statement in C
Relational Operators In C

Source Code: Employee Bonus Calculation: C

 
#include < stdio.h >

int main()
{
    int bonus, cy, yoj;

    printf("Enter current year and year of joining\n");
    scanf("%d %d", &cy, &yoj);

    if( (cy - yoj) > 3 )
    {
        bonus = 2500;
    }
    else
    {
        bonus = 1000;
    }

    printf("You get a bonus of %d \n", bonus);

    return 0;
}

Output 1:
Enter current year and year of joining
2019
2017
You get a bonus of 1000

Output 2:
Enter current year and year of joining
2019
2014
You get a bonus of 2500

C Program to Calculate Employee Bonus


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

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


In this program we check if the employee has worked for 3 years in the organization or not. We subtract his year of joining from current year and then based on that we decide his or her bonus.

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