Interview Preparation Exam  >  Interview Preparation Notes  >  Placement Papers - Technical & HR Questions  >  The C Language Basics (Part - 1) - C Interview Questions and Answers

The C Language Basics (Part - 1) - C Interview Questions and Answers | Placement Papers - Technical & HR Questions - Interview Preparation PDF Download

The C Language Basics

1. What is a local block?
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 <stdio.h>
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.


2. Should variables be stored in local blocks?

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.


3. When is a switch statement better than multiple if statements?

A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type. For instance, rather than the code

if (x == 1) 
printf("x is equal to one.\n");
else if (x == 2) 
printf("x is equal to two.\n");
else if (x == 3) 
printf("x is equal to three.\n");
else 
printf("x is not equal to one, two, or three.\n");

the following code is easier to read and maintain:

switch (x)
{ 
case 1: printf("x is equal to one.\n"); break; 
case 2: printf("x is equal to two.\n"); break; 
case 3: printf("x is equal to three.\n"); break; 
default: printf("x is not equal to one, two, or three.\n"); break;
}

Notice that for this method to work, the conditional expression must be based on a variable of numeric type in order to use the switch statement. Also, the conditional expression must be based on a single variable. For instance, even though the following if statement contains more than two conditions, it is not a candidate for using a switch statement because it is based on string comparisons and not numeric comparisons:

char* name = "Lupto";
if (!stricmp(name, "Isaac")) 
printf("Your name means 'Laughter'.\n");
else if (!stricmp(name, "Amy")) 
printf("Your name means 'Beloved'.\n ");
else if (!stricmp(name, "Lloyd")) 
printf("Your name means 'Mysterious'.\n ");
else 
printf("I haven't a clue as to what your name means.\n");

4. Is a default case necessary in a switch statement?

No, but it is not a bad idea to put default statements in switch statements for error- or logic-checking purposes. For instance, the following switch statement is perfectly normal:

switch (char_code)
{ 
case 'Y': case 'y': printf("You answered YES!\n"); break; 
case 'N': case 'n': printf("You answered NO!\n"); break;
}

Consider, however, what would happen if an unknown character code were passed to this switch statement. The program would not print anything. It would be a good idea, therefore, to insert a default case where this condition would be taken care of:

... 
default: printf("Unknown response: %d\n", char_code); break;
...

Additionally, default cases come in handy for logic checking. For instance, if your switch statement handled a fixed number of conditions and you considered any value outside those conditions to be a logic error, you could insert a default case which would flag that condition. Consider the following example:

void move_cursor(int direction)
{ 
switch (direction) 
{ case UP: cursor_up(); break; 
case DOWN: cursor_down(); break; 
case LEFT: cursor_left(); break; 
case RIGHT: cursor_right(); break; 
default: printf("Logic error on line number %ld!!!\n", __LINE__); break; }
}

 

5. Can the last case of a switch statement skip including the break?

Even though the last case of a switch statement does not require a break statement at the end, you should add break statements to all cases of the switch statement, including the last case. You should do so primarily because your program has a strong chance of being maintained by someone other than you who might add cases but neglect to notice that the last case has no break statement.

This oversight would cause what would formerly be the last case statement to "fall through" to the new statements added to the bottom of the switch statement. Putting a break after each case statement would prevent this possible mishap and make your program more "bulletproof." Besides, most of today's optimizing compilers will optimize out the last break, so there will be no performance degradation if you add it.


6. Other than in a for statement, when is the comma operator used?

The comma operator is commonly used to separate variable declarations, function arguments, and expressions, as well as the elements of a for statement. Look closely at the following program, which shows some of the many ways a comma can be used:

 

#include <stdio.h>
#include <stdlib.h>
void main(void);
void main()
{
     /* Here, the comma operator is used to separate
        three variable declarations. */
     int i, j, k;
     /* Notice how you can use the comma operator to perform
        multiple initializations on the same line. */
     i = 0, j = 1, k = 2;
     printf("i = %d, j = %d, k = %d\n", i, j, k);
     /* Here, the comma operator is used to execute three expressions
        in one line: assign k to i, increment j, and increment k.
        The value that i receives is always the rightmost expression. */
     i = (j++, k++);
     printf("i = %d, j = %d, k = %d\n", i, j, k);
     /* Here, the while statement uses the comma operator to
        assign the value of i as well as test it. */
     while (i = (rand() % 100), i != 50)
          printf("i is %d, trying again...\n", i);
     printf("\nGuess what? i is 50!\n");
}

 

Notice the line that reads

i = (j++, k++);

This line actually performs three actions at once. These are the three actions, in order:

1. Assigns the value of k to i. This happens because the left value (lvalue) always evaluates to the rightmost argument. In this case, it evaluates to k. Notice that it does not evaluate to k++, because k++ is a postfix incremental expression, and k is not incremented until the assignment of k to i is made. If the expression had read ++k, the value of ++k would be assigned to i because it is a prefix incremental expression, and it is incremented before the assignment is made.

2. Increments j.

3. Increments k.

Also, notice the strange-looking while statement:

 

while (i = (rand() % 100), i != 50)
     printf("i is %d, trying again...\n");

 

Here, the comma operator separates two expressions, each of which is evaluated for each iteration of the whilestatement. The first expression, to the left of the comma, assigns i to a random number from 0 to 99.

The second expression, which is more commonly found in a while statement, is a conditional expression that tests to see whether i is not equal to 50. For each iteration of the while statement, i is assigned a new random number, and the value of i is checked to see that it is not 50. Eventually, i is randomly assigned the value 50, and the while statement terminates.

The document The C Language Basics (Part - 1) - C Interview Questions and Answers | Placement Papers - Technical & HR Questions - Interview Preparation is a part of the Interview Preparation Course Placement Papers - Technical & HR Questions.
All you need of Interview Preparation at this link: Interview Preparation
85 docs|57 tests

Top Courses for Interview Preparation

FAQs on The C Language Basics (Part - 1) - C Interview Questions and Answers - Placement Papers - Technical & HR Questions - Interview Preparation

1. What are the basic data types in C language?
Ans. The basic data types in C language are int, float, char, double, and void.
2. How are variables declared and initialized in C?
Ans. Variables in C are declared by specifying the data type, followed by the variable name. They can be initialized at the time of declaration by assigning a value using the assignment operator (=). For example, int num = 10;
3. What is the difference between break and continue statements in C?
Ans. The break statement is used to terminate the execution of the loop or switch statement and transfer control to the next statement after the loop or switch. On the other hand, the continue statement is used to skip the remaining statements in the loop and move to the next iteration.
4. What is the difference between call by value and call by reference in C?
Ans. In call by value, a copy of the variable's value is passed to the function. Any changes made to the parameter inside the function do not affect the original variable. In call by reference, the address of the variable is passed to the function. Any changes made to the parameter inside the function will affect the original variable.
5. What is the purpose of the scanf() function in C?
Ans. The scanf() function is used to read input from the user in C. It allows the program to wait for user input and store the input in variables. The function uses format specifiers to determine the type of data being entered and converts it to the appropriate data type.
Explore Courses for Interview Preparation exam

Top Courses for Interview Preparation

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

practice quizzes

,

mock tests for examination

,

ppt

,

Free

,

Extra Questions

,

MCQs

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

video lectures

,

Important questions

,

The C Language Basics (Part - 1) - C Interview Questions and Answers | Placement Papers - Technical & HR Questions - Interview Preparation

,

Objective type Questions

,

Summary

,

past year papers

,

pdf

,

Exam

,

Semester Notes

,

The C Language Basics (Part - 1) - C Interview Questions and Answers | Placement Papers - Technical & HR Questions - Interview Preparation

,

Viva Questions

,

Sample Paper

,

study material

,

The C Language Basics (Part - 1) - C Interview Questions and Answers | Placement Papers - Technical & HR Questions - Interview Preparation

;