Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : Logical Operators - 2Next Topic : Assignment Operator - 2

This section on C interview questions, tricky questions, and puzzles focuses on “Assignment operator”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, viva exams and company interviews), placements, entrance exams and other competitive exams. These programming questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate, engineering student, bca student, mca student, mcs students or an experienced IT professional. Here is a listing of C questions and puzzles on “Assignment operators” along with answers and/or explanations.

Q1: What will be the value of a and b ?
int main()
{
         int a,b;
       a=(100,200,300);
       b=100,200,300;
        printf("a=%d,b=%d",a,b);
       return 0;
}
a)  100 300
b)  100 100
c)  300 300
d)  300 100
Correct Answer : d
Explanation : When assigning values using a comma separated list, the last value is used for assignment, if the list is enclosed in parenthesis otherwise first value is used for assignment.


Q2: What will be the output of following C code?
#include <stdio.h>
int main()
{    int a = 10, 20, 30;    
     printf("%d", a);    
     return 0;
}
a)  10
b)  20
c)  30
d)  Syntax Error
Correct Answer : d
Explanation : Comma acts as a separator here. The compiler creates an integer variable and initializes it with 10. The compiler fails to create integer variable 20 because 20 is not a valid identifier.


Q3: Predict the output.
# include <stdio.h> 
int main() 
{   int x = 10,y = 20; 
    x += y += 10; 
    printf (" %d %d", x, y); 
    return 0; 
} 
a)  40 20
b)  20 40
c)  40 30
d)  30 40
Correct Answer : c
Explanation : In the statement “x += y += 10”, there are two += operators in the statement, here associativity comes into the picture. Associativity of compound assignment operators is right to left, so the expression is evaluated as x += (y += 10).


Previous Topic : Logical Operators - 2Next Topic : Assignment Operator - 2