C Language Interview Questions

C Language Interview Questions

Questions on Operators in C Language:

1. What are Assignment operators in C language?
The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.
For example,
x = a + b
Here the value of a + b is evaluated and substituted to the variable x.
In addition, C has a set of shorthand assignment operators of the form.
var oper = exp;
Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator
For example,
x + = 1 is same as x = x + 1.

2. What are increment and decrement operators in C language?
The increment and decrement operators are one of the unary operators which are very useful in C language. They are extensively used in for and while loops. The syntax of the operators is
•    ++ variable name
•    variable name++
•    – –variable name
•    variable name– –
The increment operator ++ adds the value 1 to the current value of operand and the decrement operator – – subtracts the value 1 from the current value of operand. ++variable name and variable name++ mean the same thing when they form statements independently, they behave differently when they are used in expression on the right hand side of an assignment statement.
Consider the following
m = 5;
y = ++m; (prefix)
In this case the value of y and m would be 6.
Suppose if we rewrite the above statement as,
m = 5;
y = m++; (post fix)
Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left. On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand.

3. What are conditional or ternary operators in C language?
The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false. It works from left to right.
condition? result1: result2
If the condition is true, result1 is returned else result2 is returned.
For example,
10==5? 11: 12 // returns 12, since 10 not equal to 5.
10! =5? 4: 3 // returns 4, since 10 not equal to 5.
12>8? a: b // returns the value of a, since 12 is greater than 8.

4. What is the use of bitwise operators in C language?
The smallest element in memory on which is able to operate is called byte; and the programming languages are byte oriented. One of the c powerful features is a set of bit manipulation operators. The bitwise operator performs the operation on bits (i.e. bit by bit). Using the bitwise operators we can set / reset / check any bit in the value of the variable.
There are three types of bitwise operator.
•    Bitwise AND(&)
•    Bitwise OR(|)
•    Bitwise Exclusive OR(^)
Bitwise AND operator: The output of logical AND is 1 if both the corresponding bits of operand is 1. If either of bit is 0 or both bits are 0, the output will be 0. It is a binary operator (works on two operands) and indicated in C programming by & symbol.
Bitwise OR operator: The output of bitwise OR is 1 if either of the bit is 1 or both the bits are 1. In C Programming, bitwise OR operator is denoted by |.
Bitwise XOR(exclusive OR) operator The output of bitwise XOR operator is 1 if the corresponding bits of two operators are opposite(i.e., To get corresponding output bit 1; if corresponding bit of first operand is 0 then, corresponding bit of second operand should be 1 and vice-versa.). It is denoted by ^.

5. What is sizeof () operator? 
The operator size of gives the size of the data type or variable in terms of bytes occupied in the memory. The operand may be a variable, a constant or a data type qualifier.
For example,
m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);
The size of operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during the execution of the program.

6. What is comma operator?
The comma operator can be used to link related expressions together. A comma-linked list of expressions is evaluated left to right and value of right most expression is the value of the combined expression.
For example, the statement
value = (x = 10, y = 5, x + y);
First assigns 10 to x and 5 to y and finally assigns 15 to value. Since comma has the lowest precedence in operators the parenthesis is necessary. Some examples of comma operator are,
In for loops:
for (n=1, m=10, n <=m; n++,m++)
In while loops
While (c=getchar (), c! = ‘10’)
Exchanging values
t = x, x = y, y = t;

7. What is an L-value?

An L-value is an expression to which a value can be assigned. The L-value expression is located on the left side of an assignment statement, whereas an R-value is located on the right side of an assignment statement. Each assignment statement must have an L-value and an R-value. The L-value expression must reference a storable variable in memory. It cannot be a constant. For instance, the following lines show a few examples of L-values:
int x;
int* p_int;
x = 1;
*p_int = 5;
The variable x is an integer, which is a storable location in memory. Therefore, the statement x = 1 qualifies x to be an L-value. Notice the second assignment statement, *p_int = 5. By using the * modifier to reference the area of memory that p_int points to, *p_int is qualified as an L-value.

8. What is an R-value?
R-value can be defined as an expression that can be assigned to an L-value. The R-value appears on the right side of an assignment statement.
Unlike an L-value, an R-value can be a constant or an expression, as shown here:
int x, y;
x = 1; /* 1 is an R-value; x is an L-value */
y = (x + 1); /* (x + 1) is an R-value; y is an L-value */
An assignment statement must have both an L-value and an R-value. Therefore, the following statement would not compile because it is missing an R-value:
int x; x = void_function_call() /* the function void_function_call() returns nothing */
If the function had returned an integer, it would be considered an R-value because it evaluates into something that the L-value, x, can store.

9. What is arrow operator?
In C programming we use arrow operator to access structure member using the pointer variable.
Syntax and Use of Arrow operator:
struct student
char name [20];
int roll;
Whenever we declare structure variable then member can be accessed using the dot operator. But when pointer to a structure is used then arrow operator is used.

10. What is unary operator?
Unary Operators are those operators which operate on 1 operand like/example a++, ++a,--b etc. The letters a, b are called operands and ++, -- are called as Operators.

11. What is binary operator?

A type of operator that works with two operands is known as binary operators.
+,-,*, /, % these are binary operators.


Post a Comment