Interview Questions

C

C - Switch case statements

We saw the use of if...else... statements in the first round. We will see switch()...case in this chapter. Remember that it can only be used to compare values of variables and choices it can take.

#include<stdio.h>

void main (void)
{
        unsigned short int myVal = 0;
       
        switch (myVal)
        {
                case 1:
                        printf (“myVal is 1\n”);
                        break;  /* mandatory to put break. This makes the program go to the next line out of switch statement */
                case 2:
                        printf (“myVal is 2\n”);
                        break;
                case 3:
                        printf (“myVal is 3\n”);
                        break;

C - If Else Loop

C as a procedural programming languages lets the programmer design and implement decision/logic using constructs. By this we mean the following:

If... else... – blocks of code used to take choice between 2 or more decisions on logic, based on a true and false condition. The condition itself could be derived based on a value of a variable, output of executing a command, etc.

Switch... case... – blocks of code used to take choice between multiple decision points on logic based on comparing multiple values for a variable or a condition’ output.

C - Variables Second Round

In the previous chapter, we saw that a character variable can hold 1 byte of value. How do I store a value – say “techdive” in a character variable. It is not possible in a character variable that holds 1 byte of value. Here the value “techdive” seems to hold more than 1 byte of value. What is the solution?

C - Variables First Round

Just as any other programming language, C supports various types of variables for processing of the required functionality by methods. You might recall that all operations require data to operate upon and this is achieved by variables that hold values.

Data in a programming language is used to represent the problem statement and to achieve a desired output. Variables represent different forms of data representing real time values.

In C, we have the following data types:
Char – 1 byte
Int – 2 bytes
Long – 4 bytes
Float – 4 bytes
Double – 4 bytes

C - Functions First Round

We saw in the earlier chapter about the function main() as pre-requisite for any C program. To understand C a bit more, let us call (invoke) functions from main() and understand what it means. Later, we will use this concept to understand functions in detail.

#include <stdio.h>
void myFunction1(void); /* line 1 – this is declaring functions before use*/
void myFunction2(void); /* line 2 – also called as declaring the prototype */
void myFunction3(void); /* line 3 */
void myFunction4(void); /* line 4 */

main()
{

A Simple Example

Let us understand how a simple C program works. I am taking the first program of all C developers as my choice to explain this.

#include <stdio.h>
main()
{
  printf (“Hello World!);
}

(Type in the above program in any editor, save the file as first.c and compile using cc –c first.c –o first in your present directory. You will be able to execute the program by ./first. You can see the output as Hello World.)

C Programming Language

One of the oldest and longest running programming language is C. It was "invented" by Dennis Ritchie and Kerning ham as an alternative programming language to assembly level languages prevailing then.

To understand the evolution, please refer books by Ritchie. In this section we will start seeing the various aspects of C – the programming language.