Technical Interview Questions and Answers :: C
1 / 123
The ++ operator is called the increment operator. When the operator is placed before the variable (++var), the variable is incremented by 1 before it is used in the expression. When the operator is placed after the variable (var++), the expression is evaluated, and then the variable is incremented by 1.
The same holds true for the decrement operator (--). When the operator is placed before the variable, you are said to have a prefix operation. When the operator is placed after the variable, you are said to have a postfix operation.
For instance, consider the following example of postfix incrementation:
int x, y;
x = 1;
y = (x++ * 5);
In this example, postfix incrementation is used, and x is not incremented until after the evaluation of the expression is done. Therefore, y evaluates to 1 times 5, or 5. After the evaluation, x is incremented to 2.
Now look at an example using prefix incrementation:
int x, y;
x = 1;
y = (++x * 5);
This example is the same as the first one, except that this example uses prefix incrementation rather than postfix. Therefore, x is incremented before the expression is evaluated, making it 2. Hence, y evaluates to 2 times 5, or 10.
2 / 123
On what basis u have rated. It might be depend on the question
3 / 123
A local block is any portion of a C program that is enclosed by the left brace ({) and the right brace (}). A C function contains left and right braces, and therefore anything between the two braces is contained in a local block. An if statement or a switch statement can also contain braces, so the portion of code between these two braces would be considered a local block.
Additionally, you might want to create your own local block without the aid of a C function or keyword construct. This is perfectly legal. Variables can be declared within local blocks, but they must be declared only at the beginning of a local block. Variables declared in this manner are visible only within the local block. Duplicate variable names declared within a local block take precedence over variables with the same name declared outside the local block. Here is an example of a program that uses local blocks:
#include
void main(void);
void main()
{
/* Begin local block for function main() */
int test_var = 10;
printf("Test variable before the if statement: %d\n", test_var);
if (test_var > 5)
{
/* Begin local block for "if" statement */
int test_var = 5;
printf("Test variable within the if statement: %d\n",
test_var);
{
/* Begin independent local block (not tied to
any function or keyword) */
int test_var = 0;
printf(
"Test variable within the independent local block:%d\n",
test_var);
}
/* End independent local block */
}
/* End local block for "if" statement */
printf("Test variable after the if statement: %d\n", test_var);
}
/* End local block for function main() */
This example program produces the following output:
Test variable before the if statement: 10
Test variable within the if statement: 5
Test variable within the independent local block: 0
Test variable after the if statement: 10
Notice that as each test_var was defined, it took precedence over the previously defined test_var. Also notice that when the if statement local block had ended, the program had reentered the scope of the original test_var, and its value was 10.
4 / 123
The use of local blocks for storing variables is unusual and therefore should be avoided, with only rare exceptions. One of these exceptions would be for debugging purposes when you might want to declare a local instance of a global variable to test within your function. You also might want to use a local block when you want to make your program more readable in the current context. Sometimes having the variable declared closer to where it is used makes your program more readable. However, well-written programs usually do not have to resort to declaring variables in this manner, and you should avoid using local blocks.
5 / 123
A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.
6 / 123
No switch cases can exist with out default case, in switch case default case will trigger when it is not match with any other case values.
7 / 123
it's optinal because last case is defaut statement no need the default statement neceesary.
8 / 123
If a break is used in a loop. like below
i = 0
while (i < 10)
{
if (i == 3)
break;
}
9 / 123
Modulus operator % calculates the remainder when used
say a%b will find the remainder when the number a is divided b .
10 / 123
Bitmasking refers to the technique of representing a subset of a set using bits of a number .
for eg suppose we have n numbers {1,2,3,4,5,6,7,8} in an array
then a bitmask to indicate the subset{2,3,6} of the above set will be a set of binary values like {0,1,1,0,0,1,0,0} where 1 indicates element at ith index belong to set and 0 indicated that it doesnt.
this 0,1 information can also be stored in bits of a number.
for eg consider a number mask=0;
if we want to set the ith bit of mask to 1 we can apply following operation
mask = mask|(1<
where '|' is bitwise or and '<<' is bitwise left shift operator
and the ith bit will become 1 indicating the number is included in the set
similarly to remove a number from the set following operating can be performed
mask = mask^(1<
where '^' is bitwise xor operator