C Programming Interview Questions Answers Part-II

52) How do you search data in a data file using random access method?

Use the fseek() function to perform random access input/output on a file. After the file was opened by the fopen() function, the fseek() would require three parameters to work: a file pointer to the file, the number of bytes to search, and the point of origin in the file.

53) What is the advantage of a random access file?

If the amount of data stored in a file is fairly large, the use of random access will allow you to search through it quicker. If it had been a sequential access file, you would have to go through one record at a time until you reach the target data. A random access file lets you jump directly to the target address where data is located.

54) Are comments included during the compilation stage and placed in the EXE file as well?

No, comments that were encountered by the compiler are disregarded. Comments are mostly for the guidance of the programmer only and do not have any other significant use in the program functionality.learn-embedded-system

55) Is there a built-in function in C that can be used for sorting data?

Yes, use the qsort() function. It is also possible to create user defined functions for sorting, such as those based on the balloon sort and bubble sort algorithm.

56) What are the advantages and disadvantages of a heap?

Storing data on the heap is slower than it would take when using the stack. However, the main advantage of using the heap is its flexibility. That’s because memory in this structure can be allocated and remove in any particular order. Slowness in the heap can be compensated if an algorithm was well designed and implemented.

57) How do you convert strings to numbers in C?

You can write you own functions to do string to number conversions, or instead use C’s built in functions. You can use atof to convert to a floating point value, atoi to convert to an integer value, and atol to convert to a long integer value.

58) What are command line arguments?

The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. First argument represents the count of arguments (below in count) and updated automatically by operating system.

main( int count, char *args[]){ 

}

59) Create a simple code fragment that will swap the values of two variables num1 and num2.

int temp; 
temp = num1; 
num1 = num2; 
num2 = temp;

60) What is super loop or endless loop?
Super loop is the infinite loop that runs all the time because , most of the embedded systems has no OS in it to return to application.

//Super loop example
while(true)
{
 //all other code
}

//or
for(;;)
{
}

//or
label:
goto label:		//This is is tricky assembly version

61) What is the difference between Structure and union? Where do we use union?

When a structure is defined the compiler allocates memory for all of its member variables with necessary alignment .Whereas for unions the compiler allocates memory for the highest the union is equal to the biggest size of member from the union member variable list. union can only store information in one field at any one time.

#include <stdio.h>

struct mStruct
{
    char name[10];
    int age;
    float height;
};

union mUnion
{
    char name[15];
    int age;
    float height;
};

int main(void)
{
    union  mUnion  uTest;
    struct mStruct sTest;
    strcpy(sTest.name,"sTest");
    sTest.age    = 20;
    sTest.height = 6.1;
    strcpy(uTest.name ,"uTest");
    uTest.height = 6.0;
    uTest.age    = 20;
    printf("\n");
    printf("sizeof(uTest) = %d ,sizeof(sTest) = %d \n",sizeof(uTest),sizeof(sTest));
    printf("uTest.age = %d , uTest.height = %f , uTest.name = %s\n",uTest.age, uTest.height, uTest.name);
    printf("sTest.age = %d , sTest.height = %f , sTest.name = %s\n",sTest.age, sTest.height, sTest.name);
    printf("\n");
}

62) What are bitwise shift operators?

<< – Bitwise Left-Shift
Bitwise Left-Shift is useful when to want to MULTIPLY an integer (not floating point numbers) by a power of 2.
Expression: a << b 

>> – Bitwise Right-Shift
Bitwise Right-Shift does the opposite, and takes away bits on the right.
Expression: a >> b 

This expression returns the value of a divided by 2 to the power of b.

63) Set, Get , Clear ,Toggle , Display Bits ? 

// Display Every bits in a int number 
void displayBits(int data)
{
    int dataSize = 1<<sizeof(data);
    int count = 0;
    for (count = dataSize;count >= 0; count--)
    {
        printf("%d",(testBit(data,count)));
    }
}
// test a bit if it is zero or one 
int8_t testBit(int8_t value,int whichBit)
{
    int mask = 1 << whichBit;
    if (value&mask)
    {
        return TRUE;
    }
    else return FALSE;
}

// Set a bit to one 
int8_t setBit(int8_t result, int whichBit)
{
    return (result |= (1<<whichBit));
}

// toggle a bit  
int8_t toggleBit(int8_t result, int whichBit)
{
    return (result ^= (1<<whichBit));
}

/* Clear a bit to zero */
int8_t clearBit(int8_t result, int whichBit)
{
    return (result &=~ (1<<whichBit));
}

64) Convert number 2 –> 37 without using any other operator but bitwise?

int x = 2;
printf("X before :%d\n",x);
x = (x<<x<<x) | (x<<x<<x) | x<<!!x | !!x;
printf("X After  :%d\n",x);

65) Which one would you prefer – a Macro or a Function?

It actually depends on the purpose of the program!

  • In case of macros, the corresponding code is inserted directly into your source code at the point where macro is called. There is no overhead involved in using a macro. This makes the Macros more efficient and faster than functions. However, macros are usually small and cannot handle large, complex coding constructs. So, if it is a complex situation that the program wants to handle, functions are more suitable.
  • Macros are expanded inline – this means that every time a macro occurs, the code is replicated. So, the code size in case of usage of macros will be larger in comparison to functions.

So, the choice of using macros or functions actually depends on your requirement – speed Vs program size. If your program contains small, repeated code sections, you should use Macros. If the program requires, large number of unique code lines – you should prefer functions.

66) Explain #pragma statements.

Implementations of C, C++ supports features unique to the OS or host machine.

  • #pragma directives offer a way for each compiler to offer machine and OS specific features while retaining overall compatibility.
  • Pragmas are used in conditional statements, to provide new pre-processor functionality or implementation-defused information to the compiler.

67) What is the format specifier for printing a pointer value?

  • %p format specifier displays the corresponding argument that is a pointer.
  • %x can be used to print a value in hexadecimal format.

68) What is the use of ?: operator?

This ?: operator is used to show the conditional statement that allows the output of the statement to be either true or false. This operator is used as:

expression?expression1:expression2

If the expression is true then the result will be expression1. If the expression is false then the result will be expression2. There is only one evaluation take place for the result. The expression that is used with arithmetic operators are easy to solve and it is easy to convert and apply to the expressions.

69) What is the condition that is applied with ?: operator?

The condition that has to be applied with the ?: operator is as follows:
The type of the result will be generated if there are two operands of compatible types. The pointers can be mixed together to give some result. The pointers that are involved for the result has two steps to follow:

  • If an operand is a pointer then the result will be of a pointer type.
  • If any of the operand is a null pointer constant then result will be considered of the operand. The example is shown below:
#include <stdio.h>
#include <stdlib.h>

void main(){
int i;
for(i=0; i <= 10; i++){
 printf((i&1) ? "odd\n" : "even\n");
}

exit(EXIT_SUCCESS);

I hope you have found this post useful. Normally in any interview, questions starts with basic concept of subject and later they continue further discussion on advance topics to check whether candidate have fully understood or not. This is why in C Programming Interview Questions Answers Part-II focuses more on basic fundamentals to open up interview for discussion. In next post will present problem solving task or you can say often asked programs. We will take some interesting case studies and solve them by programming. I recommend you to check next post (Test Your C Programming). Thank You and Good Luck!!!

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Login

Register | Lost your password?